flutterbasit commited on
Commit
1fef6d4
·
verified ·
1 Parent(s): b632c45

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +203 -55
app.py CHANGED
@@ -1,58 +1,206 @@
1
  import streamlit as st
2
- import time
3
-
4
- # Set the page config
5
- st.set_page_config(page_title="⏱️ Stopwatch", page_icon="⏱️", layout="centered")
6
-
7
- # App Title
8
- st.title("⏱️ Attractive Stopwatch")
9
- st.subheader("Start, Stop, and Reset the Stopwatch with Style!")
10
-
11
- # Stopwatch logic
12
- if "start_time" not in st.session_state:
13
- st.session_state.start_time = None # Store start time
14
- st.session_state.elapsed_time = 0 # Store elapsed time
15
- st.session_state.running = False # Store running state
16
-
17
- # Function to format the time
18
- def format_time(seconds):
19
- mins, secs = divmod(int(seconds), 60)
20
- return f"{mins:02}:{secs:02}"
21
-
22
- # Start the stopwatch
23
- if st.session_state.running:
24
- st.session_state.elapsed_time += time.time() - st.session_state.start_time
25
- st.session_state.start_time = time.time()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  else:
27
- st.session_state.start_time = time.time()
28
-
29
- # Display the stopwatch time
30
- display_time = format_time(st.session_state.elapsed_time)
31
- st.markdown(f"### Time: **{display_time}**", unsafe_allow_html=True)
32
-
33
- # Control buttons
34
- col1, col2, col3 = st.columns([1, 3, 1])
35
-
36
- with col1:
37
- stop_button = st.button("⏹️ Stop", use_container_width=True, disabled=not st.session_state.running)
38
- with col2:
39
- start_button = st.button("▶️ Start", use_container_width=True, disabled=st.session_state.running)
40
- with col3:
41
- reset_button = st.button("🔄 Reset", use_container_width=True)
42
-
43
- # Start/Stop logic
44
- if start_button:
45
- st.session_state.running = True
46
- st.session_state.start_time = time.time() # Reset the start time when starting
47
- elif stop_button:
48
- st.session_state.running = False
49
-
50
- # Reset logic
51
- if reset_button:
52
- st.session_state.elapsed_time = 0 # Reset the elapsed time
53
- st.session_state.running = False # Stop the stopwatch
54
- st.session_state.start_time = None # Clear the start time
55
-
56
- # Footer section
57
  st.write("---")
58
- st.write("✨ Created with ❤️ using Streamlit")
 
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")
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("📝 To-Do List \n Notebook")
14
+ st.subheader("Your tasks, your way!")
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 the session state
20
+ if "tasks" not in st.session_state:
21
+ st.session_state.tasks = []
22
+
23
+ # Initialize task input fields in the session state
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, image_files, audio_file, bg_color):
50
+ try:
51
+ new_task = {
52
+ "title": title,
53
+ "description": description,
54
+ "images": [save_uploaded_file(image) for image in image_files] if image_files else [],
55
+ "audio": save_uploaded_file(audio_file) if audio_file else None,
56
+ "bg_color": bg_color,
57
+ }
58
+ st.session_state.tasks.append(new_task)
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
+ try:
69
+ task = st.session_state.tasks.pop(index)
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, new_images, new_audio, new_bg_color):
84
+ try:
85
+ task = st.session_state.tasks[index]
86
+ task["title"] = new_title
87
+ task["description"] = new_description
88
+ task["images"] = [save_uploaded_file(img) for img in new_images] if new_images else task["images"]
89
+ task["audio"] = save_uploaded_file(new_audio) if new_audio else task["audio"]
90
+ task["bg_color"] = new_bg_color
91
+ st.success(f"Task '{new_title}' updated successfully!")
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.image_files, st.session_state.audio_file, st.session_state.bg_color)
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.image_files, st.session_state.audio_file, st.session_state.bg_color)
158
+
159
+ # Add "Clear" button to reset all input fields
160
+ if st.button("Clear"):
161
+ clear_fields()
162
+
163
+ # Show tasks
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: 10px; border-radius: 8px;'>",
171
+ unsafe_allow_html=True,
172
+ )
173
+
174
+ # Task content
175
+ col1, col2 = st.columns([6, 1])
176
+ with col1:
177
+ st.write(f"### TASK # {i + 1}")
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
192
+ if st.button("✏️ Edit", key=f"edit_{i}"):
193
+ st.session_state.editing_index = i # Set the task index to be edited
194
+ # Delete button
195
+ if st.button("🗑️ Delete", key=f"delete_{i}"):
196
+ delete_task(i)
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("✨ Made with ❤️ using Streamlit")