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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -111
app.py CHANGED
@@ -1,115 +1,58 @@
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
102
- if st.button("✏️ Edit", key=f"edit_{i}"):
103
- st.session_state.editing_index = i # Set the task index to be edited
104
- # Delete button
105
- if st.button("🗑️ Delete", key=f"delete_{i}"):
106
- delete_task(i)
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")
 
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")