Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
-
from azure.cosmos import CosmosClient,
|
3 |
import os
|
4 |
import pandas as pd
|
5 |
import traceback
|
6 |
-
import requests
|
7 |
import shutil
|
8 |
-
import zipfile
|
9 |
from github import Github
|
10 |
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")
|
17 |
|
18 |
# π Cosmos DB configuration
|
19 |
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
20 |
-
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
21 |
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
22 |
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
23 |
Key = os.environ.get("Key") # π Don't forget your key!
|
@@ -116,10 +114,10 @@ def delete_record(container, name, id):
|
|
116 |
except Exception as e:
|
117 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
118 |
|
119 |
-
#
|
120 |
-
def
|
121 |
-
#
|
122 |
-
return
|
123 |
|
124 |
# π¦ Function to archive current container
|
125 |
def archive_current_container(database_name, container_name, client):
|
@@ -164,22 +162,18 @@ def main():
|
|
164 |
st.session_state.selected_database = None
|
165 |
if 'selected_container' not in st.session_state:
|
166 |
st.session_state.selected_container = None
|
|
|
|
|
167 |
|
168 |
-
# π Login
|
169 |
-
if
|
170 |
-
st.
|
171 |
-
|
172 |
-
input_key = Key
|
173 |
-
|
174 |
-
# π Time to blast off!
|
175 |
-
if st.button("π Login"):
|
176 |
-
if input_key:
|
177 |
-
st.session_state.primary_key = input_key
|
178 |
-
st.session_state.logged_in = True
|
179 |
-
st.rerun()
|
180 |
-
else:
|
181 |
-
st.error("Invalid key. Please check your input. πβ")
|
182 |
else:
|
|
|
|
|
|
|
|
|
183 |
# π Initialize Cosmos DB client
|
184 |
try:
|
185 |
if st.session_state.client is None:
|
@@ -194,7 +188,8 @@ def main():
|
|
194 |
if selected_db != st.session_state.selected_database:
|
195 |
st.session_state.selected_database = selected_db
|
196 |
st.session_state.selected_container = None
|
197 |
-
st.
|
|
|
198 |
|
199 |
if st.session_state.selected_database:
|
200 |
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
@@ -203,7 +198,8 @@ def main():
|
|
203 |
|
204 |
if selected_container != st.session_state.selected_container:
|
205 |
st.session_state.selected_container = selected_container
|
206 |
-
st.
|
|
|
207 |
|
208 |
if st.session_state.selected_container:
|
209 |
container = database.get_container_client(st.session_state.selected_container)
|
@@ -221,11 +217,15 @@ def main():
|
|
221 |
|
222 |
if documents:
|
223 |
document_ids = [doc.get('id', 'Unknown') for doc in documents]
|
224 |
-
selected_document = st.sidebar.selectbox("π Select Document", document_ids)
|
225 |
|
226 |
-
if selected_document:
|
227 |
-
st.
|
228 |
-
|
|
|
|
|
|
|
|
|
229 |
if selected_doc:
|
230 |
# π¨ Add Viewer/Editor selection
|
231 |
view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
|
@@ -261,6 +261,8 @@ def main():
|
|
261 |
success, message = update_record(container, updated_doc)
|
262 |
if success:
|
263 |
st.success(message)
|
|
|
|
|
264 |
else:
|
265 |
st.error(message)
|
266 |
except json.JSONDecodeError as e:
|
@@ -269,17 +271,20 @@ def main():
|
|
269 |
# 𧬠Clone Document
|
270 |
if st.button("π Clone Document"):
|
271 |
cloned_doc = selected_doc.copy()
|
272 |
-
# Generate a unique ID
|
273 |
-
cloned_doc['id'] =
|
274 |
success, message = insert_record(container, cloned_doc)
|
275 |
if success:
|
276 |
st.success(f"Document cloned with new id: {cloned_doc['id']} π")
|
|
|
|
|
|
|
277 |
else:
|
278 |
st.error(message)
|
279 |
elif selected_view == 'New Record':
|
280 |
# π New Record
|
281 |
st.markdown("#### Create a new document:")
|
282 |
-
new_id = st.text_input("ID", value=
|
283 |
new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
|
284 |
if st.button("β Create New Document"):
|
285 |
try:
|
@@ -288,6 +293,9 @@ def main():
|
|
288 |
success, message = insert_record(container, new_doc)
|
289 |
if success:
|
290 |
st.success(f"New document created with id: {new_doc['id']} π")
|
|
|
|
|
|
|
291 |
else:
|
292 |
st.error(message)
|
293 |
except json.JSONDecodeError as e:
|
@@ -315,7 +323,7 @@ def main():
|
|
315 |
if st.button("π₯ Clone Repository"):
|
316 |
if github_token and source_repo:
|
317 |
try:
|
318 |
-
local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%
|
319 |
download_github_repo(source_repo, local_path)
|
320 |
zip_filename = f"{new_repo_name}.zip"
|
321 |
create_zip_file(local_path, zip_filename[:-4])
|
@@ -361,7 +369,8 @@ def main():
|
|
361 |
st.session_state.client = None
|
362 |
st.session_state.selected_database = None
|
363 |
st.session_state.selected_container = None
|
364 |
-
st.
|
|
|
365 |
|
366 |
if __name__ == "__main__":
|
367 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from azure.cosmos import CosmosClient, exceptions
|
3 |
import os
|
4 |
import pandas as pd
|
5 |
import traceback
|
|
|
6 |
import shutil
|
|
|
7 |
from github import Github
|
8 |
from git import Repo
|
9 |
from datetime import datetime
|
10 |
import base64
|
11 |
import json
|
12 |
+
import uuid # π² For generating unique IDs
|
13 |
|
14 |
# π Welcome to our fun-filled Cosmos DB and GitHub Integration app!
|
15 |
st.set_page_config(layout="wide")
|
16 |
|
17 |
# π Cosmos DB configuration
|
18 |
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
|
|
19 |
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
20 |
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
21 |
Key = os.environ.get("Key") # π Don't forget your key!
|
|
|
114 |
except Exception as e:
|
115 |
return False, f"An unexpected error occurred: {traceback.format_exc()} π±"
|
116 |
|
117 |
+
# π² Function to generate a unique UUID
|
118 |
+
def generate_unique_id():
|
119 |
+
# π§ββοΈ Generating a unique UUID!
|
120 |
+
return str(uuid.uuid4())
|
121 |
|
122 |
# π¦ Function to archive current container
|
123 |
def archive_current_container(database_name, container_name, client):
|
|
|
162 |
st.session_state.selected_database = None
|
163 |
if 'selected_container' not in st.session_state:
|
164 |
st.session_state.selected_container = None
|
165 |
+
if 'selected_document_id' not in st.session_state:
|
166 |
+
st.session_state.selected_document_id = None
|
167 |
|
168 |
+
# π Automatic Login
|
169 |
+
if Key:
|
170 |
+
st.session_state.primary_key = Key
|
171 |
+
st.session_state.logged_in = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
else:
|
173 |
+
st.error("Cosmos DB Key is not set in environment variables. πβ")
|
174 |
+
return # Can't proceed without a key
|
175 |
+
|
176 |
+
if st.session_state.logged_in:
|
177 |
# π Initialize Cosmos DB client
|
178 |
try:
|
179 |
if st.session_state.client is None:
|
|
|
188 |
if selected_db != st.session_state.selected_database:
|
189 |
st.session_state.selected_database = selected_db
|
190 |
st.session_state.selected_container = None
|
191 |
+
st.session_state.selected_document_id = None
|
192 |
+
st.experimental_rerun()
|
193 |
|
194 |
if st.session_state.selected_database:
|
195 |
database = st.session_state.client.get_database_client(st.session_state.selected_database)
|
|
|
198 |
|
199 |
if selected_container != st.session_state.selected_container:
|
200 |
st.session_state.selected_container = selected_container
|
201 |
+
st.session_state.selected_document_id = None
|
202 |
+
st.experimental_rerun()
|
203 |
|
204 |
if st.session_state.selected_container:
|
205 |
container = database.get_container_client(st.session_state.selected_container)
|
|
|
217 |
|
218 |
if documents:
|
219 |
document_ids = [doc.get('id', 'Unknown') for doc in documents]
|
220 |
+
selected_document = st.sidebar.selectbox("π Select Document", document_ids, index=document_ids.index(st.session_state.selected_document_id) if st.session_state.selected_document_id in document_ids else 0)
|
221 |
|
222 |
+
if selected_document != st.session_state.selected_document_id:
|
223 |
+
st.session_state.selected_document_id = selected_document
|
224 |
+
st.experimental_rerun()
|
225 |
+
|
226 |
+
if st.session_state.selected_document_id:
|
227 |
+
st.subheader(f"π Document Details: {st.session_state.selected_document_id}")
|
228 |
+
selected_doc = next((doc for doc in documents if doc.get('id') == st.session_state.selected_document_id), None)
|
229 |
if selected_doc:
|
230 |
# π¨ Add Viewer/Editor selection
|
231 |
view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
|
|
|
261 |
success, message = update_record(container, updated_doc)
|
262 |
if success:
|
263 |
st.success(message)
|
264 |
+
st.session_state.selected_document_id = updated_doc['id']
|
265 |
+
st.experimental_rerun()
|
266 |
else:
|
267 |
st.error(message)
|
268 |
except json.JSONDecodeError as e:
|
|
|
271 |
# 𧬠Clone Document
|
272 |
if st.button("π Clone Document"):
|
273 |
cloned_doc = selected_doc.copy()
|
274 |
+
# Generate a unique ID
|
275 |
+
cloned_doc['id'] = generate_unique_id()
|
276 |
success, message = insert_record(container, cloned_doc)
|
277 |
if success:
|
278 |
st.success(f"Document cloned with new id: {cloned_doc['id']} π")
|
279 |
+
st.session_state.selected_document_id = cloned_doc['id']
|
280 |
+
# Switch to 'Show as Edit and Save' mode
|
281 |
+
st.experimental_rerun()
|
282 |
else:
|
283 |
st.error(message)
|
284 |
elif selected_view == 'New Record':
|
285 |
# π New Record
|
286 |
st.markdown("#### Create a new document:")
|
287 |
+
new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
|
288 |
new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
|
289 |
if st.button("β Create New Document"):
|
290 |
try:
|
|
|
293 |
success, message = insert_record(container, new_doc)
|
294 |
if success:
|
295 |
st.success(f"New document created with id: {new_doc['id']} π")
|
296 |
+
st.session_state.selected_document_id = new_doc['id']
|
297 |
+
# Switch to 'Show as Edit and Save' mode
|
298 |
+
st.experimental_rerun()
|
299 |
else:
|
300 |
st.error(message)
|
301 |
except json.JSONDecodeError as e:
|
|
|
323 |
if st.button("π₯ Clone Repository"):
|
324 |
if github_token and source_repo:
|
325 |
try:
|
326 |
+
local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
327 |
download_github_repo(source_repo, local_path)
|
328 |
zip_filename = f"{new_repo_name}.zip"
|
329 |
create_zip_file(local_path, zip_filename[:-4])
|
|
|
369 |
st.session_state.client = None
|
370 |
st.session_state.selected_database = None
|
371 |
st.session_state.selected_container = None
|
372 |
+
st.session_state.selected_document_id = None
|
373 |
+
st.experimental_rerun()
|
374 |
|
375 |
if __name__ == "__main__":
|
376 |
main()
|