Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -49,6 +49,16 @@ def delete_records(ids):
|
|
49 |
except Exception as e:
|
50 |
return False, f"Error deleting records: {str(e)}"
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
# Streamlit app
|
53 |
st.title("π Cosmos DB Record Management")
|
54 |
|
@@ -80,57 +90,95 @@ else:
|
|
80 |
edited_df = st.sidebar.data_editor(records_df, num_rows="dynamic", key="sidebar_editor")
|
81 |
|
82 |
# Main Content Area
|
83 |
-
col1, col2 = st.columns(2)
|
84 |
|
85 |
with col1:
|
86 |
-
st.subheader("π
|
87 |
id = st.text_input("ID")
|
88 |
name = st.text_input("Name")
|
89 |
document = st.text_area("Document")
|
90 |
evaluation_text = st.text_area("Evaluation Text")
|
91 |
evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
with col2:
|
110 |
-
st.subheader("
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
"evaluationText": evaluation_text,
|
117 |
-
"evaluationScore": evaluation_score
|
118 |
-
}
|
119 |
-
|
120 |
-
success, response = call_stored_procedure(record)
|
121 |
-
if success:
|
122 |
-
st.success("β
Stored procedure executed successfully!")
|
123 |
-
st.json(response)
|
124 |
-
else:
|
125 |
-
st.error(f"β Failed to execute stored procedure: {response}")
|
126 |
|
127 |
st.subheader("π All Records")
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
-
|
131 |
-
with
|
132 |
if st.button("ποΈ Delete Selected"):
|
133 |
-
selected_ids = edited_df[edited_df['select']]['id'].tolist()
|
134 |
if selected_ids:
|
135 |
success, message = delete_records(selected_ids)
|
136 |
if success:
|
@@ -141,7 +189,7 @@ else:
|
|
141 |
else:
|
142 |
st.warning("No records selected for deletion.")
|
143 |
|
144 |
-
with
|
145 |
if st.download_button("π₯ Download Data", edited_df.to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
|
146 |
st.success("Data downloaded successfully!")
|
147 |
|
@@ -156,4 +204,13 @@ else:
|
|
156 |
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
157 |
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
158 |
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
159 |
-
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
except Exception as e:
|
50 |
return False, f"Error deleting records: {str(e)}"
|
51 |
|
52 |
+
def delete_all_records():
|
53 |
+
try:
|
54 |
+
query = "SELECT c.id FROM c"
|
55 |
+
items = list(container.query_items(query=query, enable_cross_partition_query=True))
|
56 |
+
for item in items:
|
57 |
+
container.delete_item(item=item['id'], partition_key=item['id'])
|
58 |
+
return True, f"Successfully deleted all records"
|
59 |
+
except Exception as e:
|
60 |
+
return False, f"Error deleting all records: {str(e)}"
|
61 |
+
|
62 |
# Streamlit app
|
63 |
st.title("π Cosmos DB Record Management")
|
64 |
|
|
|
90 |
edited_df = st.sidebar.data_editor(records_df, num_rows="dynamic", key="sidebar_editor")
|
91 |
|
92 |
# Main Content Area
|
93 |
+
col1, col2 = st.columns([2, 1])
|
94 |
|
95 |
with col1:
|
96 |
+
st.subheader("π Record Details")
|
97 |
id = st.text_input("ID")
|
98 |
name = st.text_input("Name")
|
99 |
document = st.text_area("Document")
|
100 |
evaluation_text = st.text_area("Evaluation Text")
|
101 |
evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
102 |
|
103 |
+
col3, col4, col5 = st.columns(3)
|
104 |
+
with col3:
|
105 |
+
if st.button("πΎ Insert Record"):
|
106 |
+
record = {
|
107 |
+
"id": id,
|
108 |
+
"name": name,
|
109 |
+
"document": document,
|
110 |
+
"evaluationText": evaluation_text,
|
111 |
+
"evaluationScore": evaluation_score
|
112 |
+
}
|
113 |
+
|
114 |
+
success, response = insert_record(record)
|
115 |
+
if success:
|
116 |
+
st.success("β
Record inserted successfully!")
|
117 |
+
st.json(response)
|
118 |
+
else:
|
119 |
+
st.error(f"β Failed to insert record: {response}")
|
120 |
+
|
121 |
+
with col4:
|
122 |
+
if st.button("π§ Call Procedure"):
|
123 |
+
record = {
|
124 |
+
"id": id,
|
125 |
+
"name": name,
|
126 |
+
"document": document,
|
127 |
+
"evaluationText": evaluation_text,
|
128 |
+
"evaluationScore": evaluation_score
|
129 |
+
}
|
130 |
+
|
131 |
+
success, response = call_stored_procedure(record)
|
132 |
+
if success:
|
133 |
+
st.success("β
Stored procedure executed successfully!")
|
134 |
+
st.json(response)
|
135 |
+
else:
|
136 |
+
st.error(f"β Failed to execute stored procedure: {response}")
|
137 |
+
|
138 |
+
with col5:
|
139 |
+
if st.button("ποΈ Delete All"):
|
140 |
+
success, message = delete_all_records()
|
141 |
+
if success:
|
142 |
+
st.success(message)
|
143 |
+
else:
|
144 |
+
st.error(message)
|
145 |
+
st.rerun()
|
146 |
|
147 |
with col2:
|
148 |
+
st.subheader("π Selected Record")
|
149 |
+
selected_rows = edited_df[edited_df['select'] == True]
|
150 |
+
if not selected_rows.empty:
|
151 |
+
st.json(selected_rows.iloc[0].to_dict())
|
152 |
+
else:
|
153 |
+
st.write("No record selected")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
st.subheader("π All Records")
|
156 |
+
for i, record in edited_df.iterrows():
|
157 |
+
cols = st.columns([3, 2, 2, 1, 1, 1])
|
158 |
+
with cols[0]:
|
159 |
+
st.markdown(f"**{record['name']}**")
|
160 |
+
st.markdown(f"*{record['document']}*")
|
161 |
+
with cols[1]:
|
162 |
+
st.markdown(f"Evaluation: {record['evaluationText']}")
|
163 |
+
with cols[2]:
|
164 |
+
st.markdown(f"Score: {record['evaluationScore']}")
|
165 |
+
with cols[3]:
|
166 |
+
st.button("π", key=f"like_{i}")
|
167 |
+
with cols[4]:
|
168 |
+
st.button("π", key=f"dislike_{i}")
|
169 |
+
with cols[5]:
|
170 |
+
if st.button("ποΈ", key=f"delete_{i}"):
|
171 |
+
success, message = delete_records([record['id']])
|
172 |
+
if success:
|
173 |
+
st.success(message)
|
174 |
+
else:
|
175 |
+
st.error(message)
|
176 |
+
st.rerun()
|
177 |
|
178 |
+
col6, col7 = st.columns(2)
|
179 |
+
with col6:
|
180 |
if st.button("ποΈ Delete Selected"):
|
181 |
+
selected_ids = edited_df[edited_df['select'] == True]['id'].tolist()
|
182 |
if selected_ids:
|
183 |
success, message = delete_records(selected_ids)
|
184 |
if success:
|
|
|
189 |
else:
|
190 |
st.warning("No records selected for deletion.")
|
191 |
|
192 |
+
with col7:
|
193 |
if st.download_button("π₯ Download Data", edited_df.to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
|
194 |
st.success("Data downloaded successfully!")
|
195 |
|
|
|
204 |
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
205 |
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
206 |
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
207 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
208 |
+
|
209 |
+
# Update fields when a record is selected in the sidebar
|
210 |
+
if st.session_state.logged_in and not edited_df[edited_df['select'] == True].empty:
|
211 |
+
selected_record = edited_df[edited_df['select'] == True].iloc[0]
|
212 |
+
st.session_state['id'] = selected_record['id']
|
213 |
+
st.session_state['name'] = selected_record['name']
|
214 |
+
st.session_state['document'] = selected_record['document']
|
215 |
+
st.session_state['evaluation_text'] = selected_record['evaluationText']
|
216 |
+
st.session_state['evaluation_score'] = selected_record['evaluationScore']
|