Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,11 +36,9 @@ def display_cosmos_db_structure():
|
|
| 36 |
if st.button('Show Cosmos DB Structure'):
|
| 37 |
display_cosmos_db_structure()
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
# Function to Add or Update an Item
|
| 42 |
-
def add_or_update_item(
|
| 43 |
-
container =
|
| 44 |
try:
|
| 45 |
existing_item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
| 46 |
existing_item.update(item_data)
|
|
@@ -49,29 +47,39 @@ def add_or_update_item(database_name, container_name, item_id, item_data):
|
|
| 49 |
container.create_item(item_data)
|
| 50 |
|
| 51 |
# Test Function to Insert PNG Images
|
| 52 |
-
def test_insert_png_images(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# UI to Test Image Insertion Function
|
| 68 |
if st.button('Test Insert PNG Images'):
|
| 69 |
-
|
| 70 |
-
|
| 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
|
|
|
|
| 36 |
if st.button('Show Cosmos DB Structure'):
|
| 37 |
display_cosmos_db_structure()
|
| 38 |
|
|
|
|
|
|
|
| 39 |
# Function to Add or Update an Item
|
| 40 |
+
def add_or_update_item(database_client, container_name, item_id, item_data):
|
| 41 |
+
container = database_client.get_container_client(container_name)
|
| 42 |
try:
|
| 43 |
existing_item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
| 44 |
existing_item.update(item_data)
|
|
|
|
| 47 |
container.create_item(item_data)
|
| 48 |
|
| 49 |
# Test Function to Insert PNG Images
|
| 50 |
+
def test_insert_png_images():
|
| 51 |
+
# Get the first database and container
|
| 52 |
+
db_properties = next(cosmos_client.list_databases(), None)
|
| 53 |
+
if db_properties:
|
| 54 |
+
db_name = db_properties['id']
|
| 55 |
+
database_client = cosmos_client.get_database_client(db_name)
|
| 56 |
+
container_properties = next(database_client.list_containers(), None)
|
| 57 |
+
if container_properties:
|
| 58 |
+
container_name = container_properties['id']
|
| 59 |
+
|
| 60 |
+
# Insert PNG files
|
| 61 |
+
png_files = glob.glob('*.png')
|
| 62 |
+
for file_name in png_files:
|
| 63 |
+
item_id = os.path.splitext(file_name)[0] # Use file name without extension as ID
|
| 64 |
+
item_data = {"id": item_id, "file_name": file_name}
|
| 65 |
+
add_or_update_item(database_client, container_name, item_id, item_data)
|
| 66 |
+
st.write(f"Inserted: {file_name}")
|
| 67 |
+
|
| 68 |
+
# Displaying Images
|
| 69 |
+
st.subheader("Displaying Images in Container")
|
| 70 |
+
items = list(database_client.get_container_client(container_name).read_all_items())
|
| 71 |
+
for item in items:
|
| 72 |
+
if 'file_name' in item and item['file_name'].endswith('.png'):
|
| 73 |
+
st.image(item['file_name'], caption=item['file_name'])
|
| 74 |
+
else:
|
| 75 |
+
st.error("No container found in the database.")
|
| 76 |
+
else:
|
| 77 |
+
st.error("No database found.")
|
| 78 |
|
| 79 |
# UI to Test Image Insertion Function
|
| 80 |
if st.button('Test Insert PNG Images'):
|
| 81 |
+
test_insert_png_images()
|
| 82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
# Function to Add an Item
|