Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -40,27 +40,36 @@ def display_cosmos_db_structure():
|
|
40 |
else:
|
41 |
st.markdown(item_desc)
|
42 |
|
43 |
-
|
44 |
# Function to Add or Update an Item
|
45 |
def add_or_update_item(database_name, container_name, item_id, item_data):
|
46 |
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
existing_item.update(item_data)
|
50 |
-
container.replace_item(
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
53 |
|
54 |
-
#
|
55 |
-
|
|
|
|
|
|
|
56 |
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
# Button to Trigger Display of Cosmos DB Structure
|
65 |
if st.button('Show Cosmos DB Structure'):
|
66 |
display_cosmos_db_structure()
|
|
|
40 |
else:
|
41 |
st.markdown(item_desc)
|
42 |
|
|
|
43 |
# Function to Add or Update an Item
|
44 |
def add_or_update_item(database_name, container_name, item_id, item_data):
|
45 |
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
46 |
+
query = "SELECT * FROM c WHERE c.id = @id"
|
47 |
+
parameters = [{"name": "@id", "value": item_id}]
|
48 |
+
items = list(container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
|
49 |
+
|
50 |
+
if items:
|
51 |
+
# Item exists, update it
|
52 |
+
existing_item = items[0]
|
53 |
existing_item.update(item_data)
|
54 |
+
container.replace_item(item=existing_item, body=existing_item)
|
55 |
+
st.write(f"Updated Item: {item_id}")
|
56 |
+
else:
|
57 |
+
# Item does not exist, create new
|
58 |
+
container.create_item(body=item_data)
|
59 |
+
st.write(f"Created New Item: {item_id}")
|
60 |
|
61 |
+
# Display item ID and image if available
|
62 |
+
display_item(database_name, container_name, item_id)
|
63 |
+
|
64 |
+
# Function to Display an Item
|
65 |
+
def display_item(database_name, container_name, item_id):
|
66 |
container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
|
67 |
+
item = container.read_item(item_id, partition_key=PartitionKey(item_id))
|
68 |
+
st.markdown(f"- **Item ID**: {item_id}")
|
69 |
+
if 'file_name' in item and item['file_name'].endswith('.png'):
|
70 |
+
st.image(item['file_name'], caption=f"Image: {item['file_name']}")
|
71 |
+
|
72 |
+
|
|
|
73 |
# Button to Trigger Display of Cosmos DB Structure
|
74 |
if st.button('Show Cosmos DB Structure'):
|
75 |
display_cosmos_db_structure()
|