awacke1's picture
Create app.py
761fac5 verified
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)
# Set page config
st.set_page_config(page_title="Azure Manager", page_icon="☁️", layout="wide")
# 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.header('πŸ“Š Azure Cosmos DB Structure')
for db_properties in cosmos_client.list_databases():
db_name = db_properties['id']
st.subheader(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():
col1, col2, col3 = st.columns([3, 1, 1])
with col1:
st.markdown(f"πŸ“„ Item: `{item['id']}`")
if 'file_name' in item:
st.image(item['file_name'], caption=item['file_name'], width=200)
with col2:
if st.button(f"πŸ—‘οΈ Delete", 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']}")
st.rerun()
# Insert PNG Images with Unique Identifiers
def insert_png_images_with_unique_ids(db_name, container_name):
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}")
# Streamlit UI
st.title("☁️ Azure Manager")
# Sidebar for global actions
st.sidebar.header("πŸ› οΈ Global Actions")
# Azure Blob Storage - Upload/Download
st.sidebar.subheader('πŸ“€ Azure Blob Storage - Upload')
blob_container = st.sidebar.text_input('πŸ—‚οΈ Blob Container')
blob_file = st.sidebar.file_uploader('πŸ“ Upload file to Blob')
if blob_file is not None and st.sidebar.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.sidebar.success('βœ… File uploaded successfully.')
# Azure Functions - Trigger
st.sidebar.subheader('⚑ Azure Functions - Trigger')
function_url = st.sidebar.text_input('πŸ”— Function URL')
if st.sidebar.button('πŸš€ Call Azure Function'):
response = requests.get(function_url)
st.sidebar.write('πŸ“‘ Function Response:', response.text)
# Insert PNG Images
st.sidebar.subheader('πŸ–ΌοΈ Insert PNG Images')
db_name_insert = st.sidebar.selectbox("πŸ—„οΈ Select Database", [db['id'] for db in cosmos_client.list_databases()], key='db_insert')
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')
if st.sidebar.button('πŸ“₯ Insert PNG Images with Unique IDs'):
insert_png_images_with_unique_ids(db_name_insert, container_name_insert)
# Delete All Items in a Container
st.sidebar.subheader('πŸ—‘οΈ Delete All Items')
db_name_delete = st.sidebar.selectbox("πŸ—„οΈ Select Database to Delete From", [db['id'] for db in cosmos_client.list_databases()], key='db_delete')
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')
if st.sidebar.button('πŸ—‘οΈ Delete All Items in Container'):
delete_all_items_in_container(db_name_delete, container_name_delete)
st.sidebar.success("βœ… All items deleted successfully.")
st.rerun()
# Main content
display_and_manage_cosmos_db()