Spaces:
Running
Running
Delete backup.0532pm.app.py
Browse files- backup.0532pm.app.py +0 -176
backup.0532pm.app.py
DELETED
@@ -1,176 +0,0 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from azure.cosmos import CosmosClient, PartitionKey
|
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")
|
13 |
-
|
14 |
-
def insert_record(record):
|
15 |
-
try:
|
16 |
-
response = container.create_item(body=record)
|
17 |
-
return True, response
|
18 |
-
except Exception as e:
|
19 |
-
return False, str(e)
|
20 |
-
|
21 |
-
def call_stored_procedure(record):
|
22 |
-
try:
|
23 |
-
response = container.scripts.execute_stored_procedure(
|
24 |
-
sproc="processPrompt",
|
25 |
-
params=[record],
|
26 |
-
partition_key=record['id']
|
27 |
-
)
|
28 |
-
return True, response
|
29 |
-
except Exception as e:
|
30 |
-
error_message = f"Error type: {type(e).__name__}\nError message: {str(e)}"
|
31 |
-
if hasattr(e, 'sub_status'):
|
32 |
-
error_message += f"\nSub-status: {e.sub_status}"
|
33 |
-
if hasattr(e, 'response'):
|
34 |
-
error_message += f"\nResponse: {e.response}"
|
35 |
-
return False, error_message
|
36 |
-
|
37 |
-
def fetch_all_records():
|
38 |
-
query = "SELECT * FROM c"
|
39 |
-
items = list(container.query_items(query=query, enable_cross_partition_query=True))
|
40 |
-
return pd.DataFrame(items)
|
41 |
-
|
42 |
-
def delete_records(ids):
|
43 |
-
try:
|
44 |
-
for id in ids:
|
45 |
-
container.delete_item(item=id, partition_key=id)
|
46 |
-
return True, f"Successfully deleted {len(ids)} records"
|
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
|
67 |
-
st.session_state.logged_in = True
|
68 |
-
st.rerun()
|
69 |
-
else:
|
70 |
-
st.error("Invalid key. Please check your environment variables.")
|
71 |
-
else:
|
72 |
-
# Initialize Cosmos DB client
|
73 |
-
client = CosmosClient(ENDPOINT, credential=st.session_state.primary_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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|