zyliu commited on
Commit
8fec417
1 Parent(s): e44ddda

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pymongo
3
+
4
+ # Connect to MongoDB database
5
+ client = pymongo.MongoClient("mongodb://localhost:27017/")
6
+ db = client["todo_db"]
7
+ collection = db["tasks"]
8
+
9
+ # Define a function to create a task
10
+ def create_task(task, category, priority):
11
+ # Create a dictionary with the task information
12
+ task_dict = {"task": task, "category": category, "priority": priority}
13
+ # Insert the task into the database
14
+ collection.insert_one(task_dict)
15
+ # Display a success message
16
+ st.success("Task created successfully!")
17
+
18
+ # Define a function to edit a task
19
+ def edit_task(id, task, category, priority):
20
+ # Create a dictionary with the updated task information
21
+ new_values = {"$set": {"task": task, "category": category, "priority": priority}}
22
+ # Update the task in the database using the id
23
+ collection.update_one({"_id": id}, new_values)
24
+ # Display a success message
25
+ st.success("Task edited successfully!")
26
+
27
+ # Define a function to delete a task
28
+ def delete_task(id):
29
+ # Delete the task from the database using the id
30
+ collection.delete_one({"_id": id})
31
+ # Display a success message
32
+ st.success("Task deleted successfully!")
33
+
34
+ # Define a function to validate the user input
35
+ def validate_input(task, category, priority):
36
+ # Check if any field is empty
37
+ if task == "" or category == "" or priority == "":
38
+ # Display a warning message
39
+ st.warning("Please fill in all the fields before creating or editing a task.")
40
+ # Return False to indicate invalid input
41
+ return False
42
+ else:
43
+ # Return True to indicate valid input
44
+ return True
45
+
46
+ # Create a sidebar with navigation options
47
+ st.sidebar.title("To-do List App")
48
+ navigation = st.sidebar.radio("Navigation", ["Home", "Create", "Edit", "Delete"])
49
+
50
+ # Display the corresponding page based on the navigation option
51
+ if navigation == "Home":
52
+ # Display the title and subtitle
53
+ st.title("To-do List App")
54
+ st.subheader("View your tasks")
55
+ # Fetch all the tasks from the database and sort them by priority
56
+ tasks = collection.find().sort("priority", -1)
57
+ # Display the tasks in a table
58
+ st.table(tasks)
59
+ elif navigation == "Create":
60
+ # Display the title and subtitle
61
+ st.title("To-do List App")
62
+ st.subheader("Create a new task")
63
+ # Create input fields for the task information
64
+ task = st.text_input("Task")
65
+ category = st.text_input("Category")
66
+ priority = st.number_input("Priority (1-5)", min_value=1, max_value=5)
67
+ # Create a button to create the task
68
+ if st.button("Create"):
69
+ # Validate the user input
70
+ if validate_input(task, category, priority):
71
+ # Create the task
72
+ create_task(task, category, priority)
73
+ elif navigation == "Edit":
74
+ # Display the title and subtitle
75
+ st.title("To-do List App")
76
+ st.subheader("Edit an existing task")
77
+ # Create a selectbox to choose a task to edit
78
+ tasks = collection.find()
79
+ task_list = [(task["_id"], task["task"]) for task in tasks]
80
+ task_id, task_name = st.selectbox("Select a task to edit", task_list)
81
+ # Create input fields for the new task information
82
+ new_task = st.text_input("Task", value=task_name)
83
+ new_category = st.text_input("Category")
84
+ new_priority = st.number_input("Priority (1-5)", min_value=1, max_value=5)
85
+ # Create a button to edit the task
86
+ if st.button("Edit"):
87
+ # Validate the user input
88
+ if validate_input(new_task, new_category, new_priority):
89
+ # Edit the task
90
+ edit_task(task_id, new_task, new_category, new_priority)
91
+ elif navigation == "Delete":
92
+ # Display the title and subtitle
93
+ st.title("To-do List App")
94
+ st.subheader("Delete a task")
95
+ # Create a selectbox to choose a task to delete
96
+ tasks = collection.find()
97
+ task_list = [(task["_id"], task["task"]) for task in tasks]
98
+ task_id, task_name = st.selectbox("Select a task to delete", task_list)
99
+ # Create a button to delete the task
100
+ if st.button("Delete"):
101
+ # Delete the task
102
+ delete_task(task_id)