File size: 3,553 Bytes
11e11c3
 
 
b172f75
1090351
11e11c3
 
 
 
58e87e3
 
 
ad29e91
11e11c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b345d93
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
# 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"]

uri = os.environ.get("user_name", None)
uri = 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)