Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
from azure.cosmos import CosmosClient, PartitionKey | |
from azure.storage.blob import BlobServiceClient | |
from azure.cosmos.exceptions import CosmosResourceNotFoundError | |
import requests | |
import glob | |
from datetime import datetime | |
# Initialize Azure Clients | |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING') | |
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING') | |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING) | |
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING) | |
# Function to Delete All Items in a Container | |
def delete_all_items_in_container(db_name, container_name): | |
database_client = cosmos_client.get_database_client(db_name) | |
container_client = database_client.get_container_client(container_name) | |
for item in container_client.read_all_items(): | |
try: | |
partition_key = '/id' | |
container_client.delete_item(item=item['id'], partition_key=partition_key) | |
st.write(f"Deleted Item: {item['id']}") | |
except CosmosResourceNotFoundError: | |
st.error(f"Item not found: {item['id']}") | |
# Display and Manage Cosmos DB Structure | |
def display_and_manage_cosmos_db(): | |
st.subheader('Azure Cosmos DB Structure') | |
for db_properties in cosmos_client.list_databases(): | |
db_name = db_properties['id'] | |
st.markdown(f"#### Database: {db_name}") | |
database_client = cosmos_client.get_database_client(db_name) | |
for container_properties in database_client.list_containers(): | |
container_name = container_properties['id'] | |
st.markdown(f"- **Container**: {container_name}") | |
container_client = database_client.get_container_client(container_name) | |
for item in container_client.read_all_items(): | |
item_desc = f" - Item: `{item['id']}`" | |
st.markdown(item_desc) | |
if 'file_name' in item: | |
st.image(item['file_name']) | |
# Update and Delete buttons for each item | |
if st.button(f"🗑️ Delete {item['id']}", key=f"delete_{item['id']}"): | |
partition_key = '/id' | |
container_client.delete_item(item=item['id'], partition_key=partition_key) | |
st.success(f"Deleted Item: {item['id']}") | |
# Insert PNG Images with Unique Identifiers | |
def insert_png_images_with_unique_ids(): | |
db_name = st.selectbox("Select Database", [db['id'] for db in cosmos_client.list_databases()]) | |
container_name = st.selectbox("Select Container", [container['id'] for container in cosmos_client.get_database_client(db_name).list_containers()]) | |
container_client = cosmos_client.get_database_client(db_name).get_container_client(container_name) | |
png_files = glob.glob('*.png') | |
for file_name in png_files: | |
unique_id = f"{os.path.splitext(file_name)[0]}_{datetime.now().strftime('%Y%m%d%H%M%S')}" | |
item_data = {"id": unique_id, "file_name": file_name} | |
container_client.create_item(body=item_data) | |
st.write(f"Inserted Item: {unique_id}") | |
# Azure Blob Storage - Upload/Download | |
st.subheader('Azure Blob Storage - Upload/Download') | |
blob_container = st.text_input('Blob Container') | |
blob_file = st.file_uploader('Upload file to Blob') | |
if blob_file is not None and st.button('Upload to Blob'): | |
blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name) | |
blob_client.upload_blob(blob_file.getvalue()) | |
st.success('File uploaded successfully.') | |
# Azure Functions - Trigger | |
st.subheader('Azure Functions - Trigger') | |
function_url = st.text_input('Function URL') | |
if st.button('Call Azure Function'): | |
response = requests.get(function_url) | |
st.write('Function Response:', response.text) | |
# Display Cosmos DB Structure and Manage Items | |
display_and_manage_cosmos_db() | |
# Button to Insert PNG Images with Unique Identifiers | |
if st.button('Insert PNG Images with Unique IDs'): | |
insert_png_images_with_unique_ids() | |
# Button to Delete All Items in a Container | |
db_name_to_delete = st.selectbox("Select Database to Delete From", [db['id'] for db in cosmos_client.list_databases()]) | |
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()]) | |
if st.button('Delete All Items in Container'): | |
delete_all_items_in_container(db_name_to_delete, container_name_to_delete) | |
st.success("All items deleted successfully.") | |