Spaces:
Sleeping
Sleeping
import streamlit as st | |
from streamlit_option_menu import option_menu | |
import pymongo | |
class ErrorMSG: | |
ERROR_MSG_NONESTR = 0 | |
ERROR_MSG_NOFOUND = 1 | |
ERROR_MSG_NONESTREDIT = 2 | |
msg = [ | |
'Warning: You should input a Task above and select the right Category & Priority', | |
'Warning: No such task found, Please check your input agian', | |
'Warning: You should input a Task you want to edit and input the right information you want to edit' | |
] | |
uri = "mongodb+srv://123123:mb123123@cluster.c0bshep.mongodb.net/?retryWrites=true&w=majority" | |
client = pymongo.MongoClient(uri) | |
db = client["demo"] | |
collection = db["class"] | |
st.set_page_config(page_title="Marble's ToDo List",layout='wide') | |
st.title('TODO LIST') | |
st.write('This is a TODO List which allows you to insert, update, delete your task') | |
with st.sidebar: | |
page = option_menu("ToDo List",['Home','Insert','Edit', 'Delete'], | |
icons=['house','box-arrow-in-up','brush', 'backspace-reverse']) | |
if page == 'Insert': | |
st.header('Insert Task') | |
task = st.text_input('Task') | |
cate = st.selectbox('Category', ['Work', 'Study', 'Entertainment']) | |
pri = st.slider('Priority', 1, 5) | |
if st.button('Insert'): | |
if len(task): | |
collection.insert_one({'Task': task, 'Category': cate, 'Priority': pri}) | |
st.success('Successfully Inserted') | |
else: | |
st.warning(ErrorMSG.msg[ErrorMSG.ERROR_MSG_NONESTR], icon="⚠️") | |
elif page == 'Edit': | |
st.header('Edit Task') | |
task = st.text_input('Find the Task & Category & Priority you want to Edit') | |
cate = st.selectbox('Category', ['Work', 'Study', 'Entertainment']) | |
pri = st.slider('Priority', 1, 5) | |
st.header('Input your Edit') | |
taskEdit = st.text_input('Input the Task & Category & Priority you want to Edit') | |
cateEdit = st.selectbox('Edit Category', ['Work', 'Study', 'Entertainment']) | |
priEdit = st.slider('Edit Priority', 1, 5) | |
if st.button('Edit'): | |
if len(task) and len(taskEdit): | |
result = collection.find_one({'Task': task, 'Category': cate, 'Priority': pri}) | |
if (result): | |
collection.update_one({'Task': task, 'Category': cate, 'Priority': pri}, {'$set':{'Task': taskEdit, 'Category': cateEdit, 'Priority': priEdit}}) | |
st.success('Successfully Edited') | |
else: | |
st.warning(ErrorMSG.msg[ErrorMSG.ERROR_MSG_NOFOUND], icon="⚠️") | |
else: | |
st.warning(ErrorMSG.msg[ErrorMSG.ERROR_MSG_NONESTREDIT], icon="⚠️") | |
elif page == 'Delete': | |
st.header('Delete Task') | |
task = st.text_input('Input the Task & Category you want to Delete') | |
cate = st.selectbox('Category', ['Work', 'Study', 'Entertainment']) | |
pri = st.slider('Priority', 1, 5) | |
if st.button('Delete'): | |
if (len(task)): | |
result = collection.find_one({'Task': task, 'Category': cate, 'Priority': pri}) | |
if (result): | |
collection.delete_one({'Task': task, 'Category': cate, 'Priority': pri}) | |
st.success('Successfully Deleted') | |
else: | |
st.warning(ErrorMSG.msg[ErrorMSG.ERROR_MSG_NOFOUND], icon="⚠️") | |
else: | |
st.warning(ErrorMSG.msg[ErrorMSG.ERROR_MSG_NONESTR], icon="⚠️") | |
elif page == 'Home': | |
st.header('Home Page') | |
tasks = collection.find({}, {'_id':0, }).sort([('Priority', -1), ('Task', 1)]) | |
st.table(tasks) |