Spaces:
Sleeping
Sleeping
Ricky596474230
commited on
Commit
•
77bb6bb
1
Parent(s):
28217de
Upload 2 files
Browse files- app.py +142 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from pymongo.mongo_client import MongoClient
|
5 |
+
from pymongo.server_api import ServerApi
|
6 |
+
|
7 |
+
|
8 |
+
# Function to connect to MongoDB
|
9 |
+
def connect_to_mongodb():
|
10 |
+
uri = "mongodb+srv://ricky:ykcir@cluster0.ifzrzzd.mongodb.net/?retryWrites=true&w=majority"
|
11 |
+
client = MongoClient(uri, server_api=ServerApi("1"))
|
12 |
+
db = client["todo"]
|
13 |
+
collection = db["tasks"]
|
14 |
+
return collection
|
15 |
+
|
16 |
+
|
17 |
+
# Function to validate task fields
|
18 |
+
def validate_fields(task, category, priority, deadline):
|
19 |
+
if not task or not category or not priority or not deadline:
|
20 |
+
st.warning("Please fill in all fields.")
|
21 |
+
return False
|
22 |
+
return True
|
23 |
+
|
24 |
+
|
25 |
+
# Home page
|
26 |
+
def home(collection):
|
27 |
+
# Display tasks sorted by priority
|
28 |
+
tasks = collection.find().sort("priority", -1)
|
29 |
+
for task in tasks:
|
30 |
+
st.write(f"Task ID: {task['_id']}")
|
31 |
+
st.write(f"Task: {task['task']}")
|
32 |
+
st.write(f"Category: {task['category']}")
|
33 |
+
st.write(f"Priority: {task['priority']}")
|
34 |
+
st.write(f"Deadline: {task['deadline'].strftime('%Y-%m-%d')}")
|
35 |
+
st.write("---")
|
36 |
+
|
37 |
+
|
38 |
+
# Create page
|
39 |
+
def create(collection):
|
40 |
+
# Get input from user
|
41 |
+
task = st.text_input("Task", key="create_task")
|
42 |
+
category = st.text_input("Category", key="create_category")
|
43 |
+
priority = st.number_input("Priority", min_value=1, max_value=5, key="create_priority")
|
44 |
+
deadline = st.date_input("Deadline", key="create_deadline")
|
45 |
+
if st.button("Create", key="create_button"):
|
46 |
+
if validate_fields(task, category, priority, deadline):
|
47 |
+
# Check if task already exists
|
48 |
+
if collection.find_one({"task": task}):
|
49 |
+
st.warning("Task already exists.")
|
50 |
+
else:
|
51 |
+
# Convert deadline to datetime object
|
52 |
+
deadline_datetime = datetime.combine(deadline, datetime.min.time())
|
53 |
+
# Insert task into database
|
54 |
+
new_task = {"task": task, "category": category, "priority": priority, "deadline": deadline_datetime}
|
55 |
+
collection.insert_one(new_task)
|
56 |
+
st.success("Task created successfully.")
|
57 |
+
|
58 |
+
|
59 |
+
# Edit page
|
60 |
+
def edit(collection):
|
61 |
+
# Get task titles for dropdown selection
|
62 |
+
tasks = collection.find()
|
63 |
+
task_titles = [task["task"] for task in tasks]
|
64 |
+
# Select task to edit
|
65 |
+
selected_task = st.selectbox("Select Task", [""] + task_titles, key="edit_select_task")
|
66 |
+
if selected_task:
|
67 |
+
# Get selected task
|
68 |
+
task = collection.find_one({"task": selected_task})
|
69 |
+
if task:
|
70 |
+
# Display task details
|
71 |
+
st.write(f"Task: {task['task']}")
|
72 |
+
st.write(f"Category: {task['category']}")
|
73 |
+
st.write(f"Priority: {task['priority']}")
|
74 |
+
st.write(f"Deadline: {task['deadline'].strftime('%Y-%m-%d')}")
|
75 |
+
# Get input from user
|
76 |
+
new_task = st.text_input("Task", task["task"], key="edit_task")
|
77 |
+
new_category = st.text_input("Category", task["category"], key="edit_category")
|
78 |
+
new_priority = st.number_input("Priority", min_value=1, max_value=5, value=task["priority"], key="edit_priority")
|
79 |
+
new_deadline = st.date_input("Deadline", task["deadline"].date(), key="edit_deadline")
|
80 |
+
if st.button("Save", key="edit_button"):
|
81 |
+
if validate_fields(new_task, new_category, new_priority, new_deadline):
|
82 |
+
# Convert new deadline to datetime object
|
83 |
+
new_deadline_datetime = datetime.combine(new_deadline, datetime.min.time())
|
84 |
+
# Update task in database
|
85 |
+
updated_task = {"$set": {"task": new_task, "category": new_category, "priority": new_priority, "deadline": new_deadline_datetime}}
|
86 |
+
collection.update_one({"task": selected_task}, updated_task)
|
87 |
+
st.success("Task updated successfully.")
|
88 |
+
|
89 |
+
|
90 |
+
# Delete page
|
91 |
+
def delete(collection):
|
92 |
+
# Get task titles for dropdown selection
|
93 |
+
tasks = collection.find()
|
94 |
+
task_titles = [task["task"] for task in tasks]
|
95 |
+
# Select task to delete
|
96 |
+
selected_task = st.selectbox("Select Task", [""] + task_titles, key="delete_select_task")
|
97 |
+
if selected_task:
|
98 |
+
# Get selected task
|
99 |
+
task = collection.find_one({"task": selected_task})
|
100 |
+
if task:
|
101 |
+
# Display task details
|
102 |
+
st.write(f"Task: {task['task']}")
|
103 |
+
st.write(f"Category: {task['category']}")
|
104 |
+
st.write(f"Priority: {task['priority']}")
|
105 |
+
st.write(f"Deadline: {task['deadline'].strftime('%Y-%m-%d')}")
|
106 |
+
if st.button("Delete", key="delete_button"):
|
107 |
+
# Delete task from database
|
108 |
+
result = collection.delete_one({"task": selected_task})
|
109 |
+
if result.deleted_count > 0:
|
110 |
+
st.success("Task deleted successfully.")
|
111 |
+
else:
|
112 |
+
st.warning("Task not found.")
|
113 |
+
|
114 |
+
|
115 |
+
# Main function
|
116 |
+
def main():
|
117 |
+
st.set_page_config(layout="wide")
|
118 |
+
st.title("My List")
|
119 |
+
# Connect to MongoDB
|
120 |
+
collection = connect_to_mongodb()
|
121 |
+
# Clear all tasks
|
122 |
+
if st.button("Clear All"):
|
123 |
+
collection.delete_many({})
|
124 |
+
st.success("All tasks cleared successfully.")
|
125 |
+
# Refresh page
|
126 |
+
st.experimental_rerun()
|
127 |
+
# Create tabs
|
128 |
+
tabs = ["Home", "Create", "Edit", "Delete"]
|
129 |
+
tabs = st.tabs(tabs)
|
130 |
+
with tabs[0]:
|
131 |
+
home(collection)
|
132 |
+
with tabs[1]:
|
133 |
+
create(collection)
|
134 |
+
with tabs[2]:
|
135 |
+
edit(collection)
|
136 |
+
with tabs[3]:
|
137 |
+
delete(collection)
|
138 |
+
|
139 |
+
|
140 |
+
# Run the app
|
141 |
+
if __name__ == "__main__":
|
142 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit==1.28.2
|
2 |
+
pymongo==4.6.0
|