Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from azure.cosmos import CosmosClient, PartitionKey
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
|
|
@@ -17,8 +17,10 @@ def insert_record(record):
|
|
| 17 |
try:
|
| 18 |
response = container.create_item(body=record)
|
| 19 |
return True, response
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
return False, str(e)
|
| 22 |
|
| 23 |
def call_stored_procedure(record):
|
| 24 |
try:
|
|
@@ -28,25 +30,35 @@ def call_stored_procedure(record):
|
|
| 28 |
partition_key=record['id']
|
| 29 |
)
|
| 30 |
return True, response
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
error_message = f"
|
| 33 |
-
if hasattr(e, 'sub_status'):
|
| 34 |
-
error_message += f"\nSub-status: {e.sub_status}"
|
| 35 |
-
if hasattr(e, 'response'):
|
| 36 |
-
error_message += f"\nResponse: {e.response}"
|
| 37 |
return False, error_message
|
| 38 |
|
| 39 |
def fetch_all_records():
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def delete_record(name, id):
|
| 45 |
try:
|
| 46 |
container.delete_item(item=id, partition_key=id)
|
| 47 |
return True, f"Successfully deleted record with name: {name} and id: {id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
return False, f"
|
| 50 |
|
| 51 |
# Streamlit app
|
| 52 |
st.title("🌟 Cosmos DB Record Management")
|
|
@@ -71,31 +83,46 @@ if not st.session_state.logged_in:
|
|
| 71 |
st.error("Invalid key. Please check your environment variables.")
|
| 72 |
else:
|
| 73 |
# Initialize Cosmos DB client
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# Fetch and display all records
|
| 79 |
st.subheader("📊 All Records")
|
| 80 |
df = fetch_all_records()
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# Display selected records
|
| 101 |
st.subheader("Selected Records")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from azure.cosmos import CosmosClient, PartitionKey, exceptions
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
| 17 |
try:
|
| 18 |
response = container.create_item(body=record)
|
| 19 |
return True, response
|
| 20 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 21 |
+
return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
| 22 |
except Exception as e:
|
| 23 |
+
return False, f"An unexpected error occurred: {str(e)}"
|
| 24 |
|
| 25 |
def call_stored_procedure(record):
|
| 26 |
try:
|
|
|
|
| 30 |
partition_key=record['id']
|
| 31 |
)
|
| 32 |
return True, response
|
| 33 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 34 |
+
error_message = f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
| 35 |
+
return False, error_message
|
| 36 |
except Exception as e:
|
| 37 |
+
error_message = f"An unexpected error occurred: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return False, error_message
|
| 39 |
|
| 40 |
def fetch_all_records():
|
| 41 |
+
try:
|
| 42 |
+
query = "SELECT * FROM c"
|
| 43 |
+
items = list(container.query_items(query=query, enable_cross_partition_query=True))
|
| 44 |
+
return pd.DataFrame(items)
|
| 45 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 46 |
+
st.error(f"HTTP error occurred while fetching records: {str(e)}. Status code: {e.status_code}")
|
| 47 |
+
return pd.DataFrame()
|
| 48 |
+
except Exception as e:
|
| 49 |
+
st.error(f"An unexpected error occurred while fetching records: {str(e)}")
|
| 50 |
+
return pd.DataFrame()
|
| 51 |
|
| 52 |
def delete_record(name, id):
|
| 53 |
try:
|
| 54 |
container.delete_item(item=id, partition_key=id)
|
| 55 |
return True, f"Successfully deleted record with name: {name} and id: {id}"
|
| 56 |
+
except exceptions.CosmosResourceNotFoundError:
|
| 57 |
+
return False, f"Record with id {id} not found. It may have been already deleted."
|
| 58 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 59 |
+
return False, f"HTTP error occurred: {str(e)}. Status code: {e.status_code}"
|
| 60 |
except Exception as e:
|
| 61 |
+
return False, f"An unexpected error occurred: {str(e)}"
|
| 62 |
|
| 63 |
# Streamlit app
|
| 64 |
st.title("🌟 Cosmos DB Record Management")
|
|
|
|
| 83 |
st.error("Invalid key. Please check your environment variables.")
|
| 84 |
else:
|
| 85 |
# Initialize Cosmos DB client
|
| 86 |
+
try:
|
| 87 |
+
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_key)
|
| 88 |
+
database = client.get_database_client(DATABASE_NAME)
|
| 89 |
+
container = database.get_container_client(CONTAINER_NAME)
|
| 90 |
+
except exceptions.CosmosHttpResponseError as e:
|
| 91 |
+
st.error(f"Failed to connect to Cosmos DB. HTTP error: {str(e)}. Status code: {e.status_code}")
|
| 92 |
+
st.stop()
|
| 93 |
+
except Exception as e:
|
| 94 |
+
st.error(f"An unexpected error occurred while connecting to Cosmos DB: {str(e)}")
|
| 95 |
+
st.stop()
|
| 96 |
|
| 97 |
# Fetch and display all records
|
| 98 |
st.subheader("📊 All Records")
|
| 99 |
df = fetch_all_records()
|
| 100 |
|
| 101 |
+
if df.empty:
|
| 102 |
+
st.write("No records found in the database.")
|
| 103 |
+
else:
|
| 104 |
+
for index, row in df.iterrows():
|
| 105 |
+
col1, col2, col3 = st.columns([3, 1, 1])
|
| 106 |
+
|
| 107 |
+
with col1:
|
| 108 |
+
st.write(f"ID: {row['id']}, Name: {row['name']}")
|
| 109 |
+
|
| 110 |
+
with col2:
|
| 111 |
+
key = f"select_{row['id']}"
|
| 112 |
+
if st.checkbox("Select", key=key, value=row.to_dict() in st.session_state.selected_records):
|
| 113 |
+
if row.to_dict() not in st.session_state.selected_records:
|
| 114 |
+
st.session_state.selected_records.append(row.to_dict())
|
| 115 |
+
else:
|
| 116 |
+
st.session_state.selected_records = [r for r in st.session_state.selected_records if r['id'] != row['id']]
|
| 117 |
+
|
| 118 |
+
with col3:
|
| 119 |
+
if st.button(f"Delete", key=f"delete_{row['id']}"):
|
| 120 |
+
success, message = delete_record(row['name'], row['id'])
|
| 121 |
+
if success:
|
| 122 |
+
st.success(message)
|
| 123 |
+
st.rerun()
|
| 124 |
+
else:
|
| 125 |
+
st.error(message)
|
| 126 |
|
| 127 |
# Display selected records
|
| 128 |
st.subheader("Selected Records")
|