awacke1 commited on
Commit
8759ff7
·
1 Parent(s): fb72835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -27
app.py CHANGED
@@ -40,36 +40,43 @@ def display_cosmos_db_structure():
40
  else:
41
  st.markdown(item_desc)
42
 
43
- # Function to Add or Update an Item
44
- def add_or_update_item(database_name, container_name, item_id, item_data):
 
45
  container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- # Check if item exists
48
- query = "SELECT * FROM c WHERE c.id = @id"
49
- parameters = [{"name": "@id", "value": item_id}]
50
- items = list(container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
51
-
52
- if items:
53
- # Item exists, delete it
54
- container.delete_item(item=items[0], partition_key=item_id)
55
-
56
- # Add new item
57
- container.create_item(body=item_data)
58
- st.write(f"Added Item: {item_id}")
59
-
60
- # Display item ID and image if available
61
- display_item(database_name, container_name, item_id)
62
-
63
-
64
-
65
- # Function to Display an Item
66
- def display_item(database_name, container_name, item_id):
67
- container = cosmos_client.get_database_client(database_name).get_container_client(container_name)
68
- item = container.read_item(item_id, partition_key=PartitionKey(item_id))
69
- st.markdown(f"- **Item ID**: {item_id}")
70
- if 'file_name' in item and item['file_name'].endswith('.png'):
71
- st.image(item['file_name'], caption=f"Image: {item['file_name']}")
72
 
 
 
 
 
 
 
73
 
74
  # Button to Trigger Display of Cosmos DB Structure
75
  if st.button('Show 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'):