|
|
|
import streamlit as st |
|
import pymongo |
|
from pymongo.server_api import ServerApi |
|
import os |
|
|
|
|
|
|
|
|
|
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}") |
|
|
|
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) |
|
|
|
st.success("Task created successfully!") |
|
|
|
|
|
def edit_task(id, task, category, priority): |
|
new_values = {"$set": {"task": task, "category": category, "priority": priority}} |
|
collection.update_one({"_id": id}, new_values) |
|
|
|
st.success("Task edited successfully!") |
|
|
|
|
|
def delete_task(id): |
|
collection.delete_one({"_id": id}) |
|
|
|
st.success("Task deleted successfully!") |
|
|
|
|
|
def validate_input(task, category, priority): |
|
if task == "" or category == "" or priority == "": |
|
|
|
st.warning("Please fill in all the fields before creating or editing a task.") |
|
|
|
return False |
|
else: |
|
|
|
return True |
|
|
|
|
|
st.sidebar.title("To-do List App") |
|
navigation = st.sidebar.radio("Navigation", ["Home", "Create", "Edit", "Delete"]) |
|
|
|
|
|
if navigation == "Home": |
|
st.title("To-do List App") |
|
st.subheader("View your tasks") |
|
tasks = collection.find().sort("priority", -1) |
|
|
|
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"): |
|
|
|
if validate_input(task, category, priority): |
|
|
|
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"): |
|
|
|
if validate_input(new_task, new_category, new_priority): |
|
|
|
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_task(task_id) |
|
|
|
|