Spaces:
Sleeping
Sleeping
Update backup1-app.py
Browse files- backup1-app.py +59 -50
backup1-app.py
CHANGED
@@ -3,75 +3,67 @@ 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 |
-
|
7 |
import requests
|
8 |
import glob
|
|
|
9 |
|
10 |
-
#
|
11 |
COSMOS_CONNECTION_STRING = os.getenv('COSMOS_CONNECTION_STRING')
|
12 |
BLOB_STORAGE_CONNECTION_STRING = os.getenv('BLOB_STORAGE_CONNECTION_STRING')
|
13 |
-
|
14 |
-
# Initialize Azure Cosmos DB Client
|
15 |
cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
|
16 |
-
|
17 |
-
# Initialize Azure Blob Storage Client
|
18 |
blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
|
19 |
|
20 |
-
# Function to
|
21 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
st.subheader('Azure Cosmos DB Structure')
|
23 |
-
db_properties
|
24 |
-
if db_properties:
|
25 |
db_name = db_properties['id']
|
26 |
st.markdown(f"#### Database: {db_name}")
|
27 |
database_client = cosmos_client.get_database_client(db_name)
|
28 |
-
|
29 |
-
|
30 |
container_name = container_properties['id']
|
31 |
st.markdown(f"- **Container**: {container_name}")
|
32 |
container_client = database_client.get_container_client(container_name)
|
33 |
-
|
34 |
-
for item in
|
35 |
item_desc = f" - Item: `{item['id']}`"
|
36 |
-
|
37 |
-
|
38 |
st.image(item['file_name'])
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
-
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
container_properties = next(database_client.list_containers(), None)
|
53 |
-
if container_properties:
|
54 |
-
container_name = container_properties['id']
|
55 |
-
container_client = database_client.get_container_client(container_name)
|
56 |
-
existing_items = list(container_client.read_all_items())
|
57 |
-
existing_ids = {item['id'] for item in existing_items}
|
58 |
-
|
59 |
-
# Add or update PNG files from directory
|
60 |
-
png_files = glob.glob('*.png')
|
61 |
-
for file_name in png_files:
|
62 |
-
item_id = os.path.splitext(file_name)[0]
|
63 |
-
item_data = {"id": item_id, "file_name": file_name}
|
64 |
-
if item_id not in existing_ids:
|
65 |
-
container_client.create_item(body=item_data)
|
66 |
-
st.write(f"Added Item: {item_id}")
|
67 |
-
else:
|
68 |
-
if st.button(f"🗑️ Delete {item_id}", key=f"delete_{item_id}"):
|
69 |
-
container_client.delete_item(item=item_data, partition_key=item_id)
|
70 |
-
st.write(f"Item already exists: {item_id}")
|
71 |
-
|
72 |
-
# UI to Add or Update PNG Images
|
73 |
-
if st.button('Manage PNG Images'):
|
74 |
-
add_or_update_png_images()
|
75 |
|
76 |
# Azure Blob Storage - Upload/Download
|
77 |
st.subheader('Azure Blob Storage - Upload/Download')
|
@@ -81,6 +73,7 @@ blob_file = st.file_uploader('Upload file to Blob')
|
|
81 |
if blob_file is not None and st.button('Upload to Blob'):
|
82 |
blob_client = blob_service.get_blob_client(container=blob_container, blob=blob_file.name)
|
83 |
blob_client.upload_blob(blob_file.getvalue())
|
|
|
84 |
|
85 |
# Azure Functions - Trigger
|
86 |
st.subheader('Azure Functions - Trigger')
|
@@ -89,3 +82,19 @@ function_url = st.text_input('Function URL')
|
|
89 |
if st.button('Call Azure Function'):
|
90 |
response = requests.get(function_url)
|
91 |
st.write('Function Response:', response.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Clients
|
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 All Items in a Container
|
17 |
+
def delete_all_items_in_container(db_name, container_name):
|
18 |
+
database_client = cosmos_client.get_database_client(db_name)
|
19 |
+
container_client = database_client.get_container_client(container_name)
|
20 |
+
|
21 |
+
for item in container_client.read_all_items():
|
22 |
+
try:
|
23 |
+
partition_key = '/id'
|
24 |
+
container_client.delete_item(item=item['id'], partition_key=partition_key)
|
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 |
item_desc = f" - Item: `{item['id']}`"
|
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 |
+
partition_key = '/id'
|
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 = st.selectbox("Select Database", [db['id'] for db in cosmos_client.list_databases()])
|
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 |
# Azure Blob Storage - Upload/Download
|
69 |
st.subheader('Azure Blob Storage - Upload/Download')
|
|
|
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')
|
|
|
82 |
if st.button('Call Azure Function'):
|
83 |
response = requests.get(function_url)
|
84 |
st.write('Function Response:', response.text)
|
85 |
+
|
86 |
+
|
87 |
+
# Display Cosmos DB Structure and Manage Items
|
88 |
+
display_and_manage_cosmos_db()
|
89 |
+
|
90 |
+
# Button to Insert PNG Images with Unique Identifiers
|
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.")
|