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