awacke1 commited on
Commit
811a3e7
1 Parent(s): 56db977

Update backup1-app.py

Browse files
Files changed (1) hide show
  1. backup1-app.py +30 -103
backup1-app.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import streamlit as st
3
  from azure.cosmos import CosmosClient, PartitionKey
4
  from azure.storage.blob import BlobServiceClient
 
 
5
  import requests
6
  import glob
7
 
@@ -18,37 +20,31 @@ blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_
18
  # Function to Retrieve and Display Cosmos DB Structure
19
  def display_cosmos_db_structure():
20
  st.subheader('Azure Cosmos DB Structure')
21
- for db_properties in cosmos_client.list_databases():
 
22
  db_name = db_properties['id']
23
  st.markdown(f"#### Database: {db_name}")
24
  database_client = cosmos_client.get_database_client(db_name)
25
-
26
- for container_properties in database_client.list_containers():
27
  container_name = container_properties['id']
28
  st.markdown(f"- **Container**: {container_name}")
29
  container_client = database_client.get_container_client(container_name)
30
-
31
  items = list(container_client.read_all_items())
32
  for item in items:
33
- st.markdown(f" - Item: `{item['id']}`") # Replace 'id' with the appropriate key if different
 
 
 
 
 
34
 
35
  # Button to Trigger Display of Cosmos DB Structure
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)
45
- container.replace_item(item_id, existing_item)
46
- except:
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']
@@ -56,95 +52,26 @@ def test_insert_png_images():
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
86
- def add_item_to_container(database_name, container_name, item):
87
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
88
- container.create_item(item)
89
-
90
- # Function to Get All Item Keys from a Container
91
- def get_all_item_keys(database_name, container_name):
92
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
93
- items = list(container.query_items(
94
- query="SELECT c.id FROM c",
95
- enable_cross_partition_query=True))
96
- return [item['id'] for item in items]
97
-
98
- # Function to Retrieve and Display an Item
99
- def display_item(database_name, container_name, item_id):
100
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
101
- item = container.read_item(item_id, partition_key=PartitionKey(item_id))
102
- st.markdown(f"- **Item ID**: {item_id}, **Content**: {json.dumps(item)}")
103
-
104
- # Test Function to Add and Retrieve Image Prompts
105
- def test_image_prompts(database_name, container_name):
106
- # Sample data
107
- image_prompts = [{"id": f"image_{i}", "prompt": f"Image prompt {i}", "image_url": f"http://example.com/image_{i}.jpg"} for i in range(10)]
108
-
109
- # Add items
110
- for item in image_prompts:
111
- add_item_to_container(database_name, container_name, item)
112
-
113
- # Retrieve and display items
114
- item_keys = get_all_item_keys(database_name, container_name)
115
- for key in item_keys:
116
- display_item(database_name, container_name, key)
117
-
118
- # UI to Test Image Prompts Function
119
- if st.button('Test Image Prompts'):
120
- test_database_name = st.text_input('Enter Test Database Name')
121
- test_container_name = st.text_input('Enter Test Container Name')
122
- if test_database_name and test_container_name:
123
- test_image_prompts(test_database_name, test_container_name)
124
- else:
125
- st.error('Please enter the test database and container names.')
126
-
127
-
128
-
129
-
130
- # Streamlit UI
131
- st.title('Azure Services Integration with Streamlit')
132
-
133
- # Azure Cosmos DB - CRUD Operations
134
- st.subheader('Azure Cosmos DB - CRUD Operations')
135
- cosmos_db = st.text_input('Database Name')
136
- cosmos_container = st.text_input('Container Name')
137
- item_id = st.text_input("Item ID (for Read, Update, Delete)")
138
- item_data = st.text_area("Item Data (JSON format, for Create and Update)")
139
-
140
- if st.button('Create Item in Cosmos DB'):
141
- container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
142
- container.create_item(item_data)
143
-
144
- if st.button('Read Item from Cosmos DB'):
145
- container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
146
- item = container.read_item(item_id, partition_key=PartitionKey(item_id))
147
- st.json(item)
148
 
149
  # Azure Blob Storage - Upload/Download
150
  st.subheader('Azure Blob Storage - Upload/Download')
 
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
+
7
  import requests
8
  import glob
9
 
 
20
  # Function to Retrieve and Display Cosmos DB Structure
21
  def display_cosmos_db_structure():
22
  st.subheader('Azure Cosmos DB Structure')
23
+ db_properties = next(cosmos_client.list_databases(), None)
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
+ container_properties = next(database_client.list_containers(), None)
29
+ if container_properties:
30
  container_name = container_properties['id']
31
  st.markdown(f"- **Container**: {container_name}")
32
  container_client = database_client.get_container_client(container_name)
 
33
  items = list(container_client.read_all_items())
34
  for item in items:
35
+ item_desc = f" - Item: `{item['id']}`"
36
+ if 'file_name' in item and item['file_name'].endswith('.png'):
37
+ st.markdown(item_desc)
38
+ st.image(item['file_name'])
39
+ else:
40
+ st.markdown(item_desc)
41
 
42
  # Button to Trigger Display of Cosmos DB Structure
43
  if st.button('Show Cosmos DB Structure'):
44
  display_cosmos_db_structure()
45
 
46
+ # Function to Add or Update PNG Images
47
+ def add_or_update_png_images():
 
 
 
 
 
 
 
 
 
 
 
48
  db_properties = next(cosmos_client.list_databases(), None)
49
  if db_properties:
50
  db_name = db_properties['id']
 
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')