forked from deepankarvarma/To-Do-List-Using-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
76 lines (67 loc) · 2.01 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import streamlit as st
import csv
# Define the path to the CSV file
CSV_FILE = "tasks.csv"
# Define the main function
def main():
# Set the title of the web app
st.title("To-Do List")
st.markdown(
f"""
<style>
.stApp {{
background-image: url("https://images.pexels.com/photos/2387793/pexels-photo-2387793.jpeg?cs=srgb&dl=pexels-adrien-olichon-2387793.jpg&fm=jpg");
background-attachment: fixed;
background-size: cover
}}
</style>
""",
unsafe_allow_html=True
)
# Load the tasks from the CSV file
task_list = load_tasks()
# Add a form to input new tasks
task_input = st.text_input("Add a new task:")
if st.button("Add"):
if task_input != "":
# Add the new task to the list and save it to the CSV file
task_list.append(task_input)
save_tasks(task_list)
task_input = ""
display(task_list)
# Add a button to clear the task list
if st.button("Clear all tasks"):
# Clear the task list and save the changes to the CSV file
task_list.clear()
save_tasks(task_list)
display(task_list)
def load_tasks():
"""
Load the tasks from the CSV file.
"""
try:
with open(CSV_FILE, "r") as f:
reader = csv.reader(f)
task_list = [row[0] for row in reader]
except FileNotFoundError:
task_list = []
return task_list
def display(task_list):
# Display the current tasks
if len(task_list) == 0:
st.write("No tasks added yet.")
else:
st.write("Current tasks:")
for i, task in enumerate(task_list):
st.write(f"{i+1}. {task}")
def save_tasks(task_list):
"""
Save the tasks to the CSV file.
"""
with open(CSV_FILE, "w", newline="") as f:
f.truncate(0)
writer = csv.writer(f)
writer.writerows([[task] for task in task_list])
# Run the app
if __name__ == "__main__":
main()