import streamlit as st import pymongo # Connect to MongoDB database client = pymongo.MongoClient("mongodb://localhost:27017/") db = client["todo_db"] collection = db["tasks"] # Define a function to create a task def create_task(task, category, priority): # Create a dictionary with the task information task_dict = {"task": task, "category": category, "priority": priority} # Insert the task into the database 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): # Create a dictionary with the updated task information new_values = {"$set": {"task": task, "category": category, "priority": priority}} # Update the task in the database using the id 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): # Delete the task from the database using the 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): # Check if any field is empty 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": # Display the title and subtitle st.title("To-do List App") st.subheader("View your tasks") # Fetch all the tasks from the database and sort them by priority tasks = collection.find().sort("priority", -1) # Display the tasks in a table st.table(tasks) elif navigation == "Create": # Display the title and subtitle st.title("To-do List App") st.subheader("Create a new task") # Create input fields for the task information task = st.text_input("Task") category = st.text_input("Category") priority = st.number_input("Priority (1-5)", min_value=1, max_value=5) # Create a button to create the task 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": # Display the title and subtitle st.title("To-do List App") st.subheader("Edit an existing task") # Create a selectbox to choose a task to edit 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) # Create input fields for the new task information 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) # Create a button to edit the task 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": # Display the title and subtitle st.title("To-do List App") st.subheader("Delete a task") # Create a selectbox to choose a task to delete 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) # Create a button to delete the task if st.button("Delete"): # Delete the task delete_task(task_id)