hkust / app1.py
YAYAQIU's picture
Update app1.py
7cb351e
import streamlit as st
import pymongo
############################## MongoDB Config ##############################
# Create a new client and connect to the server
uri = "mongodb+srv://yaqiu8025:[email protected]/?retryWrites=true&w=majority"" # Put your connection string into it
client = pymongo.MongoClient(uri)
db = client[
'todo'] # Connect to a Database (MongoDB will create the database if it does not exist, and make a connection to it.)
collection = db[
'tasks'] # Connect to a collection (MongoDB will create the collection if it does not exist, and make a connection to it.)
############################## App Setting ##############################
categories = ['home', 'work', 'school']
priority_range = range(1, 6)
############################## UI ##############################
# Add a selectbox to select actions:
page = st.sidebar.selectbox('Menu', ['View All', 'Create Task', 'Edit Task', 'Delete Task'])
# Page - View All
if page == 'View All':
st.write('View All Tasks')
tasks = collection.find({}, {"_id": 0}).sort("priority", pymongo.DESCENDING)
st.table(tasks)
# Page - Create Task
elif page == 'Create Task':
st.write('Create Task')
input_task = st.text_input('Enter Task')
input_category = st.selectbox('Select Category', categories)
input_priority = st.slider('Select Priority', min_value=1, max_value=5, value=1)
if st.button('Add Task'):
if not input_task or not input_category:
st.warning('Warning: Please fill in all fields!')
else:
collection.insert_one({'task': input_task, 'category': input_category, 'priority': input_priority})
st.success('Successfully Added')
# Page - Edit Task
elif page == 'Edit Task':
st.write('Edit Task')
tasks = [task['task'] for task in collection.find({}, {"_id": 0})]
task_to_edit = st.selectbox('Select Task to Edit', tasks)
task_data = collection.find_one({'task': task_to_edit})
if task_data:
st.write('Task:', task_data['task'])
st.write('Category:', task_data['category'])
st.write('Priority:', task_data['priority'])
if st.button('Edit Task'):
update_result = collection.update_one({'task': task_to_edit},
{'$set': {'task': task_data['task'],
'category': task_data['category'],
'priority': task_data['priority']}})
if update_result.modified_count > 0:
st.success('Task successfully updated.')
else:
st.warning('Task not found.')
else:
st.warning('Task not found.')
# Page - Delete Task
elif page == 'Delete Task':
st.write('Delete Task')
tasks = [task['task'] for task in collection.find({}, {"_id": 0})]
task_to_delete = st.selectbox('Select Task to Delete', tasks)
if task_to_delete:
task_data = collection.find_one({'task': task_to_delete})
if task_data:
st.write('Task:', task_data['task'])
st.write('Category:', task_data['category'])
st.write('Priority:', task_data['priority'])
if st.button('Delete Task'):
if not task_to_delete:
st.warning('Please select a task to delete.')
else:
delete_result = collection.delete_one({'task': task_to_delete})
if delete_result.deleted_count > 0:
st.success('Task successfully deleted.')
else:
st.warning('Task not found.')