Spaces:
Sleeping
Sleeping
flutterbasit
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
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 \n Notebook", page_icon="π", layout="centered")
|
@@ -45,9 +44,14 @@ def save_uploaded_file(uploaded_file):
|
|
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, image_files, audio_file, bg_color):
|
50 |
try:
|
|
|
|
|
|
|
|
|
|
|
51 |
new_task = {
|
52 |
"title": title,
|
53 |
"description": description,
|
@@ -60,20 +64,15 @@ def add_task(title, description, image_files, audio_file, bg_color):
|
|
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 |
try:
|
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:
|
@@ -95,24 +94,11 @@ def edit_task(index, new_title, new_description, new_images, new_audio, new_bg_c
|
|
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
|
|
|
1 |
import streamlit as st
|
2 |
from pathlib import Path
|
3 |
from datetime import datetime
|
|
|
4 |
|
5 |
# Application title
|
6 |
st.set_page_config(page_title="To-Do List \n Notebook", page_icon="π", layout="centered")
|
|
|
44 |
st.error(f"Error saving file: {e}")
|
45 |
return None
|
46 |
|
47 |
+
# Function to add a new task with exception handling
|
48 |
def add_task(title, description, image_files, audio_file, bg_color):
|
49 |
try:
|
50 |
+
if not title.strip():
|
51 |
+
raise ValueError("Title cannot be empty.")
|
52 |
+
if not description.strip():
|
53 |
+
raise ValueError("Description cannot be empty.")
|
54 |
+
|
55 |
new_task = {
|
56 |
"title": title,
|
57 |
"description": description,
|
|
|
64 |
|
65 |
# Clear the inputs after adding the task
|
66 |
clear_fields() # Call the function to clear fields
|
67 |
+
except ValueError as ve:
|
68 |
+
st.error(f"Error: {ve}")
|
69 |
except Exception as e:
|
70 |
st.error(f"Error adding task: {e}")
|
71 |
|
72 |
# Function to delete a task
|
73 |
def delete_task(index):
|
74 |
try:
|
75 |
+
st.session_state.tasks.pop(index)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
st.success("Task deleted successfully!")
|
77 |
st.session_state.editing_index = None # Reset editing index after deletion
|
78 |
except Exception as e:
|
|
|
94 |
|
95 |
# Function to clear the fields after a task is added or edited
|
96 |
def clear_fields():
|
|
|
97 |
st.session_state.title = ""
|
98 |
st.session_state.description = ""
|
99 |
st.session_state.bg_color = "#ffffff"
|
|
|
|
|
100 |
st.session_state.image_files = []
|
101 |
st.session_state.audio_file = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
st.session_state.editing_index = None # Reset editing index
|
103 |
|
104 |
# Add task section
|