Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,10 @@ from azure.cosmos import CosmosClient, PartitionKey
|
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
-
# Streamlit page configuration for wide mode
|
| 7 |
-
st.set_page_config(layout="wide")
|
| 8 |
-
|
| 9 |
# Cosmos DB configuration
|
| 10 |
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
| 11 |
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
|
|
|
| 12 |
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
| 13 |
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
| 14 |
Key = os.environ.get("Key")
|
|
@@ -49,26 +47,20 @@ def delete_records(ids):
|
|
| 49 |
except Exception as e:
|
| 50 |
return False, f"Error deleting records: {str(e)}"
|
| 51 |
|
| 52 |
-
def delete_all_records():
|
| 53 |
-
try:
|
| 54 |
-
query = "SELECT c.id FROM c"
|
| 55 |
-
items = list(container.query_items(query=query, enable_cross_partition_query=True))
|
| 56 |
-
for item in items:
|
| 57 |
-
container.delete_item(item=item['id'], partition_key=item['id'])
|
| 58 |
-
return True, f"Successfully deleted all records"
|
| 59 |
-
except Exception as e:
|
| 60 |
-
return False, f"Error deleting all records: {str(e)}"
|
| 61 |
-
|
| 62 |
# Streamlit app
|
| 63 |
st.title("π Cosmos DB Record Management")
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
# Login section
|
| 66 |
if 'logged_in' not in st.session_state:
|
| 67 |
st.session_state.logged_in = False
|
| 68 |
|
| 69 |
if not st.session_state.logged_in:
|
| 70 |
st.subheader("π Login")
|
| 71 |
-
input_key = Key
|
| 72 |
if st.button("π Login"):
|
| 73 |
if input_key:
|
| 74 |
st.session_state.primary_key = input_key
|
|
@@ -82,135 +74,103 @@ else:
|
|
| 82 |
database = client.get_database_client(DATABASE_NAME)
|
| 83 |
container = database.get_container_client(CONTAINER_NAME)
|
| 84 |
|
| 85 |
-
# Fetch all records
|
| 86 |
-
records_df = fetch_all_records()
|
| 87 |
-
|
| 88 |
-
# Sidebar for data editing
|
| 89 |
-
st.sidebar.title("Edit Records")
|
| 90 |
-
edited_df = st.sidebar.data_editor(records_df, num_rows="dynamic", key="sidebar_editor")
|
| 91 |
-
|
| 92 |
-
# Main Content Area
|
| 93 |
-
col1, col2 = st.columns([2, 1])
|
| 94 |
-
|
| 95 |
-
with col1:
|
| 96 |
-
st.subheader("π Record Details")
|
| 97 |
-
id = st.text_input("ID")
|
| 98 |
-
name = st.text_input("Name")
|
| 99 |
-
document = st.text_area("Document")
|
| 100 |
-
evaluation_text = st.text_area("Evaluation Text")
|
| 101 |
-
evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
| 102 |
-
|
| 103 |
-
col3, col4, col5 = st.columns(3)
|
| 104 |
-
with col3:
|
| 105 |
-
if st.button("πΎ Insert Record"):
|
| 106 |
-
record = {
|
| 107 |
-
"id": id,
|
| 108 |
-
"name": name,
|
| 109 |
-
"document": document,
|
| 110 |
-
"evaluationText": evaluation_text,
|
| 111 |
-
"evaluationScore": evaluation_score
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
success, response = insert_record(record)
|
| 115 |
-
if success:
|
| 116 |
-
st.success("β
Record inserted successfully!")
|
| 117 |
-
st.json(response)
|
| 118 |
-
else:
|
| 119 |
-
st.error(f"β Failed to insert record: {response}")
|
| 120 |
-
|
| 121 |
-
with col4:
|
| 122 |
-
if st.button("π§ Call Procedure"):
|
| 123 |
-
record = {
|
| 124 |
-
"id": id,
|
| 125 |
-
"name": name,
|
| 126 |
-
"document": document,
|
| 127 |
-
"evaluationText": evaluation_text,
|
| 128 |
-
"evaluationScore": evaluation_score
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
success, response = call_stored_procedure(record)
|
| 132 |
-
if success:
|
| 133 |
-
st.success("β
Stored procedure executed successfully!")
|
| 134 |
-
st.json(response)
|
| 135 |
-
else:
|
| 136 |
-
st.error(f"β Failed to execute stored procedure: {response}")
|
| 137 |
-
|
| 138 |
-
with col5:
|
| 139 |
-
if st.button("ποΈ Delete All"):
|
| 140 |
-
success, message = delete_all_records()
|
| 141 |
-
if success:
|
| 142 |
-
st.success(message)
|
| 143 |
-
else:
|
| 144 |
-
st.error(message)
|
| 145 |
-
st.rerun()
|
| 146 |
-
|
| 147 |
-
with col2:
|
| 148 |
-
st.subheader("π Selected Record")
|
| 149 |
-
selected_rows = edited_df[edited_df['select'] == True]
|
| 150 |
-
if not selected_rows.empty:
|
| 151 |
-
st.json(selected_rows.iloc[0].to_dict())
|
| 152 |
-
else:
|
| 153 |
-
st.write("No record selected")
|
| 154 |
-
|
| 155 |
st.subheader("π All Records")
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
with col6:
|
| 180 |
if st.button("ποΈ Delete Selected"):
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
success, message = delete_records(selected_ids)
|
| 184 |
if success:
|
| 185 |
st.success(message)
|
|
|
|
| 186 |
else:
|
| 187 |
st.error(message)
|
| 188 |
st.rerun()
|
| 189 |
else:
|
| 190 |
st.warning("No records selected for deletion.")
|
| 191 |
|
| 192 |
-
with
|
| 193 |
-
if st.download_button("π₯ Download Data",
|
| 194 |
st.success("Data downloaded successfully!")
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
# Logout button
|
| 197 |
if st.button("πͺ Logout"):
|
| 198 |
st.session_state.logged_in = False
|
|
|
|
| 199 |
st.rerun()
|
| 200 |
|
| 201 |
# Display connection info
|
| 202 |
-
st.sidebar.markdown("---")
|
| 203 |
st.sidebar.subheader("π Connection Information")
|
| 204 |
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
| 205 |
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
| 206 |
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
| 207 |
-
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
| 208 |
-
|
| 209 |
-
# Update fields when a record is selected in the sidebar
|
| 210 |
-
if st.session_state.logged_in and not edited_df[edited_df['select'] == True].empty:
|
| 211 |
-
selected_record = edited_df[edited_df['select'] == True].iloc[0]
|
| 212 |
-
st.session_state['id'] = selected_record['id']
|
| 213 |
-
st.session_state['name'] = selected_record['name']
|
| 214 |
-
st.session_state['document'] = selected_record['document']
|
| 215 |
-
st.session_state['evaluation_text'] = selected_record['evaluationText']
|
| 216 |
-
st.session_state['evaluation_score'] = selected_record['evaluationScore']
|
|
|
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
# Cosmos DB configuration
|
| 7 |
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
| 8 |
SUBSCRIPTION_ID = "003fba60-5b3f-48f4-ab36-3ed11bc40816"
|
| 9 |
+
# You'll need to set these environment variables or use Azure Key Vault
|
| 10 |
DATABASE_NAME = os.environ.get("COSMOS_DATABASE_NAME")
|
| 11 |
CONTAINER_NAME = os.environ.get("COSMOS_CONTAINER_NAME")
|
| 12 |
Key = os.environ.get("Key")
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
return False, f"Error deleting records: {str(e)}"
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# Streamlit app
|
| 51 |
st.title("π Cosmos DB Record Management")
|
| 52 |
|
| 53 |
+
# Initialize session state for selected IDs
|
| 54 |
+
if 'selected_ids' not in st.session_state:
|
| 55 |
+
st.session_state.selected_ids = set()
|
| 56 |
+
|
| 57 |
# Login section
|
| 58 |
if 'logged_in' not in st.session_state:
|
| 59 |
st.session_state.logged_in = False
|
| 60 |
|
| 61 |
if not st.session_state.logged_in:
|
| 62 |
st.subheader("π Login")
|
| 63 |
+
input_key = Key # Use the predefined Key instead of asking for user input
|
| 64 |
if st.button("π Login"):
|
| 65 |
if input_key:
|
| 66 |
st.session_state.primary_key = input_key
|
|
|
|
| 74 |
database = client.get_database_client(DATABASE_NAME)
|
| 75 |
container = database.get_container_client(CONTAINER_NAME)
|
| 76 |
|
| 77 |
+
# Fetch and display all records
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
st.subheader("π All Records")
|
| 79 |
+
df = fetch_all_records()
|
| 80 |
+
|
| 81 |
+
# Add a checkbox column to the dataframe
|
| 82 |
+
df['select'] = df['id'].isin(st.session_state.selected_ids)
|
| 83 |
+
|
| 84 |
+
# Use Streamlit's data editor
|
| 85 |
+
edited_df = st.data_editor(df, key="data_editor", disabled=["id", "name", "document", "evaluationText", "evaluationScore"])
|
| 86 |
+
|
| 87 |
+
# Update selected_ids based on the edited dataframe
|
| 88 |
+
selected_rows = edited_df[edited_df['select']]
|
| 89 |
+
st.session_state.selected_ids = set(selected_rows['id'])
|
| 90 |
+
|
| 91 |
+
# Display selected IDs with emoji checkboxes
|
| 92 |
+
if st.session_state.selected_ids:
|
| 93 |
+
st.markdown("### Selected Records:")
|
| 94 |
+
for id in st.session_state.selected_ids:
|
| 95 |
+
st.markdown(f"β
{id}")
|
| 96 |
+
else:
|
| 97 |
+
st.markdown("### No Records Selected")
|
| 98 |
+
|
| 99 |
+
# Add delete and download buttons
|
| 100 |
+
col1, col2 = st.columns(2)
|
| 101 |
+
with col1:
|
|
|
|
| 102 |
if st.button("ποΈ Delete Selected"):
|
| 103 |
+
if st.session_state.selected_ids:
|
| 104 |
+
success, message = delete_records(list(st.session_state.selected_ids))
|
|
|
|
| 105 |
if success:
|
| 106 |
st.success(message)
|
| 107 |
+
st.session_state.selected_ids.clear() # Clear the selection after successful deletion
|
| 108 |
else:
|
| 109 |
st.error(message)
|
| 110 |
st.rerun()
|
| 111 |
else:
|
| 112 |
st.warning("No records selected for deletion.")
|
| 113 |
|
| 114 |
+
with col2:
|
| 115 |
+
if st.download_button("π₯ Download Data", df.to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
|
| 116 |
st.success("Data downloaded successfully!")
|
| 117 |
|
| 118 |
+
# Input fields for new record
|
| 119 |
+
st.subheader("π Enter New Record Details")
|
| 120 |
+
new_id = st.text_input("ID")
|
| 121 |
+
new_name = st.text_input("Name")
|
| 122 |
+
new_document = st.text_area("Document")
|
| 123 |
+
new_evaluation_text = st.text_area("Evaluation Text")
|
| 124 |
+
new_evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
| 125 |
+
|
| 126 |
+
col1, col2 = st.columns(2)
|
| 127 |
+
|
| 128 |
+
# Insert Record button
|
| 129 |
+
with col1:
|
| 130 |
+
if st.button("πΎ Insert Record"):
|
| 131 |
+
record = {
|
| 132 |
+
"id": new_id,
|
| 133 |
+
"name": new_name,
|
| 134 |
+
"document": new_document,
|
| 135 |
+
"evaluationText": new_evaluation_text,
|
| 136 |
+
"evaluationScore": new_evaluation_score
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
success, response = insert_record(record)
|
| 140 |
+
if success:
|
| 141 |
+
st.success("β
Record inserted successfully!")
|
| 142 |
+
st.json(response)
|
| 143 |
+
else:
|
| 144 |
+
st.error(f"β Failed to insert record: {response}")
|
| 145 |
+
st.rerun()
|
| 146 |
+
|
| 147 |
+
# Call Procedure button
|
| 148 |
+
with col2:
|
| 149 |
+
if st.button("π§ Call Procedure"):
|
| 150 |
+
record = {
|
| 151 |
+
"id": new_id,
|
| 152 |
+
"name": new_name,
|
| 153 |
+
"document": new_document,
|
| 154 |
+
"evaluationText": new_evaluation_text,
|
| 155 |
+
"evaluationScore": new_evaluation_score
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
success, response = call_stored_procedure(record)
|
| 159 |
+
if success:
|
| 160 |
+
st.success("β
Stored procedure executed successfully!")
|
| 161 |
+
st.json(response)
|
| 162 |
+
else:
|
| 163 |
+
st.error(f"β Failed to execute stored procedure: {response}")
|
| 164 |
+
|
| 165 |
# Logout button
|
| 166 |
if st.button("πͺ Logout"):
|
| 167 |
st.session_state.logged_in = False
|
| 168 |
+
st.session_state.selected_ids.clear() # Clear selected IDs on logout
|
| 169 |
st.rerun()
|
| 170 |
|
| 171 |
# Display connection info
|
|
|
|
| 172 |
st.sidebar.subheader("π Connection Information")
|
| 173 |
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
| 174 |
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
| 175 |
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
| 176 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|