hw2 / app.py
zyliu's picture
Update app.py
d2545b9
# Import modules
import streamlit as st
import pymongo
from pymongo.server_api import ServerApi
import os
# client = pymongo.MongoClient("mongodb://localhost:27017/")
# db = client["todo_db"]
user_name = os.environ.get("user_name", None)
passwd = os.environ.get("passwd", None)
uri = f"mongodb+srv://{user_name}:{passwd}@cluster0.2wsoa5b.mongodb.net/?retryWrites=true&w=majority"
print(f"uri: {uri}")
# Create a new client and connect to the server
client = pymongo.MongoClient(uri, server_api=ServerApi('1'))
db = client["todo_db"]
collection = db["tasks"]
def create_task(task, category, priority):
task_dict = {"task": task, "category": category, "priority": priority}
collection.insert_one(task_dict)
# Display a success message
st.success("Task created successfully!")
# Define a function to edit a task
def edit_task(id, task, category, priority):
new_values = {"$set": {"task": task, "category": category, "priority": priority}}
collection.update_one({"_id": id}, new_values)
# Display a success message
st.success("Task edited successfully!")
# Define a function to delete a task
def delete_task(id):
collection.delete_one({"_id": id})
# Display a success message
st.success("Task deleted successfully!")
# Define a function to validate the user input
def validate_input(task, category, priority):
if task == "" or category == "" or priority == "":
# Display a warning message
st.warning("Please fill in all the fields before creating or editing a task.")
# Return False to indicate invalid input
return False
else:
# Return True to indicate valid input
return True
# Create a sidebar with navigation options
st.sidebar.title("To-do List App")
navigation = st.sidebar.radio("Navigation", ["Home", "Create", "Edit", "Delete"])
# Display the corresponding page based on the navigation option
if navigation == "Home":
st.title("To-do List App")
st.subheader("View your tasks")
tasks = collection.find().sort("priority", -1)
# Display the tasks in a table
st.table(tasks)
elif navigation == "Create":
st.title("To-do List App")
st.subheader("Create a new task")
task = st.text_input("Task")
category = st.text_input("Category")
priority = st.number_input("Priority (1-5)", min_value=1, max_value=5)
if st.button("Create"):
# Validate the user input
if validate_input(task, category, priority):
# Create the task
create_task(task, category, priority)
elif navigation == "Edit":
st.title("To-do List App")
st.subheader("Edit an existing task")
tasks = collection.find()
task_list = [(task["_id"], task["task"]) for task in tasks]
task_id, task_name = st.selectbox("Select a task to edit", task_list)
new_task = st.text_input("Task", value=task_name)
new_category = st.text_input("Category")
new_priority = st.number_input("Priority (1-5)", min_value=1, max_value=5)
if st.button("Edit"):
# Validate the user input
if validate_input(new_task, new_category, new_priority):
# Edit the task
edit_task(task_id, new_task, new_category, new_priority)
elif navigation == "Delete":
st.title("To-do List App")
st.subheader("Delete a task")
tasks = collection.find()
task_list = [(task["_id"], task["task"]) for task in tasks]
task_id, task_name = st.selectbox("Select a task to delete", task_list)
if st.button("Delete"):
# Delete the task
delete_task(task_id)