awacke1 commited on
Commit
761fac5
β€’
1 Parent(s): db0924b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Set page config
17
+ st.set_page_config(page_title="Azure Manager", page_icon="☁️", layout="wide")
18
+
19
+ # Function to Delete All Items in a Container
20
+ def delete_all_items_in_container(db_name, container_name):
21
+ database_client = cosmos_client.get_database_client(db_name)
22
+ container_client = database_client.get_container_client(container_name)
23
+
24
+ for item in container_client.read_all_items():
25
+ try:
26
+ partition_key = '/id'
27
+ container_client.delete_item(item=item['id'], partition_key=partition_key)
28
+ st.write(f"πŸ—‘οΈ Deleted Item: {item['id']}")
29
+ except CosmosResourceNotFoundError:
30
+ st.error(f"❌ Item not found: {item['id']}")
31
+
32
+ # Display and Manage Cosmos DB Structure
33
+ def display_and_manage_cosmos_db():
34
+ st.header('πŸ“Š Azure Cosmos DB Structure')
35
+ for db_properties in cosmos_client.list_databases():
36
+ db_name = db_properties['id']
37
+ st.subheader(f"πŸ—„οΈ Database: {db_name}")
38
+ database_client = cosmos_client.get_database_client(db_name)
39
+
40
+ for container_properties in database_client.list_containers():
41
+ container_name = container_properties['id']
42
+ st.markdown(f"πŸ“ **Container**: {container_name}")
43
+ container_client = database_client.get_container_client(container_name)
44
+
45
+ for item in container_client.read_all_items():
46
+ col1, col2, col3 = st.columns([3, 1, 1])
47
+ with col1:
48
+ st.markdown(f"πŸ“„ Item: `{item['id']}`")
49
+ if 'file_name' in item:
50
+ st.image(item['file_name'], caption=item['file_name'], width=200)
51
+ with col2:
52
+ if st.button(f"πŸ—‘οΈ Delete", key=f"delete_{item['id']}"):
53
+ partition_key = '/id'
54
+ container_client.delete_item(item=item['id'], partition_key=partition_key)
55
+ st.success(f"βœ… Deleted Item: {item['id']}")
56
+ st.rerun()
57
+
58
+ # Insert PNG Images with Unique Identifiers
59
+ def insert_png_images_with_unique_ids(db_name, container_name):
60
+ container_client = cosmos_client.get_database_client(db_name).get_container_client(container_name)
61
+
62
+ png_files = glob.glob('*.png')
63
+ for file_name in png_files:
64
+ unique_id = f"{os.path.splitext(file_name)[0]}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
65
+ item_data = {"id": unique_id, "file_name": file_name}
66
+ container_client.create_item(body=item_data)
67
+ st.write(f"πŸ“₯ Inserted Item: {unique_id}")
68
+
69
+ # Streamlit UI
70
+ st.title("☁️ Azure Manager")
71
+
72
+ # Sidebar for global actions
73
+ st.sidebar.header("πŸ› οΈ Global Actions")
74
+
75
+ # Azure Blob Storage - Upload/Download
76
+ st.sidebar.subheader('πŸ“€ Azure Blob Storage - Upload')
77
+ blob_container = st.sidebar.text_input('πŸ—‚οΈ Blob Container')
78
+ blob_file = st.sidebar.file_uploader('πŸ“ Upload file to Blob')
79
+
80
+ if blob_file is not None and st.sidebar.button('πŸ“€ Upload to Blob'):
81
+ blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
82
+ blob_client.upload_blob(blob_file.getvalue())
83
+ st.sidebar.success('βœ… File uploaded successfully.')
84
+
85
+ # Azure Functions - Trigger
86
+ st.sidebar.subheader('⚑ Azure Functions - Trigger')
87
+ function_url = st.sidebar.text_input('πŸ”— Function URL')
88
+
89
+ if st.sidebar.button('πŸš€ Call Azure Function'):
90
+ response = requests.get(function_url)
91
+ st.sidebar.write('πŸ“‘ Function Response:', response.text)
92
+
93
+ # Insert PNG Images
94
+ st.sidebar.subheader('πŸ–ΌοΈ Insert PNG Images')
95
+ db_name_insert = st.sidebar.selectbox("πŸ—„οΈ Select Database", [db['id'] for db in cosmos_client.list_databases()], key='db_insert')
96
+ container_name_insert = st.sidebar.selectbox("πŸ“ Select Container", [container['id'] for container in cosmos_client.get_database_client(db_name_insert).list_containers()], key='container_insert')
97
+
98
+ if st.sidebar.button('πŸ“₯ Insert PNG Images with Unique IDs'):
99
+ insert_png_images_with_unique_ids(db_name_insert, container_name_insert)
100
+
101
+ # Delete All Items in a Container
102
+ st.sidebar.subheader('πŸ—‘οΈ Delete All Items')
103
+ db_name_delete = st.sidebar.selectbox("πŸ—„οΈ Select Database to Delete From", [db['id'] for db in cosmos_client.list_databases()], key='db_delete')
104
+ container_name_delete = st.sidebar.selectbox("πŸ“ Select Container to Delete From", [container['id'] for container in cosmos_client.get_database_client(db_name_delete).list_containers()], key='container_delete')
105
+
106
+ if st.sidebar.button('πŸ—‘οΈ Delete All Items in Container'):
107
+ delete_all_items_in_container(db_name_delete, container_name_delete)
108
+ st.sidebar.success("βœ… All items deleted successfully.")
109
+ st.rerun()
110
+
111
+ # Main content
112
+ display_and_manage_cosmos_db()