Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import streamlit as st
|
|
| 3 |
from azure.cosmos import CosmosClient, PartitionKey
|
| 4 |
from azure.storage.blob import BlobServiceClient
|
| 5 |
import requests
|
|
|
|
| 6 |
|
| 7 |
# Environment Variables
|
| 8 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
|
@@ -37,6 +38,42 @@ if st.button('Show Cosmos DB Structure'):
|
|
| 37 |
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Function to Add an Item
|
| 41 |
def add_item_to_container(database_name, container_name, item):
|
| 42 |
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
|
|
|
| 3 |
from azure.cosmos import CosmosClient, PartitionKey
|
| 4 |
from azure.storage.blob import BlobServiceClient
|
| 5 |
import requests
|
| 6 |
+
import glob
|
| 7 |
|
| 8 |
# Environment Variables
|
| 9 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
+
# Function to Add or Update an Item
|
| 42 |
+
def add_or_update_item(database_name, container_name, item_id, item_data):
|
| 43 |
+
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
| 44 |
+
try:
|
| 45 |
+
existing_item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
| 46 |
+
existing_item.update(item_data)
|
| 47 |
+
container.replace_item(item_id, existing_item)
|
| 48 |
+
except:
|
| 49 |
+
container.create_item(item_data)
|
| 50 |
+
|
| 51 |
+
# Test Function to Insert PNG Images
|
| 52 |
+
def test_insert_png_images(database_name, container_name):
|
| 53 |
+
png_files = glob.glob('*.png')
|
| 54 |
+
for file_name in png_files:
|
| 55 |
+
item_id = os.path.splitext(file_name)[0] # Use file name without extension as ID
|
| 56 |
+
item_data = {"id": item_id, "file_name": file_name}
|
| 57 |
+
add_or_update_item(database_name, container_name, item_id, item_data)
|
| 58 |
+
st.write(f"Inserted: {file_name}")
|
| 59 |
+
|
| 60 |
+
# Displaying Images
|
| 61 |
+
st.subheader("Displaying Images in Container")
|
| 62 |
+
items = list(cosmos_client.get_database_client(database_name).get_container_client(container_name).read_all_items())
|
| 63 |
+
for item in items:
|
| 64 |
+
if 'file_name' in item and item['file_name'].endswith('.png'):
|
| 65 |
+
st.image(item['file_name'], caption=item['file_name'])
|
| 66 |
+
|
| 67 |
+
# UI to Test Image Insertion Function
|
| 68 |
+
if st.button('Test Insert PNG Images'):
|
| 69 |
+
test_database_name = st.text_input('Enter Test Database Name')
|
| 70 |
+
test_container_name = st.text_input('Enter Test Container Name')
|
| 71 |
+
if test_database_name and test_container_name:
|
| 72 |
+
test_insert_png_images(test_database_name, test_container_name)
|
| 73 |
+
else:
|
| 74 |
+
st.error('Please enter the test database and container names.')
|
| 75 |
+
|
| 76 |
+
|
| 77 |
# Function to Add an Item
|
| 78 |
def add_item_to_container(database_name, container_name, item):
|
| 79 |
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|