yashm commited on
Commit
d4b59bf
·
verified ·
1 Parent(s): 011076f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
 
2
  import numpy as np
3
- from PIL import ImageDraw, Image
4
 
5
  def main():
6
  st.title("Streamlit Whiteboard for Note-Taking")
@@ -10,43 +10,46 @@ def main():
10
 
11
  # Create a canvas as a numpy array
12
  canvas = np.ones((400, 800, 3), dtype=np.uint8) * 255
13
- image_location = st.image(canvas, caption="Draw Here", use_column_width=True)
14
 
15
- # Get the drawing context
16
- draw = ImageDraw.Draw(image_location)
17
 
18
- # Track whether the mouse is currently clicked
19
- mouse_clicked = False
 
20
 
21
- # Track the position of the last mouse click
22
- last_position = None
23
 
24
- # Function to handle mouse events
25
- def handle_mouse_event(event, x, y, _):
26
- nonlocal mouse_clicked, last_position
27
  if event == "mousedown":
28
- mouse_clicked = True
29
- last_position = (x, y)
30
- elif event == "mousemove" and mouse_clicked:
31
- draw.line([last_position, (x, y)], fill=(0, 0, 0), width=5)
32
- last_position = (x, y)
33
- st.image(canvas)
 
34
  elif event == "mouseup":
35
- mouse_clicked = False
36
-
37
- # Set up event handling
38
- image_location.mousemove(handle_mouse_event, True)
39
- image_location.mousedown(handle_mouse_event, True, "left")
40
- image_location.mouseup(handle_mouse_event, True, "left")
41
 
42
  # Buttons for interaction
43
  if st.button("Clear Canvas"):
44
  canvas[:] = 255
45
- st.image(canvas)
46
 
47
  if st.button("Save Notes"):
48
  save_notes(note)
49
 
 
 
 
 
 
 
 
 
50
  def save_notes(note):
51
  with open("notes.txt", "w") as file:
52
  file.write(note)
 
1
  import streamlit as st
2
+ from PIL import Image
3
  import numpy as np
 
4
 
5
  def main():
6
  st.title("Streamlit Whiteboard for Note-Taking")
 
10
 
11
  # Create a canvas as a numpy array
12
  canvas = np.ones((400, 800, 3), dtype=np.uint8) * 255
 
13
 
14
+ # Display the canvas
15
+ st.image(canvas, caption="Draw Here", use_column_width=True)
16
 
17
+ # Initialize drawing variables
18
+ drawing = False
19
+ previous_point = None
20
 
21
+ # Handle mouse events
22
+ mouse_event = st._get_last_mouse_event()
23
 
24
+ if mouse_event is not None:
25
+ event, x, y = mouse_event
 
26
  if event == "mousedown":
27
+ drawing = True
28
+ previous_point = (x, y)
29
+ elif event == "mousemove" and drawing:
30
+ # Draw a line from the previous point to the current point
31
+ canvas = draw_line(canvas, previous_point, (x, y))
32
+ previous_point = (x, y)
33
+ st.image(canvas, caption="Draw Here", use_column_width=True)
34
  elif event == "mouseup":
35
+ drawing = False
 
 
 
 
 
36
 
37
  # Buttons for interaction
38
  if st.button("Clear Canvas"):
39
  canvas[:] = 255
40
+ st.image(canvas, caption="Draw Here", use_column_width=True)
41
 
42
  if st.button("Save Notes"):
43
  save_notes(note)
44
 
45
+ def draw_line(canvas, start_point, end_point):
46
+ """
47
+ Draw a line on the canvas between two points.
48
+ """
49
+ draw = Image.fromarray(canvas)
50
+ draw.line([start_point, end_point], fill=(0, 0, 0), width=5)
51
+ return np.array(draw)
52
+
53
  def save_notes(note):
54
  with open("notes.txt", "w") as file:
55
  file.write(note)