Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ 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 |
|
|
@@ -10,86 +11,26 @@ import glob
|
|
| 10 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
| 11 |
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
|
| 12 |
|
| 13 |
-
# Initialize Azure
|
| 14 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
| 15 |
-
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
|
| 16 |
-
|
| 17 |
-
@st.cache_data
|
| 18 |
-
def get_cosmos_client():
|
| 19 |
-
return cosmos_client
|
| 20 |
-
|
| 21 |
-
def get_database_client(db_name):
|
| 22 |
-
cosmos_client = get_cosmos_client()
|
| 23 |
-
return cosmos_client.get_database_client(db_name)
|
| 24 |
-
|
| 25 |
-
def get_container_client(db_name, container_name):
|
| 26 |
-
database_client = get_database_client(db_name)
|
| 27 |
-
return database_client.get_container_client(container_name)
|
| 28 |
-
|
| 29 |
-
def list_databases():
|
| 30 |
-
cosmos_client = get_cosmos_client()
|
| 31 |
-
return list(cosmos_client.list_databases())
|
| 32 |
-
|
| 33 |
-
def list_containers(db_name):
|
| 34 |
-
database_client = get_database_client(db_name)
|
| 35 |
-
return list(database_client.list_containers())
|
| 36 |
-
|
| 37 |
-
def list_items(db_name, container_name):
|
| 38 |
-
container_client = get_container_client(db_name, container_name)
|
| 39 |
-
return list(container_client.read_all_items())
|
| 40 |
-
|
| 41 |
-
def create_item(db_name, container_name, item_data):
|
| 42 |
-
container_client = get_container_client(db_name, container_name)
|
| 43 |
-
container_client.create_item(body=item_data)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
try:
|
| 48 |
-
item = container_client.read_item(item_id, partition_key=item_id)
|
| 49 |
-
for key in new_data:
|
| 50 |
-
item[key] = new_data[key]
|
| 51 |
-
container_client.replace_item(item_id, item)
|
| 52 |
-
except CosmosResourceNotFoundError:
|
| 53 |
-
st.error("Item not found for update")
|
| 54 |
-
|
| 55 |
-
def delete_item(db_name, container_name, item_id):
|
| 56 |
-
container_client = get_container_client(db_name, container_name)
|
| 57 |
-
try:
|
| 58 |
-
container_client.delete_item(item_id, partition_key=item_id)
|
| 59 |
-
except CosmosResourceNotFoundError:
|
| 60 |
-
st.error("Item not found for deletion")
|
| 61 |
|
|
|
|
| 62 |
def display_cosmos_db_structure():
|
| 63 |
st.subheader('Azure Cosmos DB Structure')
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
databases = list_databases()
|
| 67 |
-
if not databases:
|
| 68 |
-
st.error("No databases found.")
|
| 69 |
-
return
|
| 70 |
-
|
| 71 |
-
# Iterating through databases
|
| 72 |
-
for db_properties in databases:
|
| 73 |
db_name = db_properties['id']
|
| 74 |
st.markdown(f"#### Database: {db_name}")
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
if not containers:
|
| 79 |
-
st.markdown("No containers found in this database.")
|
| 80 |
-
continue
|
| 81 |
-
|
| 82 |
-
# Iterating through containers
|
| 83 |
-
for container_properties in containers:
|
| 84 |
container_name = container_properties['id']
|
| 85 |
st.markdown(f"- **Container**: {container_name}")
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
items = list_items(db_name, container_name)
|
| 89 |
-
if not items:
|
| 90 |
-
st.markdown(" - No items in this container.")
|
| 91 |
-
continue
|
| 92 |
-
|
| 93 |
for item in items:
|
| 94 |
item_desc = f" - Item: `{item['id']}`"
|
| 95 |
if 'file_name' in item and item['file_name'].endswith('.png'):
|
|
@@ -98,68 +39,53 @@ def display_cosmos_db_structure():
|
|
| 98 |
else:
|
| 99 |
st.markdown(item_desc)
|
| 100 |
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
db_properties = next(cosmos_client.list_databases(), None)
|
| 105 |
-
if
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
if st.button(f"🗑️ Delete {item_id}", key=f"delete_{item_id}"):
|
| 128 |
-
delete_item(db_name, container_name, item_id)
|
| 129 |
-
st.success(f"Deleted Item: {item_id}")
|
| 130 |
-
|
| 131 |
-
# Add or Update PNG files
|
| 132 |
-
st.subheader("Add or Update PNG Images")
|
| 133 |
-
png_files = glob.glob('*.png')
|
| 134 |
-
for file_name in png_files:
|
| 135 |
-
item_id = os.path.splitext(file_name)[0]
|
| 136 |
-
item_data = {"id": item_id, "file_name": file_name}
|
| 137 |
-
if item_id not in existing_ids:
|
| 138 |
-
if st.button(f"➕ Add {item_id}", key=f"add_{item_id}"):
|
| 139 |
-
create_item(db_name, container_name, item_data)
|
| 140 |
-
st.success(f"Added Item: {item_id}")
|
| 141 |
-
else:
|
| 142 |
-
if st.button(f"✏️ Update {item_id}", key=f"update_{item_id}"):
|
| 143 |
-
update_item(db_name, container_name, item_id, {"file_name": file_name})
|
| 144 |
-
st.success(f"Updated Item: {item_id}")
|
| 145 |
-
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
-
if st.button('
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
blobs = list_blobs_in_container(blob_container)
|
| 162 |
-
for blob in blobs:
|
| 163 |
-
st.write(blob.name)
|
| 164 |
|
| 165 |
-
|
|
|
|
|
|
|
|
|
| 3 |
from azure.cosmos import CosmosClient, PartitionKey
|
| 4 |
from azure.storage.blob import BlobServiceClient
|
| 5 |
from azure.cosmos.exceptions import CosmosResourceNotFoundError
|
| 6 |
+
|
| 7 |
import requests
|
| 8 |
import glob
|
| 9 |
|
|
|
|
| 11 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
| 12 |
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
|
| 13 |
|
| 14 |
+
# Initialize Azure Cosmos DB Client
|
| 15 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Initialize Azure Blob Storage Client
|
| 18 |
+
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Function to Retrieve and Display Cosmos DB Structure
|
| 21 |
def display_cosmos_db_structure():
|
| 22 |
st.subheader('Azure Cosmos DB Structure')
|
| 23 |
+
db_properties = next(cosmos_client.list_databases(), None)
|
| 24 |
+
if db_properties:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
db_name = db_properties['id']
|
| 26 |
st.markdown(f"#### Database: {db_name}")
|
| 27 |
+
database_client = cosmos_client.get_database_client(db_name)
|
| 28 |
+
container_properties = next(database_client.list_containers(), None)
|
| 29 |
+
if container_properties:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
container_name = container_properties['id']
|
| 31 |
st.markdown(f"- **Container**: {container_name}")
|
| 32 |
+
container_client = database_client.get_container_client(container_name)
|
| 33 |
+
items = list(container_client.read_all_items())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
for item in items:
|
| 35 |
item_desc = f" - Item: `{item['id']}`"
|
| 36 |
if 'file_name' in item and item['file_name'].endswith('.png'):
|
|
|
|
| 39 |
else:
|
| 40 |
st.markdown(item_desc)
|
| 41 |
|
| 42 |
+
# Button to Trigger Display of Cosmos DB Structure
|
| 43 |
+
if st.button('Show Cosmos DB Structure'):
|
| 44 |
+
display_cosmos_db_structure()
|
| 45 |
|
| 46 |
+
# Function to Add or Update PNG Images
|
| 47 |
+
def add_or_update_png_images():
|
| 48 |
db_properties = next(cosmos_client.list_databases(), None)
|
| 49 |
+
if db_properties:
|
| 50 |
+
db_name = db_properties['id']
|
| 51 |
+
database_client = cosmos_client.get_database_client(db_name)
|
| 52 |
+
container_properties = next(database_client.list_containers(), None)
|
| 53 |
+
if container_properties:
|
| 54 |
+
container_name = container_properties['id']
|
| 55 |
+
container_client = database_client.get_container_client(container_name)
|
| 56 |
+
existing_items = list(container_client.read_all_items())
|
| 57 |
+
existing_ids = {item['id'] for item in existing_items}
|
| 58 |
+
|
| 59 |
+
# Add or update PNG files from directory
|
| 60 |
+
png_files = glob.glob('*.png')
|
| 61 |
+
for file_name in png_files:
|
| 62 |
+
item_id = os.path.splitext(file_name)[0]
|
| 63 |
+
item_data = {"id": item_id, "file_name": file_name}
|
| 64 |
+
if item_id not in existing_ids:
|
| 65 |
+
container_client.create_item(body=item_data)
|
| 66 |
+
st.write(f"Added Item: {item_id}")
|
| 67 |
+
else:
|
| 68 |
+
if st.button(f"🗑️ Delete {item_id}", key=f"delete_{item_id}"):
|
| 69 |
+
container_client.delete_item(item=item_data, partition_key=item_id)
|
| 70 |
+
st.write(f"Item already exists: {item_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# UI to Add or Update PNG Images
|
| 73 |
+
if st.button('Manage PNG Images'):
|
| 74 |
+
add_or_update_png_images()
|
| 75 |
|
| 76 |
+
# Azure Blob Storage - Upload/Download
|
| 77 |
+
st.subheader('Azure Blob Storage - Upload/Download')
|
| 78 |
+
blob_container = st.text_input('Blob Container')
|
| 79 |
+
blob_file = st.file_uploader('Upload file to Blob')
|
| 80 |
|
| 81 |
+
if blob_file is not None and st.button('Upload to Blob'):
|
| 82 |
+
blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
|
| 83 |
+
blob_client.upload_blob(blob_file.getvalue())
|
| 84 |
|
| 85 |
+
# Azure Functions - Trigger
|
| 86 |
+
st.subheader('Azure Functions - Trigger')
|
| 87 |
+
function_url = st.text_input('Function URL')
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
if st.button('Call Azure Function'):
|
| 90 |
+
response = requests.get(function_url)
|
| 91 |
+
st.write('Function Response:', response.text)
|