Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,50 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from azure.cosmos import CosmosClient, PartitionKey
|
4 |
-
from azure.storage.blob import BlobServiceClient
|
5 |
-
from azure.cosmos.exceptions import CosmosResourceNotFoundError
|
6 |
-
|
7 |
-
import requests
|
8 |
import glob
|
9 |
|
10 |
# Environment Variables
|
11 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
12 |
-
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
|
13 |
|
14 |
# Initialize Azure Cosmos DB Client
|
15 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
# Function to Retrieve and Display Cosmos DB Structure
|
21 |
-
def display_cosmos_db_structure():
|
22 |
-
st.subheader('Azure Cosmos DB Structure')
|
23 |
db_properties = next(cosmos_client.list_databases(), None)
|
24 |
if db_properties:
|
25 |
db_name = db_properties['id']
|
26 |
-
st.markdown(f"#### Database: {db_name}")
|
27 |
database_client = cosmos_client.get_database_client(db_name)
|
28 |
container_properties = next(database_client.list_containers(), None)
|
29 |
if container_properties:
|
30 |
container_name = container_properties['id']
|
31 |
-
st.markdown(f"- **Container**: {container_name}")
|
32 |
container_client = database_client.get_container_client(container_name)
|
33 |
items = list(container_client.read_all_items())
|
|
|
|
|
34 |
for item in items:
|
35 |
-
item_desc = f"
|
36 |
if 'file_name' in item and item['file_name'].endswith('.png'):
|
37 |
st.markdown(item_desc)
|
38 |
st.image(item['file_name'])
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# Button to Trigger Display of Cosmos DB Structure
|
43 |
-
if st.button('Show Cosmos DB Structure'):
|
44 |
-
display_cosmos_db_structure()
|
45 |
-
|
46 |
-
# Function to Add or Update PNG Images
|
47 |
-
def add_or_update_png_images():
|
48 |
-
db_properties = next(cosmos_client.list_databases(), None)
|
49 |
-
if db_properties:
|
50 |
-
db_name = db_properties['id']
|
51 |
-
database_client = cosmos_client.get_database_client(db_name)
|
52 |
-
container_properties = next(database_client.list_containers(), None)
|
53 |
-
if container_properties:
|
54 |
-
container_name = container_properties['id']
|
55 |
-
container_client = database_client.get_container_client(container_name)
|
56 |
-
existing_items = list(container_client.read_all_items())
|
57 |
-
existing_ids = {item['id'] for item in existing_items}
|
58 |
|
59 |
# Add or update PNG files from directory
|
60 |
png_files = glob.glob('*.png')
|
61 |
for file_name in png_files:
|
62 |
item_id = os.path.splitext(file_name)[0]
|
63 |
item_data = {"id": item_id, "file_name": file_name}
|
64 |
-
if item_id not in
|
65 |
container_client.create_item(body=item_data)
|
66 |
st.write(f"Added Item: {item_id}")
|
67 |
-
else:
|
68 |
-
if st.button(f"🗑️ Delete {item_id}", key=f"delete_{item_id}"):
|
69 |
-
container_client.delete_item(item=item_data, partition_key=item_id)
|
70 |
-
st.write(f"Item already exists: {item_id}")
|
71 |
|
72 |
-
# UI to
|
|
|
73 |
if st.button('Manage PNG Images'):
|
74 |
-
|
75 |
|
76 |
# Azure Blob Storage - Upload/Download
|
77 |
st.subheader('Azure Blob Storage - Upload/Download')
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
from azure.cosmos import CosmosClient, PartitionKey
|
|
|
|
|
|
|
|
|
4 |
import glob
|
5 |
|
6 |
# Environment Variables
|
7 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
|
|
8 |
|
9 |
# Initialize Azure Cosmos DB Client
|
10 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
11 |
|
12 |
+
# Function to Retrieve, Display, and Manage Cosmos DB Images
|
13 |
+
def manage_cosmos_db_images():
|
14 |
+
st.subheader('Manage Cosmos DB Images')
|
|
|
|
|
|
|
15 |
db_properties = next(cosmos_client.list_databases(), None)
|
16 |
if db_properties:
|
17 |
db_name = db_properties['id']
|
|
|
18 |
database_client = cosmos_client.get_database_client(db_name)
|
19 |
container_properties = next(database_client.list_containers(), None)
|
20 |
if container_properties:
|
21 |
container_name = container_properties['id']
|
|
|
22 |
container_client = database_client.get_container_client(container_name)
|
23 |
items = list(container_client.read_all_items())
|
24 |
+
|
25 |
+
# Display existing items with delete option
|
26 |
for item in items:
|
27 |
+
item_desc = f"Item: `{item['id']}`"
|
28 |
if 'file_name' in item and item['file_name'].endswith('.png'):
|
29 |
st.markdown(item_desc)
|
30 |
st.image(item['file_name'])
|
31 |
+
if st.button(f"🗑️ Delete {item['id']}", key=f"delete_{item['id']}"):
|
32 |
+
container_client.delete_item(item=item, partition_key=item['id'])
|
33 |
+
st.success(f"Deleted Item: {item['id']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
# Add or update PNG files from directory
|
36 |
png_files = glob.glob('*.png')
|
37 |
for file_name in png_files:
|
38 |
item_id = os.path.splitext(file_name)[0]
|
39 |
item_data = {"id": item_id, "file_name": file_name}
|
40 |
+
if item_id not in {item['id'] for item in items}:
|
41 |
container_client.create_item(body=item_data)
|
42 |
st.write(f"Added Item: {item_id}")
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# UI to Manage PNG Images
|
45 |
+
st.title('Manage PNG Images in Cosmos DB')
|
46 |
if st.button('Manage PNG Images'):
|
47 |
+
manage_cosmos_db_images()
|
48 |
|
49 |
# Azure Blob Storage - Upload/Download
|
50 |
st.subheader('Azure Blob Storage - Upload/Download')
|