StreamlitAzure / backup1-app.py
awacke1's picture
Update backup1-app.py
811a3e7
raw
history blame
3.93 kB
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
# Environment Variables
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
# Initialize Azure Cosmos DB Client
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
# Initialize Azure Blob Storage Client
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
# Function to Retrieve and Display Cosmos DB Structure
def display_cosmos_db_structure():
st.subheader('Azure Cosmos DB Structure')
db_properties = next(cosmos_client.list_databases(), None)
if db_properties:
db_name = db_properties['id']
st.markdown(f"#### Database: {db_name}")
database_client = cosmos_client.get_database_client(db_name)
container_properties = next(database_client.list_containers(), None)
if container_properties:
container_name = container_properties['id']
st.markdown(f"- **Container**: {container_name}")
container_client = database_client.get_container_client(container_name)
items = list(container_client.read_all_items())
for item in items:
item_desc = f" - Item: `{item['id']}`"
if 'file_name' in item and item['file_name'].endswith('.png'):
st.markdown(item_desc)
st.image(item['file_name'])
else:
st.markdown(item_desc)
# Button to Trigger Display of Cosmos DB Structure
if st.button('Show Cosmos DB Structure'):
display_cosmos_db_structure()
# Function to Add or Update PNG Images
def add_or_update_png_images():
db_properties = next(cosmos_client.list_databases(), None)
if db_properties:
db_name = db_properties['id']
database_client = cosmos_client.get_database_client(db_name)
container_properties = next(database_client.list_containers(), None)
if container_properties:
container_name = container_properties['id']
container_client = database_client.get_container_client(container_name)
existing_items = list(container_client.read_all_items())
existing_ids = {item['id'] for item in existing_items}
# Add or update PNG files from directory
png_files = glob.glob('*.png')
for file_name in png_files:
item_id = os.path.splitext(file_name)[0]
item_data = {"id": item_id, "file_name": file_name}
if item_id not in existing_ids:
container_client.create_item(body=item_data)
st.write(f"Added Item: {item_id}")
else:
if st.button(f"🗑️ Delete {item_id}", key=f"delete_{item_id}"):
container_client.delete_item(item=item_data, partition_key=item_id)
st.write(f"Item already exists: {item_id}")
# UI to Add or Update PNG Images
if st.button('Manage PNG Images'):
add_or_update_png_images()
# 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())
# 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)