zyliu commited on
Commit
11e11c3
1 Parent(s): d1b785a

Create app.py

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