Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,8 @@ from git import Repo
|
|
11 |
from datetime import datetime
|
12 |
import base64
|
13 |
import json
|
|
|
|
|
14 |
|
15 |
# π Welcome to our fun-filled Cosmos DB and GitHub Integration app!
|
16 |
st.set_page_config(layout="wide")
|
@@ -92,7 +94,7 @@ def insert_record(container, record):
|
|
92 |
container.create_item(body=record)
|
93 |
return True, "Record inserted successfully! π"
|
94 |
except exceptions.CosmosHttpResponseError as e:
|
95 |
-
return False, f"HTTP error occurred: {str(e)}
|
96 |
except Exception as e:
|
97 |
return False, f"An unexpected error occurred: {str(e)} π±"
|
98 |
|
@@ -101,7 +103,7 @@ def update_record(container, updated_record):
|
|
101 |
container.upsert_item(body=updated_record)
|
102 |
return True, f"Record with id {updated_record['id']} successfully updated. π οΈ"
|
103 |
except exceptions.CosmosHttpResponseError as e:
|
104 |
-
return False, f"HTTP error occurred: {str(e)}
|
105 |
except Exception as e:
|
106 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
107 |
|
@@ -112,10 +114,15 @@ def delete_record(container, name, id):
|
|
112 |
except exceptions.CosmosResourceNotFoundError:
|
113 |
return False, f"Record with id {id} not found. It may have been already deleted. π΅οΈββοΈ"
|
114 |
except exceptions.CosmosHttpResponseError as e:
|
115 |
-
return False, f"HTTP error occurred: {str(e)}
|
116 |
except Exception as e:
|
117 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
118 |
|
|
|
|
|
|
|
|
|
|
|
119 |
# π¦ Function to archive current container
|
120 |
def archive_current_container(database_name, container_name, client):
|
121 |
try:
|
@@ -171,7 +178,7 @@ def main():
|
|
171 |
if input_key:
|
172 |
st.session_state.primary_key = input_key
|
173 |
st.session_state.logged_in = True
|
174 |
-
st.
|
175 |
else:
|
176 |
st.error("Invalid key. Please check your input. πβ")
|
177 |
else:
|
@@ -189,7 +196,7 @@ def main():
|
|
189 |
if selected_db != st.session_state.selected_database:
|
190 |
st.session_state.selected_database = selected_db
|
191 |
st.session_state.selected_container = None
|
192 |
-
st.
|
193 |
|
194 |
if st.session_state.selected_database:
|
195 |
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
@@ -198,7 +205,7 @@ def main():
|
|
198 |
|
199 |
if selected_container != st.session_state.selected_container:
|
200 |
st.session_state.selected_container = selected_container
|
201 |
-
st.
|
202 |
|
203 |
if st.session_state.selected_container:
|
204 |
container = database.get_container_client(st.session_state.selected_container)
|
@@ -257,8 +264,8 @@ def main():
|
|
257 |
# 𧬠Clone Document
|
258 |
if st.button("π Clone Document"):
|
259 |
cloned_doc = selected_doc.copy()
|
260 |
-
|
261 |
-
cloned_doc['id'] =
|
262 |
success, message = insert_record(container, cloned_doc)
|
263 |
if success:
|
264 |
st.success(f"Document cloned with new id: {cloned_doc['id']} π")
|
@@ -271,7 +278,7 @@ def main():
|
|
271 |
try:
|
272 |
new_doc = json.loads(new_doc_str)
|
273 |
if 'id' not in new_doc:
|
274 |
-
new_doc['id'] =
|
275 |
success, message = insert_record(container, new_doc)
|
276 |
if success:
|
277 |
st.success(f"New document created with id: {new_doc['id']} π")
|
@@ -334,7 +341,7 @@ def main():
|
|
334 |
st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. πβ")
|
335 |
|
336 |
except exceptions.CosmosHttpResponseError as e:
|
337 |
-
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)}
|
338 |
except Exception as e:
|
339 |
st.error(f"An unexpected error occurred: {str(e)} π±")
|
340 |
|
@@ -345,7 +352,10 @@ def main():
|
|
345 |
st.session_state.client = None
|
346 |
st.session_state.selected_database = None
|
347 |
st.session_state.selected_container = None
|
348 |
-
st.
|
349 |
|
350 |
if __name__ == "__main__":
|
351 |
main()
|
|
|
|
|
|
|
|
11 |
from datetime import datetime
|
12 |
import base64
|
13 |
import json
|
14 |
+
import random
|
15 |
+
import string # π² For generating random IDs
|
16 |
|
17 |
# π Welcome to our fun-filled Cosmos DB and GitHub Integration app!
|
18 |
st.set_page_config(layout="wide")
|
|
|
94 |
container.create_item(body=record)
|
95 |
return True, "Record inserted successfully! π"
|
96 |
except exceptions.CosmosHttpResponseError as e:
|
97 |
+
return False, f"HTTP error occurred: {str(e)} π¨"
|
98 |
except Exception as e:
|
99 |
return False, f"An unexpected error occurred: {str(e)} π±"
|
100 |
|
|
|
103 |
container.upsert_item(body=updated_record)
|
104 |
return True, f"Record with id {updated_record['id']} successfully updated. π οΈ"
|
105 |
except exceptions.CosmosHttpResponseError as e:
|
106 |
+
return False, f"HTTP error occurred: {str(e)} π¨"
|
107 |
except Exception as e:
|
108 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
109 |
|
|
|
114 |
except exceptions.CosmosResourceNotFoundError:
|
115 |
return False, f"Record with id {id} not found. It may have been already deleted. π΅οΈββοΈ"
|
116 |
except exceptions.CosmosHttpResponseError as e:
|
117 |
+
return False, f"HTTP error occurred: {str(e)} π¨"
|
118 |
except Exception as e:
|
119 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
120 |
|
121 |
+
# π² Function to generate a short, unique alphanumeric ID
|
122 |
+
def generate_short_id(length=8):
|
123 |
+
# π§ββοΈ Generating a short, unique ID!
|
124 |
+
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
125 |
+
|
126 |
# π¦ Function to archive current container
|
127 |
def archive_current_container(database_name, container_name, client):
|
128 |
try:
|
|
|
178 |
if input_key:
|
179 |
st.session_state.primary_key = input_key
|
180 |
st.session_state.logged_in = True
|
181 |
+
st.experimental_rerun()
|
182 |
else:
|
183 |
st.error("Invalid key. Please check your input. πβ")
|
184 |
else:
|
|
|
196 |
if selected_db != st.session_state.selected_database:
|
197 |
st.session_state.selected_database = selected_db
|
198 |
st.session_state.selected_container = None
|
199 |
+
st.experimental_rerun()
|
200 |
|
201 |
if st.session_state.selected_database:
|
202 |
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
|
|
205 |
|
206 |
if selected_container != st.session_state.selected_container:
|
207 |
st.session_state.selected_container = selected_container
|
208 |
+
st.experimental_rerun()
|
209 |
|
210 |
if st.session_state.selected_container:
|
211 |
container = database.get_container_client(st.session_state.selected_container)
|
|
|
264 |
# 𧬠Clone Document
|
265 |
if st.button("π Clone Document"):
|
266 |
cloned_doc = selected_doc.copy()
|
267 |
+
# Generate a unique, short ID for the cloned document
|
268 |
+
cloned_doc['id'] = generate_short_id()
|
269 |
success, message = insert_record(container, cloned_doc)
|
270 |
if success:
|
271 |
st.success(f"Document cloned with new id: {cloned_doc['id']} π")
|
|
|
278 |
try:
|
279 |
new_doc = json.loads(new_doc_str)
|
280 |
if 'id' not in new_doc:
|
281 |
+
new_doc['id'] = generate_short_id()
|
282 |
success, message = insert_record(container, new_doc)
|
283 |
if success:
|
284 |
st.success(f"New document created with id: {new_doc['id']} π")
|
|
|
341 |
st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. πβ")
|
342 |
|
343 |
except exceptions.CosmosHttpResponseError as e:
|
344 |
+
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} π¨")
|
345 |
except Exception as e:
|
346 |
st.error(f"An unexpected error occurred: {str(e)} π±")
|
347 |
|
|
|
352 |
st.session_state.client = None
|
353 |
st.session_state.selected_database = None
|
354 |
st.session_state.selected_container = None
|
355 |
+
st.experimental_rerun()
|
356 |
|
357 |
if __name__ == "__main__":
|
358 |
main()
|
359 |
+
|
360 |
+
|
361 |
+
|