awacke1 commited on
Commit
818d24c
โ€ข
1 Parent(s): b05ef13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -2
app.py CHANGED
@@ -196,6 +196,52 @@ def archive_all_data(client):
196
  except Exception as e:
197
  return f"An error occurred while archiving data: {str(e)}"
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  # Main Streamlit app
200
  def main():
201
  st.set_page_config(layout="wide")
@@ -212,14 +258,14 @@ def main():
212
  # Login section
213
  if not st.session_state.logged_in:
214
  st.subheader("๐Ÿ” Login")
215
- input_key = st.text_input("Enter your Cosmos DB key", type="password")
216
  if st.button("๐Ÿš€ Login"):
217
  if input_key:
218
  st.session_state.primary_key = input_key
219
  st.session_state.logged_in = True
220
  st.rerun()
221
  else:
222
- st.error("Invalid key. Please enter a valid Cosmos DB key.")
223
  else:
224
  # Initialize Cosmos DB client
225
  try:
 
196
  except Exception as e:
197
  return f"An error occurred while archiving data: {str(e)}"
198
 
199
+ # Cosmos DB operations
200
+ def create_item(client, db_name, container_name):
201
+ st.subheader("Create New Item")
202
+ item_id = st.text_input("Item ID")
203
+ item_data = st.text_area("Item Data (JSON format)")
204
+
205
+ if st.button("๐Ÿ’พ Save New Item"):
206
+ try:
207
+ container = client.get_database_client(db_name).get_container_client(container_name)
208
+ new_item = {"id": item_id, **json.loads(item_data)}
209
+ container.create_item(body=new_item)
210
+ st.success("New item created successfully!")
211
+ st.rerun()
212
+ except Exception as e:
213
+ st.error(f"Error creating item: {str(e)}")
214
+
215
+ def edit_item(client, db_name, container_name, item_id):
216
+ container = client.get_database_client(db_name).get_container_client(container_name)
217
+ item = container.read_item(item=item_id, partition_key=item_id)
218
+
219
+ st.subheader(f"Editing Item: {item_id}")
220
+ edited_item = {}
221
+ for key, value in item.items():
222
+ if key not in ['_rid', '_self', '_etag', '_attachments', '_ts']:
223
+ edited_item[key] = st.text_input(key, value)
224
+
225
+ if st.button("๐Ÿ’พ Save Changes"):
226
+ try:
227
+ container.upsert_item(body=edited_item)
228
+ st.success("Item updated successfully!")
229
+ st.rerun()
230
+ except Exception as e:
231
+ st.error(f"Error updating item: {str(e)}")
232
+
233
+ def delete_item(client, db_name, container_name, item_id):
234
+ st.subheader(f"Delete Item: {item_id}")
235
+ st.write("Are you sure you want to delete this item?")
236
+ if st.button("๐Ÿ—‘๏ธ Confirm Delete"):
237
+ try:
238
+ container = client.get_database_client(db_name).get_container_client(container_name)
239
+ container.delete_item(item=item_id, partition_key=item_id)
240
+ st.success(f"Item {item_id} deleted successfully!")
241
+ st.rerun()
242
+ except Exception as e:
243
+ st.error(f"Error deleting item: {str(e)}")
244
+
245
  # Main Streamlit app
246
  def main():
247
  st.set_page_config(layout="wide")
 
258
  # Login section
259
  if not st.session_state.logged_in:
260
  st.subheader("๐Ÿ” Login")
261
+ input_key = Key # Get the key from the environment variable
262
  if st.button("๐Ÿš€ Login"):
263
  if input_key:
264
  st.session_state.primary_key = input_key
265
  st.session_state.logged_in = True
266
  st.rerun()
267
  else:
268
+ st.error("Invalid key. Please check your environment variables.")
269
  else:
270
  # Initialize Cosmos DB client
271
  try: