import streamlit as st import pymongo url='mongodb+srv://baoyuxin1994:0817@cluster0.rcvcj3s.mongodb.net/?retryWrites=true&w=majority' client=pymongo.MongoClient(url) db=client['todo'] collection=db['tasks'] st.title('TODO app') page=st.sidebar.selectbox('Menu',['Home','Insert','Edit','Delete']) if page=='Insert': st.header('Insert Task') task=st.text_input('Task') category=st.selectbox('Category',['','Work','Personal','Shopping']) ranking=st.slider('Priority', 0, 5, 0) if st.button('Insert your task'): if task.strip() == "": st.warning('Task can not be empty',icon='⚠️') elif category.strip() == "": st.warning('Category can not be empty',icon='⚠️') elif ranking == 0: st.warning('Priority can not be 0',icon='⚠️') else: collection.insert_one({'task':task, 'category':category,'Priority':ranking}) st.success('Successfully Inserted') elif page=='Edit': st.header('Edit Task') _task = collection.find({},{'task':1,'_id':0}) _task_list=[] for t in _task: _task_list.append(t['task']) old_one=st.selectbox('Select the one to edit',_task_list) new_one=st.text_input('New Task') category=st.selectbox('Category',['','Work','Personal','Shopping']) ranking=st.slider('Priority', 0, 5, 0) if st.button('Edit your task'): if new_one.strip() == "": st.warning('Task can not be empty',icon='⚠️') elif category.strip() == "": st.warning('Category can not be empty',icon='⚠️') elif ranking == 0: st.warning('Priority can not be 0',icon='⚠️') else: collection.replace_one(collection.find_one({},{'task':old_one}),{'task':new_one,'category':category,'Priority':ranking}) st.success('Successfully Edited') elif page=='Delete': st.header('Delete Task') tasks=collection.find({},{'_id':0}) _task = collection.find({},{'task':1,'_id':0}) _task_list=[] for t in _task: _task_list.append(t['task']) delete_the_task=st.selectbox('Delete',_task_list) if st.button('Delete your task'): collection.delete_one({'task':delete_the_task}) st.success('Successfully Deleted') elif page=='Home': st.header('Home') st.write('This is a Todo list which allows you to read, insert,edit and delete') #tasks=collection.find({},{'_id':0}) #st.table(tasks) all_tasks=collection.find({},{'_id':0}).sort({'Priority':-1,}) st.table(all_tasks)