awacke1 commited on
Commit
71b0b4c
β€’
1 Parent(s): 81e895d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -33
app.py CHANGED
@@ -1,23 +1,21 @@
1
  import streamlit as st
2
- from azure.cosmos import CosmosClient, PartitionKey, exceptions
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
- # πŸ•’ Function to generate a unique ID based on timestamp
120
- def generate_timestamp_id():
121
- # ⏰ Time waits for no one, but IDs can!
122
- return datetime.now().strftime('%Y%m%d%H%M%S%f')
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 section
169
- if not st.session_state.logged_in:
170
- st.subheader("πŸ” Login")
171
- # Let's be sneaky and use the Key from environment variables! πŸ•΅οΈβ€β™‚οΈ
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.rerun()
 
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.rerun()
 
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.subheader(f"πŸ“„ Document Details: {selected_document}")
228
- selected_doc = next((doc for doc in documents if doc.get('id') == selected_document), 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,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 based on timestamp
273
- cloned_doc['id'] = generate_timestamp_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=generate_timestamp_id(), key='new_id')
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%d_%H%M%S')}"
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.rerun()
 
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()