File size: 3,926 Bytes
644ca8a
4d95901
57ebff8
bf5ebfe
8b66346
 
bf5ebfe
2faaefc
bf5ebfe
644ca8a
4d95901
644ca8a
 
 
4d95901
644ca8a
 
 
 
3e41210
 
 
56db977
 
9dbc8f3
3e41210
9dbc8f3
56db977
 
9dbc8f3
3e41210
9dbc8f3
 
3e41210
f05b0a0
 
 
 
 
 
9dbc8f3
f05b0a0
 
 
56db977
 
 
e73d5b7
 
 
 
 
 
 
56db977
 
 
2faaefc
56db977
e73d5b7
 
56db977
e73d5b7
56db977
 
 
 
 
 
 
bf5ebfe
56db977
 
 
bf5ebfe
 
 
 
 
 
 
 
 
 
 
 
 
57ebff8
bf5ebfe
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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)