Spaces:
Sleeping
Sleeping
File size: 5,562 Bytes
77bb6bb |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
from datetime import datetime
import streamlit as st
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
# Function to connect to MongoDB
def connect_to_mongodb():
uri = "mongodb+srv://ricky:ykcir@cluster0.ifzrzzd.mongodb.net/?retryWrites=true&w=majority"
client = MongoClient(uri, server_api=ServerApi("1"))
db = client["todo"]
collection = db["tasks"]
return collection
# Function to validate task fields
def validate_fields(task, category, priority, deadline):
if not task or not category or not priority or not deadline:
st.warning("Please fill in all fields.")
return False
return True
# Home page
def home(collection):
# Display tasks sorted by priority
tasks = collection.find().sort("priority", -1)
for task in tasks:
st.write(f"Task ID: {task['_id']}")
st.write(f"Task: {task['task']}")
st.write(f"Category: {task['category']}")
st.write(f"Priority: {task['priority']}")
st.write(f"Deadline: {task['deadline'].strftime('%Y-%m-%d')}")
st.write("---")
# Create page
def create(collection):
# Get input from user
task = st.text_input("Task", key="create_task")
category = st.text_input("Category", key="create_category")
priority = st.number_input("Priority", min_value=1, max_value=5, key="create_priority")
deadline = st.date_input("Deadline", key="create_deadline")
if st.button("Create", key="create_button"):
if validate_fields(task, category, priority, deadline):
# Check if task already exists
if collection.find_one({"task": task}):
st.warning("Task already exists.")
else:
# Convert deadline to datetime object
deadline_datetime = datetime.combine(deadline, datetime.min.time())
# Insert task into database
new_task = {"task": task, "category": category, "priority": priority, "deadline": deadline_datetime}
collection.insert_one(new_task)
st.success("Task created successfully.")
# Edit page
def edit(collection):
# Get task titles for dropdown selection
tasks = collection.find()
task_titles = [task["task"] for task in tasks]
# Select task to edit
selected_task = st.selectbox("Select Task", [""] + task_titles, key="edit_select_task")
if selected_task:
# Get selected task
task = collection.find_one({"task": selected_task})
if task:
# Display task details
st.write(f"Task: {task['task']}")
st.write(f"Category: {task['category']}")
st.write(f"Priority: {task['priority']}")
st.write(f"Deadline: {task['deadline'].strftime('%Y-%m-%d')}")
# Get input from user
new_task = st.text_input("Task", task["task"], key="edit_task")
new_category = st.text_input("Category", task["category"], key="edit_category")
new_priority = st.number_input("Priority", min_value=1, max_value=5, value=task["priority"], key="edit_priority")
new_deadline = st.date_input("Deadline", task["deadline"].date(), key="edit_deadline")
if st.button("Save", key="edit_button"):
if validate_fields(new_task, new_category, new_priority, new_deadline):
# Convert new deadline to datetime object
new_deadline_datetime = datetime.combine(new_deadline, datetime.min.time())
# Update task in database
updated_task = {"$set": {"task": new_task, "category": new_category, "priority": new_priority, "deadline": new_deadline_datetime}}
collection.update_one({"task": selected_task}, updated_task)
st.success("Task updated successfully.")
# Delete page
def delete(collection):
# Get task titles for dropdown selection
tasks = collection.find()
task_titles = [task["task"] for task in tasks]
# Select task to delete
selected_task = st.selectbox("Select Task", [""] + task_titles, key="delete_select_task")
if selected_task:
# Get selected task
task = collection.find_one({"task": selected_task})
if task:
# Display task details
st.write(f"Task: {task['task']}")
st.write(f"Category: {task['category']}")
st.write(f"Priority: {task['priority']}")
st.write(f"Deadline: {task['deadline'].strftime('%Y-%m-%d')}")
if st.button("Delete", key="delete_button"):
# Delete task from database
result = collection.delete_one({"task": selected_task})
if result.deleted_count > 0:
st.success("Task deleted successfully.")
else:
st.warning("Task not found.")
# Main function
def main():
st.set_page_config(layout="wide")
st.title("My List")
# Connect to MongoDB
collection = connect_to_mongodb()
# Clear all tasks
if st.button("Clear All"):
collection.delete_many({})
st.success("All tasks cleared successfully.")
# Refresh page
st.experimental_rerun()
# Create tabs
tabs = ["Home", "Create", "Edit", "Delete"]
tabs = st.tabs(tabs)
with tabs[0]:
home(collection)
with tabs[1]:
create(collection)
with tabs[2]:
edit(collection)
with tabs[3]:
delete(collection)
# Run the app
if __name__ == "__main__":
main()
|