awacke1 commited on
Commit
e13a1c7
1 Parent(s): bcd4e08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -39
app.py CHANGED
@@ -1,53 +1,100 @@
1
  import os
2
- import pandas as pd
3
  import streamlit as st
4
- from azure.cosmos import CosmosClient
 
5
  from azure.cosmos.exceptions import CosmosResourceNotFoundError
 
 
 
6
 
7
- # Initialize Azure Cosmos DB Client
8
  COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
 
9
  cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
 
10
 
11
- # Function to Delete an Item in Cosmos DB
12
- def delete_cosmos_item(db_name, container_name, item_id):
13
- try:
14
- database_client = cosmos_client.get_database_client(db_name)
15
- container_client = database_client.get_container_client(container_name)
16
- container_client.delete_item(item=item_id, partition_key='id')
17
- st.success(f"Deleted Item: {item_id}")
18
- except CosmosResourceNotFoundError:
19
- st.error(f"Item not found: {item_id}")
20
-
21
- # Load Cosmos DB Data into a DataFrame
22
- def load_cosmos_data_into_dataframe():
23
- data = []
 
 
 
 
24
  for db_properties in cosmos_client.list_databases():
25
  db_name = db_properties['id']
 
26
  database_client = cosmos_client.get_database_client(db_name)
 
27
  for container_properties in database_client.list_containers():
28
  container_name = container_properties['id']
 
29
  container_client = database_client.get_container_client(container_name)
 
30
  for item in container_client.read_all_items():
31
- data.append(item)
32
- return pd.DataFrame(data)
33
-
34
- # Display and Manage Cosmos DB Data
35
- def display_and_manage_cosmos_data(df):
36
- st.subheader('Azure Cosmos DB Data')
37
- for index, row in df.iterrows():
38
- st.json(row.to_json()) # Display the full data of the item
39
- if 'file_name' in row and os.path.isfile(row['file_name']):
40
- st.image(row['file_name']) # Show image if available
41
- delete_button_key = f"delete_{row['id']}"
42
- if st.button(f"🗑️ Delete {row['id']}", key=delete_button_key):
43
- delete_cosmos_item(row['_database_id'], row['_container_id'], row['id'])
44
- st.experimental_rerun()
45
-
46
- # Main UI Function
47
- def main():
48
- df = load_cosmos_data_into_dataframe()
49
- display_and_manage_cosmos_data(df)
50
-
51
- # Run the main function
52
- if __name__ == "__main__":
53
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
 
2
  import streamlit as st
3
+ from azure.cosmos import CosmosClient, PartitionKey
4
+ from azure.storage.blob import BlobServiceClient
5
  from azure.cosmos.exceptions import CosmosResourceNotFoundError
6
+ import requests
7
+ import glob
8
+ from datetime import datetime
9
 
10
+ # Initialize Azure Clients
11
  COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
12
+ BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
13
  cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
14
+ blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
15
 
16
+ # Function to Delete All Items in a Container
17
+ def delete_all_items_in_container(db_name, container_name):
18
+ database_client = cosmos_client.get_database_client(db_name)
19
+ container_client = database_client.get_container_client(container_name)
20
+
21
+ for item in container_client.read_all_items():
22
+ try:
23
+ partition_key = '/id'
24
+ container_client.delete_item(item=item['id'], partition_key=partition_key)
25
+ st.write(f"Deleted Item: {item['id']}")
26
+ except CosmosResourceNotFoundError:
27
+ st.error(f"Item not found: {item['id']}")
28
+
29
+
30
+ # Display and Manage Cosmos DB Structure
31
+ def display_and_manage_cosmos_db():
32
+ st.subheader('Azure Cosmos DB Structure')
33
  for db_properties in cosmos_client.list_databases():
34
  db_name = db_properties['id']
35
+ st.markdown(f"#### Database: {db_name}")
36
  database_client = cosmos_client.get_database_client(db_name)
37
+
38
  for container_properties in database_client.list_containers():
39
  container_name = container_properties['id']
40
+ st.markdown(f"- **Container**: {container_name}")
41
  container_client = database_client.get_container_client(container_name)
42
+
43
  for item in container_client.read_all_items():
44
+ item_desc = f" - Item: `{item['id']}`"
45
+ st.markdown(item_desc)
46
+ if 'file_name' in item:
47
+ st.image(item['file_name'])
48
+
49
+ # Update and Delete buttons for each item
50
+ if st.button(f"🗑️ Delete {item['id']}", key=f"delete_{item['id']}"):
51
+ partition_key = '/id'
52
+ container_client.delete_item(item=item['id'], partition_key=partition_key)
53
+ st.success(f"Deleted Item: {item['id']}")
54
+
55
+ # Insert PNG Images with Unique Identifiers
56
+ def insert_png_images_with_unique_ids():
57
+ db_name = st.selectbox("Select Database", [db['id'] for db in cosmos_client.list_databases()])
58
+ container_name = st.selectbox("Select Container", [container['id'] for container in cosmos_client.get_database_client(db_name).list_containers()])
59
+ container_client = cosmos_client.get_database_client(db_name).get_container_client(container_name)
60
+
61
+ png_files = glob.glob('*.png')
62
+ for file_name in png_files:
63
+ unique_id = f"{os.path.splitext(file_name)[0]}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
64
+ item_data = {"id": unique_id, "file_name": file_name}
65
+ container_client.create_item(body=item_data)
66
+ st.write(f"Inserted Item: {unique_id}")
67
+
68
+ # Azure Blob Storage - Upload/Download
69
+ st.subheader('Azure Blob Storage - Upload/Download')
70
+ blob_container = st.text_input('Blob Container')
71
+ blob_file = st.file_uploader('Upload file to Blob')
72
+
73
+ if blob_file is not None and st.button('Upload to Blob'):
74
+ blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
75
+ blob_client.upload_blob(blob_file.getvalue())
76
+ st.success('File uploaded successfully.')
77
+
78
+ # Azure Functions - Trigger
79
+ st.subheader('Azure Functions - Trigger')
80
+ function_url = st.text_input('Function URL')
81
+
82
+ if st.button('Call Azure Function'):
83
+ response = requests.get(function_url)
84
+ st.write('Function Response:', response.text)
85
+
86
+
87
+ # Display Cosmos DB Structure and Manage Items
88
+ display_and_manage_cosmos_db()
89
+
90
+ # Button to Insert PNG Images with Unique Identifiers
91
+ if st.button('Insert PNG Images with Unique IDs'):
92
+ insert_png_images_with_unique_ids()
93
+
94
+ # Button to Delete All Items in a Container
95
+ db_name_to_delete = st.selectbox("Select Database to Delete From", [db['id'] for db in cosmos_client.list_databases()])
96
+ container_name_to_delete = st.selectbox("Select Container to Delete From", [container['id'] for container in cosmos_client.get_database_client(db_name_to_delete).list_containers()])
97
+
98
+ if st.button('Delete All Items in Container'):
99
+ delete_all_items_in_container(db_name_to_delete, container_name_to_delete)
100
+ st.success("All items deleted successfully.")