| | 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 |
| |
|
| | |
| | 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) |
| |
|
| | |
| | st.set_page_config(page_title="Azure Manager", page_icon="βοΈ", layout="wide") |
| |
|
| | |
| | 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']}") |
| |
|
| | |
| | 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() |
| |
|
| | |
| | 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}") |
| |
|
| | |
| | st.title("βοΈ Azure Manager") |
| |
|
| | |
| | st.sidebar.header("π οΈ Global Actions") |
| |
|
| | |
| | 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.') |
| |
|
| | |
| | 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) |
| |
|
| | |
| | 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) |
| |
|
| | |
| | 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() |
| |
|
| | |
| | display_and_manage_cosmos_db() |