awacke1 commited on
Commit
29bebf5
ยท
verified ยท
1 Parent(s): 86a8c22

Delete backup.v1.101624.app.py

Browse files
Files changed (1) hide show
  1. backup.v1.101624.app.py +0 -377
backup.v1.101624.app.py DELETED
@@ -1,377 +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=1000):
82
- # ๐Ÿ“ Retrieving documents. Shhh, don't tell anyone!
83
- query = "SELECT * FROM c"
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
-
169
- # ๐Ÿ” Automatic Login
170
- if Key:
171
- st.session_state.primary_key = Key
172
- st.session_state.logged_in = True
173
- else:
174
- st.error("Cosmos DB Key is not set in environment variables. ๐Ÿ”‘โŒ")
175
- return # Can't proceed without a key
176
-
177
- if st.session_state.logged_in:
178
- # ๐ŸŒŒ Initialize Cosmos DB client
179
- try:
180
- if st.session_state.client is None:
181
- st.session_state.client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
182
-
183
- # ๐Ÿ—„๏ธ Sidebar for database, container, and document selection
184
- st.sidebar.title("๐Ÿ™Git๐ŸŒŒCosmos๐Ÿ’ซ๐Ÿ—„๏ธNavigator")
185
-
186
- databases = get_databases(st.session_state.client)
187
- selected_db = st.sidebar.selectbox("๐Ÿ—ƒ๏ธ Select Database", databases)
188
-
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.session_state.selected_document_id = None
193
- st.rerun()
194
-
195
- if st.session_state.selected_database:
196
- database = st.session_state.client.get_database_client(st.session_state.selected_database)
197
- containers = get_containers(database)
198
- selected_container = st.sidebar.selectbox("๐Ÿ“ Select Container", containers)
199
-
200
- if selected_container != st.session_state.selected_container:
201
- st.session_state.selected_container = selected_container
202
- st.session_state.selected_document_id = None
203
- st.rerun()
204
-
205
- if st.session_state.selected_container:
206
- container = database.get_container_client(st.session_state.selected_container)
207
-
208
- # ๐Ÿ“ฆ Add Export button
209
- if st.button("๐Ÿ“ฆ Export Container Data"):
210
- download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
211
- if download_link.startswith('<a'):
212
- st.markdown(download_link, unsafe_allow_html=True)
213
- else:
214
- st.error(download_link)
215
-
216
- limit_to_1000 = st.sidebar.checkbox("๐Ÿ”ข Limit to top 1000 documents", value=True)
217
- documents = get_documents(container, limit=1000 if limit_to_1000 else None)
218
-
219
- if documents:
220
- document_ids = [doc.get('id', 'Unknown') for doc in documents]
221
- 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)
222
-
223
- if selected_document != st.session_state.selected_document_id:
224
- st.session_state.selected_document_id = selected_document
225
- st.rerun()
226
-
227
- if st.session_state.selected_document_id:
228
- st.subheader(f"๐Ÿ“„ Document Details: {st.session_state.selected_document_id}")
229
- selected_doc = next((doc for doc in documents if doc.get('id') == st.session_state.selected_document_id), None)
230
- if selected_doc:
231
- # ๐ŸŽจ Add Viewer/Editor selection
232
- view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
233
- selected_view = st.selectbox("Select Viewer/Editor", view_options)
234
-
235
- if selected_view == 'Show as Markdown':
236
- # ๐Ÿ–Œ๏ธ Show as Markdown
237
- items = get_documents(container)
238
- for item in items:
239
- st.markdown(f"### **๐Ÿ†” ID: {item.get('id', 'Unknown')}**")
240
- content = item.get('content', '')
241
- if isinstance(content, dict) or isinstance(content, list):
242
- content = json.dumps(content, indent=2)
243
- st.markdown(content)
244
- elif selected_view == 'Show as Code Editor':
245
- # ๐Ÿ’ป Show as Code Editor
246
- items = get_documents(container)
247
- for item in items:
248
- st.markdown(f"### **๐Ÿ†” ID: {item.get('id', 'Unknown')}**")
249
- st.code(json.dumps(item, indent=2), language='python')
250
- elif selected_view == 'Show as Edit and Save':
251
- # โœ๏ธ Show as Edit and Save
252
- st.markdown("#### Edit the document fields below:")
253
- editable_id = st.text_input("ID", value=selected_doc.get('id', ''), key='edit_id')
254
- # Remove 'id' from the document for editing other fields
255
- editable_doc = selected_doc.copy()
256
- editable_doc.pop('id', None)
257
- doc_str = st.text_area("Document Content (in JSON format)", value=json.dumps(editable_doc, indent=2), height=300)
258
- if st.button("๐Ÿ’พ Save Changes"):
259
- try:
260
- updated_doc = json.loads(doc_str)
261
- updated_doc['id'] = editable_id # Include the possibly edited ID
262
- success, message = update_record(container, updated_doc)
263
- if success:
264
- st.success(message)
265
- st.session_state.selected_document_id = updated_doc['id']
266
- st.rerun()
267
- else:
268
- st.error(message)
269
- except json.JSONDecodeError as e:
270
- st.error(f"Invalid JSON: {str(e)} ๐Ÿšซ")
271
- elif selected_view == 'Clone Document':
272
- # ๐Ÿงฌ Clone Document
273
- if st.button("๐Ÿ“„ Clone Document"):
274
- cloned_doc = selected_doc.copy()
275
- # Generate a unique ID
276
- cloned_doc['id'] = generate_unique_id()
277
- success, message = insert_record(container, cloned_doc)
278
- if success:
279
- st.success(f"Document cloned with new id: {cloned_doc['id']} ๐ŸŽ‰")
280
- st.session_state.selected_document_id = cloned_doc['id']
281
- # Switch to 'Show as Edit and Save' mode
282
- st.rerun()
283
- else:
284
- st.error(message)
285
- elif selected_view == 'New Record':
286
- # ๐Ÿ†• New Record
287
- st.markdown("#### Create a new document:")
288
- new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
289
- new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
290
- if st.button("โž• Create New Document"):
291
- try:
292
- new_doc = json.loads(new_doc_str)
293
- new_doc['id'] = new_id # Use the provided ID
294
- success, message = insert_record(container, new_doc)
295
- if success:
296
- st.success(f"New document created with id: {new_doc['id']} ๐ŸŽ‰")
297
- st.session_state.selected_document_id = new_doc['id']
298
- # Switch to 'Show as Edit and Save' mode
299
- st.rerun()
300
- else:
301
- st.error(message)
302
- except json.JSONDecodeError as e:
303
- st.error(f"Invalid JSON: {str(e)} ๐Ÿšซ")
304
- else:
305
- st.sidebar.info("No documents found in this container. ๐Ÿ“ญ")
306
-
307
- # ๐ŸŽ‰ Main content area
308
- st.subheader(f"๐Ÿ“Š Container: {st.session_state.selected_container}")
309
- if st.session_state.selected_container:
310
- if documents:
311
- df = pd.DataFrame(documents)
312
- st.dataframe(df)
313
- else:
314
- st.info("No documents to display. ๐Ÿง")
315
-
316
- # ๐Ÿ™ GitHub section
317
- st.subheader("๐Ÿ™ GitHub Operations")
318
- github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
319
- source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
320
- new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
321
-
322
- col1, col2 = st.columns(2)
323
- with col1:
324
- if st.button("๐Ÿ“ฅ Clone Repository"):
325
- if github_token and source_repo:
326
- try:
327
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d%H%M%S')}"
328
- download_github_repo(source_repo, local_path)
329
- zip_filename = f"{new_repo_name}.zip"
330
- create_zip_file(local_path, zip_filename[:-4])
331
- st.markdown(get_base64_download_link(zip_filename, zip_filename), unsafe_allow_html=True)
332
- st.success("Repository cloned successfully! ๐ŸŽ‰")
333
- except Exception as e:
334
- st.error(f"An error occurred: {str(e)} ๐Ÿ˜ข")
335
- finally:
336
- if os.path.exists(local_path):
337
- shutil.rmtree(local_path)
338
- if os.path.exists(zip_filename):
339
- os.remove(zip_filename)
340
- else:
341
- st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. ๐Ÿ”‘โ“")
342
-
343
- with col2:
344
- if st.button("๐Ÿ“ค Push to New Repository"):
345
- if github_token and source_repo:
346
- try:
347
- g = Github(github_token)
348
- new_repo = create_repo(g, new_repo_name)
349
- local_path = f"./temp_repo_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
350
- download_github_repo(source_repo, local_path)
351
- push_to_github(local_path, new_repo, github_token)
352
- st.success(f"Repository pushed successfully to {new_repo.html_url} ๐Ÿš€")
353
- except Exception as e:
354
- st.error(f"An error occurred: {str(e)} ๐Ÿ˜ข")
355
- finally:
356
- if os.path.exists(local_path):
357
- shutil.rmtree(local_path)
358
- else:
359
- st.error("Please ensure GitHub token is set in environment variables and source repository URL is provided. ๐Ÿ”‘โ“")
360
-
361
- except exceptions.CosmosHttpResponseError as e:
362
- st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)} ๐Ÿšจ")
363
- except Exception as e:
364
- st.error(f"An unexpected error occurred: {str(e)} ๐Ÿ˜ฑ")
365
-
366
- # ๐Ÿšช Logout button
367
- if st.session_state.logged_in and st.sidebar.button("๐Ÿšช Logout"):
368
- st.session_state.logged_in = False
369
- st.session_state.selected_records.clear()
370
- st.session_state.client = None
371
- st.session_state.selected_database = None
372
- st.session_state.selected_container = None
373
- st.session_state.selected_document_id = None
374
- st.rerun()
375
-
376
- if __name__ == "__main__":
377
- main()