Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,20 +2,17 @@ import os
|
|
2 |
import streamlit as st
|
3 |
from azure.cosmos import CosmosClient, PartitionKey
|
4 |
from azure.cosmos.exceptions import CosmosResourceNotFoundError
|
5 |
-
import glob
|
6 |
-
from datetime import datetime
|
7 |
|
8 |
# Initialize Azure Cosmos DB Client
|
9 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
10 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
11 |
|
12 |
-
# Function to Delete Item
|
13 |
-
def
|
14 |
-
database_client = cosmos_client.get_database_client(db_name)
|
15 |
-
container_client = database_client.get_container_client(container_name)
|
16 |
try:
|
17 |
-
|
18 |
-
container_client.
|
|
|
19 |
st.success(f"Deleted Item: {item_id}")
|
20 |
except CosmosResourceNotFoundError:
|
21 |
st.error(f"Item not found: {item_id}")
|
@@ -30,29 +27,19 @@ def display_and_manage_cosmos_db():
|
|
30 |
container_name = container_properties['id']
|
31 |
container_client = database_client.get_container_client(container_name)
|
32 |
for item in container_client.read_all_items():
|
33 |
-
st.
|
34 |
-
if
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
for file_name in glob.glob('*.png'):
|
42 |
-
unique_id = f"{os.path.splitext(file_name)[0]}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
43 |
-
item_data = {"id": unique_id, "file_name": file_name}
|
44 |
-
container_client.create_item(body=item_data)
|
45 |
-
st.write(f"Inserted Item: {unique_id}")
|
46 |
-
|
47 |
-
# Helper to Select Database and Container
|
48 |
-
def select_db_and_container():
|
49 |
-
db_name = st.selectbox("Select Database", [db['id'] for db in cosmos_client.list_databases()])
|
50 |
-
container_name = st.selectbox("Select Container", [container['id'] for container in cosmos_client.get_database_client(db_name).list_containers()])
|
51 |
-
return db_name, container_name
|
52 |
|
53 |
-
# UI
|
54 |
-
|
|
|
55 |
|
56 |
-
#
|
57 |
-
if
|
58 |
-
|
|
|
2 |
import streamlit as st
|
3 |
from azure.cosmos import CosmosClient, PartitionKey
|
4 |
from azure.cosmos.exceptions import CosmosResourceNotFoundError
|
|
|
|
|
5 |
|
6 |
# Initialize Azure Cosmos DB Client
|
7 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
8 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
9 |
|
10 |
+
# Function to Delete an Item in Cosmos DB
|
11 |
+
def delete_cosmos_item(db_name, container_name, item_id, partition_key):
|
|
|
|
|
12 |
try:
|
13 |
+
database_client = cosmos_client.get_database_client(db_name)
|
14 |
+
container_client = database_client.get_container_client(container_name)
|
15 |
+
container_client.delete_item(item=item_id, partition_key=partition_key)
|
16 |
st.success(f"Deleted Item: {item_id}")
|
17 |
except CosmosResourceNotFoundError:
|
18 |
st.error(f"Item not found: {item_id}")
|
|
|
27 |
container_name = container_properties['id']
|
28 |
container_client = database_client.get_container_client(container_name)
|
29 |
for item in container_client.read_all_items():
|
30 |
+
st.json(item) # Display the full data of the item
|
31 |
+
if 'file_name' in item and os.path.isfile(item['file_name']):
|
32 |
+
st.image(item['file_name']) # Show image if available
|
33 |
+
# Assume the partition key is the item ID
|
34 |
+
partition_key = item.get('id')
|
35 |
+
delete_button_key = f"delete_{item['id']}"
|
36 |
+
if st.button(f"🗑️ Delete {item['id']}", key=delete_button_key):
|
37 |
+
delete_cosmos_item(db_name, container_name, item['id'], partition_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Main UI Function
|
40 |
+
def main():
|
41 |
+
display_and_manage_cosmos_db()
|
42 |
|
43 |
+
# Run the main function
|
44 |
+
if __name__ == "__main__":
|
45 |
+
main()
|