Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,100 +1,57 @@
|
|
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 |
-
import requests
|
7 |
import glob
|
8 |
from datetime import datetime
|
9 |
|
10 |
-
# Initialize Azure
|
11 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
12 |
-
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
|
13 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
14 |
-
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
|
15 |
|
16 |
-
# Function to Delete
|
17 |
-
def
|
18 |
database_client = cosmos_client.get_database_client(db_name)
|
19 |
container_client = database_client.get_container_client(container_name)
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
st.write(f"Deleted Item: {item['id']}")
|
26 |
-
except CosmosResourceNotFoundError:
|
27 |
-
st.error(f"Item not found: {item['id']}")
|
28 |
-
|
29 |
|
30 |
# Display and Manage Cosmos DB Structure
|
31 |
def display_and_manage_cosmos_db():
|
32 |
st.subheader('Azure Cosmos DB Structure')
|
33 |
for db_properties in cosmos_client.list_databases():
|
34 |
db_name = db_properties['id']
|
35 |
-
st.markdown(f"#### Database: {db_name}")
|
36 |
database_client = cosmos_client.get_database_client(db_name)
|
37 |
-
|
38 |
for container_properties in database_client.list_containers():
|
39 |
container_name = container_properties['id']
|
40 |
-
st.markdown(f"- **Container**: {container_name}")
|
41 |
container_client = database_client.get_container_client(container_name)
|
42 |
-
|
43 |
for item in container_client.read_all_items():
|
44 |
-
|
45 |
-
st.markdown(item_desc)
|
46 |
-
if 'file_name' in item:
|
47 |
-
st.image(item['file_name'])
|
48 |
-
|
49 |
-
# Update and Delete buttons for each item
|
50 |
if st.button(f"🗑️ Delete {item['id']}", key=f"delete_{item['id']}"):
|
51 |
-
|
52 |
-
container_client.delete_item(item=item['id'], partition_key=partition_key)
|
53 |
-
st.success(f"Deleted Item: {item['id']}")
|
54 |
|
55 |
# Insert PNG Images with Unique Identifiers
|
56 |
def insert_png_images_with_unique_ids():
|
57 |
-
db_name
|
58 |
-
container_name = st.selectbox("Select Container", [container['id'] for container in cosmos_client.get_database_client(db_name).list_containers()])
|
59 |
container_client = cosmos_client.get_database_client(db_name).get_container_client(container_name)
|
60 |
-
|
61 |
-
png_files = glob.glob('*.png')
|
62 |
-
for file_name in png_files:
|
63 |
unique_id = f"{os.path.splitext(file_name)[0]}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
64 |
item_data = {"id": unique_id, "file_name": file_name}
|
65 |
container_client.create_item(body=item_data)
|
66 |
st.write(f"Inserted Item: {unique_id}")
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
if blob_file is not None and st.button('Upload to Blob'):
|
74 |
-
blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
|
75 |
-
blob_client.upload_blob(blob_file.getvalue())
|
76 |
-
st.success('File uploaded successfully.')
|
77 |
-
|
78 |
-
# Azure Functions - Trigger
|
79 |
-
st.subheader('Azure Functions - Trigger')
|
80 |
-
function_url = st.text_input('Function URL')
|
81 |
-
|
82 |
-
if st.button('Call Azure Function'):
|
83 |
-
response = requests.get(function_url)
|
84 |
-
st.write('Function Response:', response.text)
|
85 |
-
|
86 |
|
87 |
-
#
|
88 |
display_and_manage_cosmos_db()
|
89 |
|
90 |
-
#
|
91 |
if st.button('Insert PNG Images with Unique IDs'):
|
92 |
insert_png_images_with_unique_ids()
|
93 |
-
|
94 |
-
# Button to Delete All Items in a Container
|
95 |
-
db_name_to_delete = st.selectbox("Select Database to Delete From", [db['id'] for db in cosmos_client.list_databases()])
|
96 |
-
container_name_to_delete = st.selectbox("Select Container to Delete From", [container['id'] for container in cosmos_client.get_database_client(db_name_to_delete).list_containers()])
|
97 |
-
|
98 |
-
if st.button('Delete All Items in Container'):
|
99 |
-
delete_all_items_in_container(db_name_to_delete, container_name_to_delete)
|
100 |
-
st.success("All items deleted successfully.")
|
|
|
1 |
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 by ID in Cosmos DB
|
13 |
+
def delete_item_by_id(db_name, container_name, item_id):
|
14 |
database_client = cosmos_client.get_database_client(db_name)
|
15 |
container_client = database_client.get_container_client(container_name)
|
16 |
+
try:
|
17 |
+
container_client.delete_item(item=item_id, partition_key=item_id)
|
18 |
+
st.success(f"Deleted Item: {item_id}")
|
19 |
+
except CosmosResourceNotFoundError:
|
20 |
+
st.error(f"Item not found: {item_id}")
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Display and Manage Cosmos DB Structure
|
23 |
def display_and_manage_cosmos_db():
|
24 |
st.subheader('Azure Cosmos DB Structure')
|
25 |
for db_properties in cosmos_client.list_databases():
|
26 |
db_name = db_properties['id']
|
|
|
27 |
database_client = cosmos_client.get_database_client(db_name)
|
|
|
28 |
for container_properties in database_client.list_containers():
|
29 |
container_name = container_properties['id']
|
|
|
30 |
container_client = database_client.get_container_client(container_name)
|
|
|
31 |
for item in container_client.read_all_items():
|
32 |
+
st.markdown(f"- **Item ID**: `{item['id']}`")
|
|
|
|
|
|
|
|
|
|
|
33 |
if st.button(f"🗑️ Delete {item['id']}", key=f"delete_{item['id']}"):
|
34 |
+
delete_item_by_id(db_name, container_name, item['id'])
|
|
|
|
|
35 |
|
36 |
# Insert PNG Images with Unique Identifiers
|
37 |
def insert_png_images_with_unique_ids():
|
38 |
+
db_name, container_name = select_db_and_container()
|
|
|
39 |
container_client = cosmos_client.get_database_client(db_name).get_container_client(container_name)
|
40 |
+
for file_name in glob.glob('*.png'):
|
|
|
|
|
41 |
unique_id = f"{os.path.splitext(file_name)[0]}_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
42 |
item_data = {"id": unique_id, "file_name": file_name}
|
43 |
container_client.create_item(body=item_data)
|
44 |
st.write(f"Inserted Item: {unique_id}")
|
45 |
|
46 |
+
# Helper to Select Database and Container
|
47 |
+
def select_db_and_container():
|
48 |
+
db_name = st.selectbox("Select Database", [db['id'] for db in cosmos_client.list_databases()])
|
49 |
+
container_name = st.selectbox("Select Container", [container['id'] for container in cosmos_client.get_database_client(db_name).list_containers()])
|
50 |
+
return db_name, container_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
# UI for Displaying and Managing Cosmos DB
|
53 |
display_and_manage_cosmos_db()
|
54 |
|
55 |
+
# UI for Inserting PNG Images
|
56 |
if st.button('Insert PNG Images with Unique IDs'):
|
57 |
insert_png_images_with_unique_ids()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|