-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
57 lines (44 loc) · 1.55 KB
/
data.py
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
from contextlib import contextmanager
from pymongo import MongoClient
import streamlit as st
@contextmanager
def MongoDBConnector():
try:
client = MongoClient(st.secrets["DB_URI"])
database = client[st.secrets["DB_NAME"]]
collection = database[st.secrets["COLLECTION_NAME"]]
yield collection
except Exception as e:
st.error(e)
finally:
client.close()
@st.cache_data(show_spinner="Fetching data from database...")
def load_sections(section):
with MongoDBConnector() as collection:
result = list(collection.find({'section': section}, {'_id':0}))
return result
def append_new_exercise(new_exercise, section_name, subsection_number):
# Filter to identify the document to update
filter = {
"section": section_name,
"subsection_number": subsection_number
}
# Push the new exercise to the 'exercises' array
append = {
"$push": {
"exercises": new_exercise[0]
}
}
with MongoDBConnector() as collection:
try:
# Update the document
exercises_appended = collection.update_one(filter, append)
collection
if exercises_appended.modified_count > 0:
st.success(f"Exercises added successfully: {exercises_appended.modified_count}.")
else:
st.error("No documents were added.")
except Exception as e:
st.error(e)
# Clear values from *all* in-memory or on-disk cached functions
st.cache_data.clear()