awacke1 commited on
Commit
83a0d6b
·
1 Parent(s): ce8d986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
 
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
@@ -8,7 +9,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)
@@ -17,9 +18,9 @@ def delete_cosmos_item(db_name, container_name, item_id, partition_key):
17
  except CosmosResourceNotFoundError:
18
  st.error(f"Item not found: {item_id}")
19
 
20
- # Display and Manage Cosmos DB Structure
21
- def display_and_manage_cosmos_db():
22
- st.subheader('Azure Cosmos DB Structure')
23
  for db_properties in cosmos_client.list_databases():
24
  db_name = db_properties['id']
25
  database_client = cosmos_client.get_database_client(db_name)
@@ -27,18 +28,25 @@ def display_and_manage_cosmos_db():
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__":
 
1
  import os
2
+ import pandas as pd
3
  import streamlit as st
4
+ from azure.cosmos import CosmosClient
5
  from azure.cosmos.exceptions import CosmosResourceNotFoundError
6
 
7
  # Initialize Azure Cosmos DB Client
 
9
  cosmos_client = CosmosClient.from_connection_string(COSMOS_CONNECTION_STRING)
10
 
11
  # Function to Delete an Item in Cosmos DB
12
+ def delete_cosmos_item(db_name, container_name, item_id):
13
  try:
14
  database_client = cosmos_client.get_database_client(db_name)
15
  container_client = database_client.get_container_client(container_name)
 
18
  except CosmosResourceNotFoundError:
19
  st.error(f"Item not found: {item_id}")
20
 
21
+ # Load Cosmos DB Data into a DataFrame
22
+ def load_cosmos_data_into_dataframe():
23
+ data = []
24
  for db_properties in cosmos_client.list_databases():
25
  db_name = db_properties['id']
26
  database_client = cosmos_client.get_database_client(db_name)
 
28
  container_name = container_properties['id']
29
  container_client = database_client.get_container_client(container_name)
30
  for item in container_client.read_all_items():
31
+ data.append(item)
32
+ return pd.DataFrame(data)
33
+
34
+ # Display and Manage Cosmos DB Data
35
+ def display_and_manage_cosmos_data(df):
36
+ st.subheader('Azure Cosmos DB Data')
37
+ for index, row in df.iterrows():
38
+ st.json(row.to_json()) # Display the full data of the item
39
+ if 'file_name' in row and os.path.isfile(row['file_name']):
40
+ st.image(row['file_name']) # Show image if available
41
+ delete_button_key = f"delete_{row['id']}"
42
+ if st.button(f"🗑️ Delete {row['id']}", key=delete_button_key):
43
+ delete_cosmos_item(row['_database_id'], row['_container_id'], row['id'])
44
+ st.experimental_rerun()
45
 
46
  # Main UI Function
47
  def main():
48
+ df = load_cosmos_data_into_dataframe()
49
+ display_and_manage_cosmos_data(df)
50
 
51
  # Run the main function
52
  if __name__ == "__main__":