File size: 4,051 Bytes
8fec417
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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)