Update app.py
Browse files
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 |
-
#
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
nonlocal mouse_clicked, last_position
|
| 27 |
if event == "mousedown":
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
elif event == "mousemove" and
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
elif event == "mouseup":
|
| 35 |
-
|
| 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)
|