File size: 3,698 Bytes
c7c7734
7411f2c
c7c7734
7411f2c
c7c7734
7411f2c
c7c7734
7411f2c
 
 
 
c7c7734
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
# import pandas as pd
import pymongo
import certifi

############################## 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, tlsCAFile=certifi.where())
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.')