awacke1 commited on
Commit
56db977
1 Parent(s): 8759ff7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -131
app.py CHANGED
@@ -17,20 +17,19 @@ cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
17
  # Initialize Azure Blob Storage Client
18
  blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
19
 
20
-
21
  # Function to Retrieve and Display Cosmos DB Structure
22
  def display_cosmos_db_structure():
23
  st.subheader('Azure Cosmos DB Structure')
24
- for db_properties in cosmos_client.list_databases():
 
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
- for container_properties in database_client.list_containers():
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
  items = list(container_client.read_all_items())
35
  for item in items:
36
  item_desc = f" - Item: `{item['id']}`"
@@ -40,51 +39,12 @@ def display_cosmos_db_structure():
40
  else:
41
  st.markdown(item_desc)
42
 
43
-
44
- # Function to Add or Update PNG Images
45
- def add_or_update_png_images(database_name, container_name):
46
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
47
- existing_items = list(container.read_all_items())
48
- existing_ids = {item['id'] for item in existing_items}
49
-
50
- # Display and delete option for existing items
51
- for item in existing_items:
52
- if 'file_name' in item and item['file_name'].endswith('.png'):
53
- st.markdown(f"Item: `{item['id']}`")
54
- st.image(item['file_name'])
55
- if st.button(f"🗑️ Delete {item['id']}", key=f"delete_{item['id']}"):
56
- container.delete_item(item=item, partition_key=item['id'])
57
-
58
- # Add or update PNG files from directory
59
- png_files = glob.glob('*.png')
60
- for file_name in png_files:
61
- item_id = os.path.splitext(file_name)[0]
62
- if item_id not in existing_ids:
63
- item_data = {"id": item_id, "file_name": file_name}
64
- container.create_item(body=item_data)
65
- st.write(f"Added Item: {item_id}")
66
- else:
67
- st.write(f"Item already exists: {item_id}")
68
-
69
- # UI to manage PNG images
70
- st.title('Manage PNG Images in Cosmos DB')
71
- selected_db_name = st.text_input('Enter Database Name')
72
- selected_container_name = st.text_input('Enter Container Name')
73
-
74
- if st.button('Manage Images'):
75
- if selected_db_name and selected_container_name:
76
- add_or_update_png_images(selected_db_name, selected_container_name)
77
- else:
78
- st.error('Please enter both database and container names.')
79
-
80
-
81
  # Button to Trigger Display of Cosmos DB Structure
82
  if st.button('Show Cosmos DB Structure'):
83
  display_cosmos_db_structure()
84
-
85
- # Test Function to Insert PNG Images
86
- def test_insert_png_images():
87
- # Get the first database and container
88
  db_properties = next(cosmos_client.list_databases(), None)
89
  if db_properties:
90
  db_name = db_properties['id']
@@ -92,95 +52,26 @@ def test_insert_png_images():
92
  container_properties = next(database_client.list_containers(), None)
93
  if container_properties:
94
  container_name = container_properties['id']
 
 
 
95
 
96
- # Insert PNG files
97
  png_files = glob.glob('*.png')
98
  for file_name in png_files:
99
- item_id = os.path.splitext(file_name)[0] # Use file name without extension as ID
100
  item_data = {"id": item_id, "file_name": file_name}
101
- add_or_update_item(database_client, container_name, item_id, item_data)
102
- st.write(f"Inserted: {file_name}")
103
-
104
- # Displaying Images
105
- st.subheader("Displaying Images in Container")
106
- items = list(database_client.get_container_client(container_name).read_all_items())
107
- for item in items:
108
- if 'file_name' in item and item['file_name'].endswith('.png'):
109
- st.image(item['file_name'], caption=item['file_name'])
110
- else:
111
- st.error("No container found in the database.")
112
- else:
113
- st.error("No database found.")
114
-
115
- # UI to Test Image Insertion Function
116
- if st.button('Test Insert PNG Images'):
117
- test_insert_png_images()
118
-
119
-
120
-
121
- # Function to Add an Item
122
- def add_item_to_container(database_name, container_name, item):
123
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
124
- container.create_item(item)
125
-
126
- # Function to Get All Item Keys from a Container
127
- def get_all_item_keys(database_name, container_name):
128
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
129
- items = list(container.query_items(
130
- query="SELECT c.id FROM c",
131
- enable_cross_partition_query=True))
132
- return [item['id'] for item in items]
133
-
134
- # Function to Retrieve and Display an Item
135
- def display_item(database_name, container_name, item_id):
136
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
137
- item = container.read_item(item_id, partition_key=PartitionKey(item_id))
138
- st.markdown(f"- **Item ID**: {item_id}, **Content**: {json.dumps(item)}")
139
-
140
- # Test Function to Add and Retrieve Image Prompts
141
- def test_image_prompts(database_name, container_name):
142
- # Sample data
143
- 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)]
144
-
145
- # Add items
146
- for item in image_prompts:
147
- add_item_to_container(database_name, container_name, item)
148
-
149
- # Retrieve and display items
150
- item_keys = get_all_item_keys(database_name, container_name)
151
- for key in item_keys:
152
- display_item(database_name, container_name, key)
153
-
154
- # UI to Test Image Prompts Function
155
- if st.button('Test Image Prompts'):
156
- test_database_name = st.text_input('Enter Test Database Name')
157
- test_container_name = st.text_input('Enter Test Container Name')
158
- if test_database_name and test_container_name:
159
- test_image_prompts(test_database_name, test_container_name)
160
- else:
161
- st.error('Please enter the test database and container names.')
162
-
163
-
164
-
165
-
166
- # Streamlit UI
167
- st.title('Azure Services Integration with Streamlit')
168
-
169
- # Azure Cosmos DB - CRUD Operations
170
- st.subheader('Azure Cosmos DB - CRUD Operations')
171
- cosmos_db = st.text_input('Database Name')
172
- cosmos_container = st.text_input('Container Name')
173
- item_id = st.text_input("Item ID (for Read, Update, Delete)")
174
- item_data = st.text_area("Item Data (JSON format, for Create and Update)")
175
-
176
- if st.button('Create Item in Cosmos DB'):
177
- container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
178
- container.create_item(item_data)
179
 
180
- if st.button('Read Item from Cosmos DB'):
181
- container = cosmos_client.get_database_client(cosmos_db).get_container_client(cosmos_container)
182
- item = container.read_item(item_id, partition_key=PartitionKey(item_id))
183
- st.json(item)
184
 
185
  # Azure Blob Storage - Upload/Download
186
  st.subheader('Azure Blob Storage - Upload/Download')
 
17
  # Initialize Azure Blob Storage Client
18
  blob_service = BlobServiceClient.from_connection_string(BLOB_STORAGE_CONNECTION_STRING)
19
 
 
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']}`"
 
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')