Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,191 +1,101 @@
|
|
1 |
import streamlit as st
|
2 |
-
from pathlib import Path
|
3 |
from datetime import datetime
|
4 |
-
import os
|
5 |
|
6 |
# Application title
|
7 |
-
st.set_page_config(page_title="To-Do List
|
8 |
-
|
9 |
-
# Create an uploads folder if it doesn't exist
|
10 |
-
Path("uploads").mkdir(exist_ok=True)
|
11 |
|
12 |
# App header
|
13 |
-
st.title("π
|
14 |
-
st.subheader("
|
15 |
-
|
16 |
-
# App description
|
17 |
-
st.write("Organize your tasks with notes, images, and audio. Edit, save, or delete tasks seamlessly!")
|
18 |
|
19 |
-
# Initialize task list in
|
20 |
if "tasks" not in st.session_state:
|
21 |
st.session_state.tasks = []
|
22 |
|
23 |
-
# Initialize task input fields in
|
24 |
if "title" not in st.session_state:
|
25 |
st.session_state.title = ""
|
26 |
if "description" not in st.session_state:
|
27 |
st.session_state.description = ""
|
28 |
if "bg_color" not in st.session_state:
|
29 |
st.session_state.bg_color = "#ffffff"
|
30 |
-
if "image_files" not in st.session_state:
|
31 |
-
st.session_state.image_files = []
|
32 |
-
if "audio_file" not in st.session_state:
|
33 |
-
st.session_state.audio_file = None
|
34 |
if "editing_index" not in st.session_state:
|
35 |
st.session_state.editing_index = None # To track which task is being edited
|
36 |
|
37 |
-
# Helper function to save uploaded files
|
38 |
-
def save_uploaded_file(uploaded_file):
|
39 |
-
try:
|
40 |
-
file_path = Path("uploads") / uploaded_file.name
|
41 |
-
with open(file_path, "wb") as f:
|
42 |
-
f.write(uploaded_file.getbuffer())
|
43 |
-
return str(file_path)
|
44 |
-
except Exception as e:
|
45 |
-
st.error(f"Error saving file: {e}")
|
46 |
-
return None
|
47 |
-
|
48 |
# Function to add a new task
|
49 |
-
def add_task(title, description,
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
st.success(f"Task '{title}' added successfully!")
|
60 |
-
|
61 |
-
# Clear the inputs after adding the task
|
62 |
-
clear_fields() # Call the function to clear fields
|
63 |
-
except Exception as e:
|
64 |
-
st.error(f"Error adding task: {e}")
|
65 |
|
66 |
# Function to delete a task
|
67 |
def delete_task(index):
|
68 |
-
|
69 |
-
|
70 |
-
# Delete associated files from the 'uploads' folder
|
71 |
-
for file_path in task["images"]:
|
72 |
-
if os.path.exists(file_path):
|
73 |
-
os.remove(file_path)
|
74 |
-
if task["audio"] and os.path.exists(task["audio"]):
|
75 |
-
os.remove(task["audio"])
|
76 |
-
|
77 |
-
st.success("Task deleted successfully!")
|
78 |
-
st.session_state.editing_index = None # Reset editing index after deletion
|
79 |
-
except Exception as e:
|
80 |
-
st.error(f"Error deleting task: {e}")
|
81 |
|
82 |
# Function to edit a task
|
83 |
-
def edit_task(index, new_title, new_description,
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
st.session_state.editing_index = None # Reset editing index after task is edited
|
93 |
-
except Exception as e:
|
94 |
-
st.error(f"Error editing task: {e}")
|
95 |
-
|
96 |
-
# Function to clear the fields after a task is added or edited
|
97 |
def clear_fields():
|
98 |
-
# Clear text inputs
|
99 |
st.session_state.title = ""
|
100 |
st.session_state.description = ""
|
101 |
st.session_state.bg_color = "#ffffff"
|
102 |
-
|
103 |
-
# Clear file inputs from session state
|
104 |
-
st.session_state.image_files = []
|
105 |
-
st.session_state.audio_file = None
|
106 |
-
|
107 |
-
# Delete the uploaded files from the 'uploads' directory
|
108 |
-
for file_path in st.session_state.image_files:
|
109 |
-
if os.path.exists(file_path):
|
110 |
-
os.remove(file_path)
|
111 |
-
|
112 |
-
if st.session_state.audio_file and os.path.exists(st.session_state.audio_file):
|
113 |
-
os.remove(st.session_state.audio_file)
|
114 |
-
|
115 |
-
# Reset editing index
|
116 |
st.session_state.editing_index = None # Reset editing index
|
117 |
|
118 |
-
# Add task section
|
119 |
with st.expander("β Add a New Task"):
|
120 |
if st.session_state.editing_index is not None:
|
121 |
-
# If editing, populate fields with the existing task data
|
122 |
task_to_edit = st.session_state.tasks[st.session_state.editing_index]
|
123 |
st.session_state.title = st.text_input("Title", value=task_to_edit["title"])
|
124 |
st.session_state.description = st.text_area("Description", value=task_to_edit["description"])
|
125 |
-
|
126 |
-
# Multiple images upload (without assigning directly to session state)
|
127 |
-
image_files = st.file_uploader("Upload Images (optional)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
128 |
-
if image_files:
|
129 |
-
st.session_state.image_files = image_files
|
130 |
-
|
131 |
-
audio_file = st.file_uploader("Upload Audio (optional)", type=["mp3", "wav"], accept_multiple_files=False)
|
132 |
-
if audio_file:
|
133 |
-
st.session_state.audio_file = audio_file
|
134 |
-
|
135 |
st.session_state.bg_color = st.color_picker("Pick Background Color", value=task_to_edit["bg_color"])
|
136 |
|
137 |
if st.button("Save Changes"):
|
138 |
-
edit_task(st.session_state.editing_index, st.session_state.title, st.session_state.description, st.session_state.
|
139 |
st.button("Cancel Edit", on_click=clear_fields)
|
140 |
else:
|
141 |
-
# If not editing, show empty input fields
|
142 |
st.session_state.title = st.text_input("Title", placeholder="Enter task title...", value=st.session_state.title)
|
143 |
st.session_state.description = st.text_area("Description", placeholder="Write your task details here...", value=st.session_state.description)
|
144 |
-
|
145 |
-
# Multiple images upload (without assigning directly to session state)
|
146 |
-
image_files = st.file_uploader("Upload Images (optional)", type=["png", "jpg", "jpeg"], accept_multiple_files=True)
|
147 |
-
if image_files:
|
148 |
-
st.session_state.image_files = image_files
|
149 |
-
|
150 |
-
audio_file = st.file_uploader("Upload Audio (optional)", type=["mp3", "wav"], accept_multiple_files=False)
|
151 |
-
if audio_file:
|
152 |
-
st.session_state.audio_file = audio_file
|
153 |
-
|
154 |
st.session_state.bg_color = st.color_picker("Pick Background Color", value=st.session_state.bg_color)
|
155 |
|
156 |
if st.button("Add Task"):
|
157 |
-
add_task(st.session_state.title, st.session_state.description, st.session_state.
|
158 |
|
159 |
-
#
|
160 |
if st.button("Clear"):
|
161 |
clear_fields()
|
162 |
|
163 |
-
#
|
164 |
if st.session_state.tasks:
|
165 |
st.subheader("π Your Tasks")
|
166 |
for i, task in enumerate(st.session_state.tasks):
|
167 |
with st.container():
|
168 |
-
# Apply background color
|
169 |
st.markdown(
|
170 |
-
f"<div style='background-color: {task['bg_color']}; padding:
|
171 |
unsafe_allow_html=True,
|
172 |
)
|
173 |
|
174 |
# Task content
|
175 |
col1, col2 = st.columns([6, 1])
|
176 |
with col1:
|
177 |
-
st.write(f"###
|
178 |
st.write(f"**Title:** {task['title']}")
|
179 |
st.write(f"**Description:** {task['description']}")
|
180 |
-
|
181 |
-
# Display images if available
|
182 |
-
if task["images"]:
|
183 |
-
for img in task["images"]:
|
184 |
-
st.image(img, caption="Attached Image", use_column_width=True)
|
185 |
-
|
186 |
-
# Display audio player if available
|
187 |
-
if task["audio"]:
|
188 |
-
st.audio(task["audio"], format="audio/mp3")
|
189 |
|
190 |
with col2:
|
191 |
# Edit button
|
@@ -197,10 +107,9 @@ if st.session_state.tasks:
|
|
197 |
|
198 |
# Close background div
|
199 |
st.markdown("</div>", unsafe_allow_html=True)
|
200 |
-
|
201 |
else:
|
202 |
st.info("You have no tasks. Add a new task to get started!")
|
203 |
|
204 |
# Footer
|
205 |
st.write("---")
|
206 |
-
st.write("β¨
|
|
|
1 |
import streamlit as st
|
|
|
2 |
from datetime import datetime
|
|
|
3 |
|
4 |
# Application title
|
5 |
+
st.set_page_config(page_title="To-Do List", page_icon="π", layout="centered")
|
|
|
|
|
|
|
6 |
|
7 |
# App header
|
8 |
+
st.title("π To-Do List")
|
9 |
+
st.subheader("Manage your tasks easily!")
|
|
|
|
|
|
|
10 |
|
11 |
+
# Initialize task list in session state
|
12 |
if "tasks" not in st.session_state:
|
13 |
st.session_state.tasks = []
|
14 |
|
15 |
+
# Initialize task input fields in session state
|
16 |
if "title" not in st.session_state:
|
17 |
st.session_state.title = ""
|
18 |
if "description" not in st.session_state:
|
19 |
st.session_state.description = ""
|
20 |
if "bg_color" not in st.session_state:
|
21 |
st.session_state.bg_color = "#ffffff"
|
|
|
|
|
|
|
|
|
22 |
if "editing_index" not in st.session_state:
|
23 |
st.session_state.editing_index = None # To track which task is being edited
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
# Function to add a new task
|
26 |
+
def add_task(title, description, bg_color):
|
27 |
+
new_task = {
|
28 |
+
"title": title,
|
29 |
+
"description": description,
|
30 |
+
"bg_color": bg_color,
|
31 |
+
"timestamp": datetime.now(),
|
32 |
+
}
|
33 |
+
st.session_state.tasks.append(new_task)
|
34 |
+
st.success(f"Task '{title}' added successfully!")
|
35 |
+
clear_fields() # Clear fields after adding task
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
# Function to delete a task
|
38 |
def delete_task(index):
|
39 |
+
st.session_state.tasks.pop(index)
|
40 |
+
st.success("Task deleted successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
# Function to edit a task
|
43 |
+
def edit_task(index, new_title, new_description, new_bg_color):
|
44 |
+
task = st.session_state.tasks[index]
|
45 |
+
task["title"] = new_title
|
46 |
+
task["description"] = new_description
|
47 |
+
task["bg_color"] = new_bg_color
|
48 |
+
st.success(f"Task '{new_title}' updated successfully!")
|
49 |
+
st.session_state.editing_index = None # Reset editing index after editing
|
50 |
+
|
51 |
+
# Function to clear the input fields
|
|
|
|
|
|
|
|
|
|
|
52 |
def clear_fields():
|
|
|
53 |
st.session_state.title = ""
|
54 |
st.session_state.description = ""
|
55 |
st.session_state.bg_color = "#ffffff"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
st.session_state.editing_index = None # Reset editing index
|
57 |
|
58 |
+
# Add/Edit task section
|
59 |
with st.expander("β Add a New Task"):
|
60 |
if st.session_state.editing_index is not None:
|
|
|
61 |
task_to_edit = st.session_state.tasks[st.session_state.editing_index]
|
62 |
st.session_state.title = st.text_input("Title", value=task_to_edit["title"])
|
63 |
st.session_state.description = st.text_area("Description", value=task_to_edit["description"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
st.session_state.bg_color = st.color_picker("Pick Background Color", value=task_to_edit["bg_color"])
|
65 |
|
66 |
if st.button("Save Changes"):
|
67 |
+
edit_task(st.session_state.editing_index, st.session_state.title, st.session_state.description, st.session_state.bg_color)
|
68 |
st.button("Cancel Edit", on_click=clear_fields)
|
69 |
else:
|
|
|
70 |
st.session_state.title = st.text_input("Title", placeholder="Enter task title...", value=st.session_state.title)
|
71 |
st.session_state.description = st.text_area("Description", placeholder="Write your task details here...", value=st.session_state.description)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
st.session_state.bg_color = st.color_picker("Pick Background Color", value=st.session_state.bg_color)
|
73 |
|
74 |
if st.button("Add Task"):
|
75 |
+
add_task(st.session_state.title, st.session_state.description, st.session_state.bg_color)
|
76 |
|
77 |
+
# Clear button to reset all fields
|
78 |
if st.button("Clear"):
|
79 |
clear_fields()
|
80 |
|
81 |
+
# Display the list of tasks
|
82 |
if st.session_state.tasks:
|
83 |
st.subheader("π Your Tasks")
|
84 |
for i, task in enumerate(st.session_state.tasks):
|
85 |
with st.container():
|
86 |
+
# Apply background color to task
|
87 |
st.markdown(
|
88 |
+
f"<div style='background-color: {task['bg_color']}; padding: 15px; border-radius: 10px; margin-bottom: 10px;'>",
|
89 |
unsafe_allow_html=True,
|
90 |
)
|
91 |
|
92 |
# Task content
|
93 |
col1, col2 = st.columns([6, 1])
|
94 |
with col1:
|
95 |
+
st.write(f"### Task #{i + 1}")
|
96 |
st.write(f"**Title:** {task['title']}")
|
97 |
st.write(f"**Description:** {task['description']}")
|
98 |
+
st.write(f"**Created At:** {task['timestamp'].strftime('%Y-%m-%d %H:%M:%S')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
with col2:
|
101 |
# Edit button
|
|
|
107 |
|
108 |
# Close background div
|
109 |
st.markdown("</div>", unsafe_allow_html=True)
|
|
|
110 |
else:
|
111 |
st.info("You have no tasks. Add a new task to get started!")
|
112 |
|
113 |
# Footer
|
114 |
st.write("---")
|
115 |
+
st.write("β¨ Created with β€οΈ using Streamlit")
|