awacke1 commited on
Commit
b8dbed7
ยท
verified ยท
1 Parent(s): 519bfc9

Delete backup3.betterFeatures.app.py

Browse files
Files changed (1) hide show
  1. backup3.betterFeatures.app.py +0 -428
backup3.betterFeatures.app.py DELETED
@@ -1,428 +0,0 @@
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
-
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
- DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
21
- CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
22
- Key = os.environ.get("Key") # ๐Ÿ”‘ Don't forget your key!
23
-
24
- # ๐Ÿ™ GitHub configuration
25
- def download_github_repo(url, local_path):
26
- # ๐Ÿšš Let's download that GitHub repo!
27
- if os.path.exists(local_path):
28
- shutil.rmtree(local_path)
29
- Repo.clone_from(url, local_path)
30
-
31
- def create_zip_file(source_dir, output_filename):
32
- # ๐Ÿ“ฆ Zipping up files like a pro!
33
- shutil.make_archive(output_filename, 'zip', source_dir)
34
-
35
- def create_repo(g, repo_name):
36
- # ๐Ÿ› ๏ธ Creating a new GitHub repo. Magic!
37
- user = g.get_user()
38
- return user.create_repo(repo_name)
39
-
40
- def push_to_github(local_path, repo, github_token):
41
- # ๐Ÿš€ Pushing code to GitHub. Hold on tight!
42
- repo_url = f"https://{github_token}@github.com/{repo.full_name}.git"
43
- local_repo = Repo(local_path)
44
-
45
- if 'origin' in [remote.name for remote in local_repo.remotes]:
46
- origin = local_repo.remote('origin')
47
- origin.set_url(repo_url)
48
- else:
49
- origin = local_repo.create_remote('origin', repo_url)
50
-
51
- if not local_repo.heads:
52
- local_repo.git.checkout('-b', 'main')
53
- current_branch = 'main'
54
- else:
55
- current_branch = local_repo.active_branch.name
56
-
57
- local_repo.git.add(A=True)
58
-
59
- if local_repo.is_dirty():
60
- local_repo.git.commit('-m', 'Initial commit')
61
-
62
- origin.push(refspec=f'{current_branch}:{current_branch}')
63
-
64
- def get_base64_download_link(file_path, file_name):
65
- # ๐Ÿง™โ€โ™‚๏ธ Generating a magical download link!
66
- with open(file_path, "rb") as file:
67
- contents = file.read()
68
- base64_encoded = base64.b64encode(contents).decode()
69
- return f'<a href="data:application/zip;base64,{base64_encoded}" download="{file_name}">โฌ‡๏ธ Download {file_name}</a>'
70
-
71
-
72
- # ๐Ÿงญ New functions for dynamic sidebar navigation
73
- def get_databases(client):
74
- # ๐Ÿ“š Fetching list of databases. So many options!
75
- return [db['id'] for db in client.list_databases()]
76
-
77
- def get_containers(database):
78
- # ๐Ÿ“‚ Getting containers. Containers within containers!
79
- return [container['id'] for container in database.list_containers()]
80
-
81
- def get_documents(container, limit=None):
82
- # ๐Ÿ“ Retrieving documents. Shhh, don't tell anyone!
83
- query = "SELECT * FROM c ORDER BY c._ts DESC"
84
- items = list(container.query_items(query=query, enable_cross_partition_query=True, max_item_count=limit))
85
- return items
86
-
87
-
88
- # ๐ŸŒŸ Cosmos DB functions
89
- def insert_record(container, record):
90
- try:
91
- container.create_item(body=record)
92
- return True, "Record inserted successfully! ๐ŸŽ‰"
93
- except exceptions.CosmosHttpResponseError as e:
94
- return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
95
- except Exception as e:
96
- return False, f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ"
97
-
98
- def update_record(container, updated_record):
99
- try:
100
- container.upsert_item(body=updated_record)
101
- return True, f"Record with id {updated_record['id']} successfully updated. ๐Ÿ› ๏ธ"
102
- except exceptions.CosmosHttpResponseError as e:
103
- return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
104
- except Exception as e:
105
- return False, f"An unexpected error occurred: {traceback.format_exc()} ๐Ÿ˜ฑ"
106
-
107
- def delete_record(container, name, id):
108
- try:
109
- container.delete_item(item=id, partition_key=id)
110
- return True, f"Successfully deleted record with name: {name} and id: {id} ๐Ÿ—‘๏ธ"
111
- except exceptions.CosmosResourceNotFoundError:
112
- return False, f"Record with id {id} not found. It may have been already deleted. ๐Ÿ•ต๏ธโ€โ™‚๏ธ"
113
- except exceptions.CosmosHttpResponseError as e:
114
- return False, f"HTTP error occurred: {str(e)} ๐Ÿšจ"
115
- except Exception as e:
116
- return False, f"An unexpected error occurred: {traceback.format_exc()} ๐Ÿ˜ฑ"
117
-
118
- # ๐ŸŽฒ Function to generate a unique UUID
119
- def generate_unique_id():
120
- # ๐Ÿง™โ€โ™‚๏ธ Generating a unique UUID!
121
- return str(uuid.uuid4())
122
-
123
- # ๐Ÿ“ฆ Function to archive current container
124
- def archive_current_container(database_name, container_name, client):
125
- try:
126
- base_dir = "./cosmos_archive_current_container"
127
- if os.path.exists(base_dir):
128
- shutil.rmtree(base_dir)
129
- os.makedirs(base_dir)
130
-
131
- db_client = client.get_database_client(database_name)
132
- container_client = db_client.get_container_client(container_name)
133
- items = list(container_client.read_all_items())
134
-
135
- container_dir = os.path.join(base_dir, container_name)
136
- os.makedirs(container_dir)
137
-
138
- for item in items:
139
- item_id = item.get('id', f"unknown_{datetime.now().strftime('%Y%m%d%H%M%S')}")
140
- with open(os.path.join(container_dir, f"{item_id}.json"), 'w') as f:
141
- json.dump(item, f, indent=2)
142
-
143
- archive_name = f"{container_name}_archive_{datetime.now().strftime('%Y%m%d%H%M%S')}"
144
- shutil.make_archive(archive_name, 'zip', base_dir)
145
-
146
- return get_base64_download_link(f"{archive_name}.zip", f"{archive_name}.zip")
147
- except Exception as e:
148
- return f"An error occurred while archiving data: {str(e)} ๐Ÿ˜ข"
149
-
150
-
151
- # ๐ŸŽˆ Let's modify the main app to be more fun!
152
- def main():
153
- st.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ - Azure Cosmos DB and Github Agent")
154
-
155
- # ๐Ÿšฆ Initialize session state
156
- if 'logged_in' not in st.session_state:
157
- st.session_state.logged_in = False
158
- if 'selected_records' not in st.session_state:
159
- st.session_state.selected_records = []
160
- if 'client' not in st.session_state:
161
- st.session_state.client = None
162
- if 'selected_database' not in st.session_state:
163
- st.session_state.selected_database = None
164
- if 'selected_container' not in st.session_state:
165
- st.session_state.selected_container = None
166
- if 'selected_document_id' not in st.session_state:
167
- st.session_state.selected_document_id = None
168
- if 'current_index' not in st.session_state:
169
- st.session_state.current_index = 0
170
- if 'cloned_doc' not in st.session_state:
171
- st.session_state.cloned_doc = None
172
-
173
- # ๐Ÿ” Automatic Login
174
- if Key:
175
- st.session_state.primary_key = Key
176
- st.session_state.logged_in = True
177
- else:
178
- st.error("Cosmos DB Key is not set in environment variables. ๐Ÿ”‘โŒ")
179
- return # Can't proceed without a key
180
-
181
- if st.session_state.logged_in:
182
- # ๐ŸŒŒ Initialize Cosmos DB client
183
- try:
184
- if st.session_state.client is None:
185
- st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
186
-
187
- # ๐Ÿ—„๏ธ Sidebar for database, container, and document selection
188
- st.sidebar.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ๐Ÿ—„๏ธNavigator")
189
-
190
- databases = get_databases(st.session_state.client)
191
- selected_db = st.sidebar.selectbox("๐Ÿ—ƒ๏ธ Select Database", databases)
192
-
193
- if selected_db != st.session_state.selected_database:
194
- st.session_state.selected_database = selected_db
195
- st.session_state.selected_container = None
196
- st.session_state.selected_document_id = None
197
- st.session_state.current_index = 0
198
- st.rerun()
199
-
200
- if st.session_state.selected_database:
201
- database = st.session_state.client.get_database_client(st.session_state.selected_database)
202
- containers = get_containers(database)
203
- selected_container = st.sidebar.selectbox("๐Ÿ“ Select Container", containers)
204
-
205
- if selected_container != st.session_state.selected_container:
206
- st.session_state.selected_container = selected_container
207
- st.session_state.selected_document_id = None
208
- st.session_state.current_index = 0
209
- st.rerun()
210
-
211
- if st.session_state.selected_container:
212
- container = database.get_container_client(st.session_state.selected_container)
213
-
214
- # ๐Ÿ“ฆ Add Export button
215
- if st.button("๐Ÿ“ฆ Export Container Data"):
216
- download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
217
- if download_link.startswith('<a'):
218
- st.markdown(download_link, unsafe_allow_html=True)
219
- else:
220
- st.error(download_link)
221
-
222
- # Fetch documents
223
- documents = get_documents(container)
224
- total_docs = len(documents)
225
-
226
- if total_docs > 5:
227
- documents_to_display = documents[:5]
228
- st.info("Showing top 5 most recent documents.")
229
- else:
230
- documents_to_display = documents
231
- st.info(f"Showing all {len(documents_to_display)} documents.")
232
-
233
- if documents_to_display:
234
- # ๐ŸŽจ Add Viewer/Editor selection
235
- view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
236
- selected_view = st.selectbox("Select Viewer/Editor", view_options, index=2)
237
-
238
- if selected_view == 'Show as Markdown':
239
- # ๐Ÿ–Œ๏ธ Show each record as Markdown
240
- for idx, doc in enumerate(documents_to_display):
241
- st.markdown(f"### **๐Ÿ†” ID: {doc.get('id', 'Unknown')}**")
242
- content = doc.get('content', '')
243
- if isinstance(content, dict) or isinstance(content, list):
244
- content = json.dumps(content, indent=2)
245
- st.markdown(content)
246
- elif selected_view == 'Show as Code Editor':
247
- # ๐Ÿ’ป Show each record in a code editor with navigation
248
- total_docs = len(documents)
249
- doc = documents[st.session_state.current_index]
250
- st.markdown(f"#### Document ID: {doc.get('id', '')}")
251
- doc_str = st.text_area("Edit Document", value=json.dumps(doc, indent=2), height=300, key=f'code_editor_{st.session_state.current_index}')
252
- col_prev, col_next = st.columns([1, 1])
253
- with col_prev:
254
- if st.button("โฌ…๏ธ Previous"):
255
- if st.session_state.current_index > 0:
256
- st.session_state.current_index -= 1
257
- st.rerun()
258
- with col_next:
259
- if st.button("โžก๏ธ Next"):
260
- if st.session_state.current_index < total_docs - 1:
261
- st.session_state.current_index += 1
262
- st.rerun()
263
- if st.button("๐Ÿ’พ Save Changes", key=f'save_button_{st.session_state.current_index}'):
264
- try:
265
- updated_doc = json.loads(doc_str)
266
- success, message = update_record(container, updated_doc)
267
- if success:
268
- st.success(f"Document {updated_doc['id']} saved successfully.")
269
- st.session_state.selected_document_id = updated_doc['id']
270
- st.rerun()
271
- else:
272
- st.error(message)
273
- except json.JSONDecodeError as e:
274
- st.error(f"Invalid JSON: {str(e)} ๐Ÿšซ")
275
- elif selected_view == 'Show as Edit and Save':
276
- # โœ๏ธ Show as Edit and Save in columns
277
- st.markdown("#### Edit the document fields below:")
278
-
279
- # Create columns for each document
280
- num_cols = len(documents_to_display)
281
- cols = st.columns(num_cols)
282
-
283
- for idx, (col, doc) in enumerate(zip(cols, documents_to_display)):
284
- with col:
285
- st.markdown(f"##### Document ID: {doc.get('id', '')}")
286
- editable_id = st.text_input("ID", value=doc.get('id', ''), key=f'edit_id_{idx}')
287
- # Remove 'id' from the document for editing other fields
288
- editable_doc = doc.copy()
289
- editable_doc.pop('id', None)
290
- doc_str = st.text_area("Document Content (in JSON format)", value=json.dumps(editable_doc, indent=2), height=300, key=f'doc_str_{idx}')
291
- if st.button("๐Ÿ’พ Save Changes", key=f'save_button_{idx}'):
292
- try:
293
- updated_doc = json.loads(doc_str)
294
- updated_doc['id'] = editable_id # Include the possibly edited ID
295
- success, message = update_record(container, updated_doc)
296
- if success:
297
- st.success(f"Document {updated_doc['id']} saved successfully.")
298
- st.session_state.selected_document_id = updated_doc['id']
299
- st.rerun()
300
- else:
301
- st.error(message)
302
- except json.JSONDecodeError as e:
303
- st.error(f"Invalid JSON: {str(e)} ๐Ÿšซ")
304
- elif selected_view == 'Clone Document':
305
- # ๐Ÿงฌ Clone Document per record
306
- st.markdown("#### Clone a document:")
307
- for idx, doc in enumerate(documents_to_display):
308
- st.markdown(f"##### Document ID: {doc.get('id', '')}")
309
- if st.button("๐Ÿ“„ Clone Document", key=f'clone_button_{idx}'):
310
- cloned_doc = doc.copy()
311
- # Generate a unique ID
312
- cloned_doc['id'] = generate_unique_id()
313
- st.session_state.cloned_doc = cloned_doc
314
- st.session_state.cloned_doc_str = json.dumps(cloned_doc, indent=2)
315
- st.session_state.clone_mode = True
316
- st.rerun()
317
- if st.session_state.get('clone_mode', False):
318
- st.markdown("#### Edit Cloned Document:")
319
- cloned_doc_str = st.text_area("Cloned Document Content (in JSON format)", value=st.session_state.cloned_doc_str, height=300)
320
- if st.button("๐Ÿ’พ Save Cloned Document"):
321
- try:
322
- new_doc = json.loads(cloned_doc_str)
323
- success, message = insert_record(container, new_doc)
324
- if success:
325
- st.success(f"Cloned document saved with id: {new_doc['id']} ๐ŸŽ‰")
326
- st.session_state.selected_document_id = new_doc['id']
327
- st.session_state.clone_mode = False
328
- st.session_state.cloned_doc = None
329
- st.session_state.cloned_doc_str = ''
330
- st.rerun()
331
- else:
332
- st.error(message)
333
- except json.JSONDecodeError as e:
334
- st.error(f"Invalid JSON: {str(e)} ๐Ÿšซ")
335
- elif selected_view == 'New Record':
336
- # ๐Ÿ†• New Record
337
- st.markdown("#### Create a new document:")
338
- new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
339
- new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
340
- if st.button("โž• Create New Document"):
341
- try:
342
- new_doc = json.loads(new_doc_str)
343
- new_doc['id'] = new_id # Use the provided ID
344
- success, message = insert_record(container, new_doc)
345
- if success:
346
- st.success(f"New document created with id: {new_doc['id']} ๐ŸŽ‰")
347
- st.session_state.selected_document_id = new_doc['id']
348
- # Switch to 'Show as Edit and Save' mode
349
- st.rerun()
350
- else:
351
- st.error(message)
352
- except json.JSONDecodeError as e:
353
- st.error(f"Invalid JSON: {str(e)} ๐Ÿšซ")
354
- else:
355
- st.sidebar.info("No documents found in this container. ๐Ÿ“ญ")
356
-
357
- # ๐ŸŽ‰ Main content area
358
- st.subheader(f"๐Ÿ“Š Container: {st.session_state.selected_container}")
359
- if st.session_state.selected_container:
360
- if documents_to_display:
361
- df = pd.DataFrame(documents_to_display)
362
- st.dataframe(df)
363
- else:
364
- st.info("No documents to display. ๐Ÿง")
365
-
366
- # ๐Ÿ™ GitHub section
367
- st.subheader("๐Ÿ™ GitHub Operations")
368
- github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
369
- source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
370
- new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
371
-
372
- col1, col2 = st.columns(2)
373
- with col1:
374
- if st.button("๐Ÿ“ฅ Clone Repository"):
375
- if github_token and source_repo:
376
- try:
377
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
378
- download_github_repo(source_repo, local_path)
379
- zip_filename = f"{new_repo_name}.zip"
380
- create_zip_file(local_path, zip_filename[:-4])
381
- st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
382
- st.success("Repository cloned successfully! ๐ŸŽ‰")
383
- except Exception as e:
384
- st.error(f"An error occurred: {str(e)} ๐Ÿ˜ข")
385
- finally:
386
- if os.path.exists(local_path):
387
- shutil.rmtree(local_path)
388
- if os.path.exists(zip_filename):
389
- os.remove(zip_filename)
390
- else:
391
- st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. ๐Ÿ”‘โ“")
392
-
393
- with col2:
394
- if st.button("๐Ÿ“ค Push to New Repository"):
395
- if github_token and source_repo:
396
- try:
397
- g = Github(github_token)
398
- new_repo = create_repo(g, new_repo_name)
399
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
400
- download_github_repo(source_repo, local_path)
401
- push_to_github(local_path, new_repo, github_token)
402
- st.success(f"Repository pushed successfully to {new_repo.html_url} ๐Ÿš€")
403
- except Exception as e:
404
- st.error(f"An error occurred: {str(e)} ๐Ÿ˜ข")
405
- finally:
406
- if os.path.exists(local_path):
407
- shutil.rmtree(local_path)
408
- else:
409
- st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. ๐Ÿ”‘โ“")
410
-
411
- except exceptions.CosmosHttpResponseError as e:
412
- st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} ๐Ÿšจ")
413
- except Exception as e:
414
- st.error(f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ")
415
-
416
- # ๐Ÿšช Logout button
417
- if st.session_state.logged_in and st.sidebar.button("๐Ÿšช Logout"):
418
- st.session_state.logged_in = False
419
- st.session_state.selected_records.clear()
420
- st.session_state.client = None
421
- st.session_state.selected_database = None
422
- st.session_state.selected_container = None
423
- st.session_state.selected_document_id = None
424
- st.session_state.current_index = 0
425
- st.rerun()
426
-
427
- if __name__ == "__main__":
428
- main()