Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,9 @@ import streamlit as st
|
|
| 2 |
from azure.cosmos import CosmosClient, PartitionKey
|
| 3 |
import os
|
| 4 |
import pandas as pd
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Cosmos DB configuration
|
| 8 |
ENDPOINT = "https://acae-afd.documents.azure.com:443/"
|
|
@@ -47,69 +49,6 @@ def delete_records(ids):
|
|
| 47 |
except Exception as e:
|
| 48 |
return False, f"Error deleting records: {str(e)}"
|
| 49 |
|
| 50 |
-
def create_html_component(records):
|
| 51 |
-
html_template = """
|
| 52 |
-
<style>
|
| 53 |
-
.record-list {
|
| 54 |
-
font-family: Arial, sans-serif;
|
| 55 |
-
}
|
| 56 |
-
.record-item {
|
| 57 |
-
display: flex;
|
| 58 |
-
align-items: center;
|
| 59 |
-
margin-bottom: 10px;
|
| 60 |
-
padding: 10px;
|
| 61 |
-
border: 1px solid #ddd;
|
| 62 |
-
border-radius: 5px;
|
| 63 |
-
}
|
| 64 |
-
.record-image {
|
| 65 |
-
width: 50px;
|
| 66 |
-
height: 50px;
|
| 67 |
-
margin-right: 10px;
|
| 68 |
-
}
|
| 69 |
-
.record-details {
|
| 70 |
-
flex-grow: 1;
|
| 71 |
-
}
|
| 72 |
-
.record-title {
|
| 73 |
-
font-weight: bold;
|
| 74 |
-
}
|
| 75 |
-
.record-description {
|
| 76 |
-
font-style: italic;
|
| 77 |
-
color: #666;
|
| 78 |
-
}
|
| 79 |
-
.record-actions {
|
| 80 |
-
display: flex;
|
| 81 |
-
align-items: center;
|
| 82 |
-
}
|
| 83 |
-
.record-actions button {
|
| 84 |
-
margin-left: 5px;
|
| 85 |
-
}
|
| 86 |
-
</style>
|
| 87 |
-
<div class="record-list">
|
| 88 |
-
"""
|
| 89 |
-
|
| 90 |
-
for record in records:
|
| 91 |
-
html_template += f"""
|
| 92 |
-
<div class="record-item">
|
| 93 |
-
<img src="https://via.placeholder.com/50" class="record-image" alt="Record thumbnail">
|
| 94 |
-
<div class="record-details">
|
| 95 |
-
<div class="record-title">{record['name']}</div>
|
| 96 |
-
<div class="record-description">{record['document']}</div>
|
| 97 |
-
</div>
|
| 98 |
-
<div class="record-actions">
|
| 99 |
-
<button onclick="Streamlit.setComponentValue({{action: 'extend', id: '{record['id']}'}})">Extend</button>
|
| 100 |
-
<label>
|
| 101 |
-
Public
|
| 102 |
-
<input type="checkbox" onclick="Streamlit.setComponentValue({{action: 'toggle_public', id: '{record['id']}'}})">
|
| 103 |
-
</label>
|
| 104 |
-
<button onclick="Streamlit.setComponentValue({{action: 'like', id: '{record['id']}'}})">👍</button>
|
| 105 |
-
<button onclick="Streamlit.setComponentValue({{action: 'dislike', id: '{record['id']}'}})">👎</button>
|
| 106 |
-
</div>
|
| 107 |
-
</div>
|
| 108 |
-
"""
|
| 109 |
-
|
| 110 |
-
html_template += "</div>"
|
| 111 |
-
return html_template
|
| 112 |
-
|
| 113 |
# Streamlit app
|
| 114 |
st.title("🌟 Cosmos DB Record Management")
|
| 115 |
|
|
@@ -141,57 +80,23 @@ else:
|
|
| 141 |
edited_df = st.sidebar.data_editor(records_df, num_rows="dynamic", key="sidebar_editor")
|
| 142 |
|
| 143 |
# Main Content Area
|
| 144 |
-
st.subheader("📊 All Records")
|
| 145 |
-
|
| 146 |
-
# Create and render the HTML component
|
| 147 |
-
html_component = create_html_component(edited_df.to_dict('records'))
|
| 148 |
-
component_value = html(html_component, height=400)
|
| 149 |
-
|
| 150 |
-
# Handle component interactions
|
| 151 |
-
if component_value:
|
| 152 |
-
try:
|
| 153 |
-
action = component_value['action']
|
| 154 |
-
record_id = component_value['id']
|
| 155 |
-
if action == 'extend':
|
| 156 |
-
st.write(f"Extending record {record_id}")
|
| 157 |
-
elif action == 'toggle_public':
|
| 158 |
-
st.write(f"Toggling public status for record {record_id}")
|
| 159 |
-
elif action == 'like':
|
| 160 |
-
st.write(f"Liked record {record_id}")
|
| 161 |
-
elif action == 'dislike':
|
| 162 |
-
st.write(f"Disliked record {record_id}")
|
| 163 |
-
except KeyError:
|
| 164 |
-
st.write("Interaction detected, but action or id not found.")
|
| 165 |
-
|
| 166 |
-
# Add delete and download buttons
|
| 167 |
col1, col2 = st.columns(2)
|
| 168 |
-
with col1:
|
| 169 |
-
if st.button("🗑️ Delete Selected"):
|
| 170 |
-
st.warning("Deletion functionality needs to be implemented for the new component")
|
| 171 |
|
| 172 |
-
with col2:
|
| 173 |
-
if st.download_button("📥 Download Data", edited_df.to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
|
| 174 |
-
st.success("Data downloaded successfully!")
|
| 175 |
-
|
| 176 |
-
# Input fields for new record
|
| 177 |
-
st.subheader("📝 Enter New Record Details")
|
| 178 |
-
new_id = st.text_input("ID")
|
| 179 |
-
new_name = st.text_input("Name")
|
| 180 |
-
new_document = st.text_area("Document")
|
| 181 |
-
new_evaluation_text = st.text_area("Evaluation Text")
|
| 182 |
-
new_evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
| 183 |
-
|
| 184 |
-
col1, col2 = st.columns(2)
|
| 185 |
-
|
| 186 |
-
# Insert Record button
|
| 187 |
with col1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
if st.button("💾 Insert Record"):
|
| 189 |
record = {
|
| 190 |
-
"id":
|
| 191 |
-
"name":
|
| 192 |
-
"document":
|
| 193 |
-
"evaluationText":
|
| 194 |
-
"evaluationScore":
|
| 195 |
}
|
| 196 |
|
| 197 |
success, response = insert_record(record)
|
|
@@ -200,17 +105,16 @@ else:
|
|
| 200 |
st.json(response)
|
| 201 |
else:
|
| 202 |
st.error(f"❌ Failed to insert record: {response}")
|
| 203 |
-
st.rerun()
|
| 204 |
|
| 205 |
-
# Call Procedure button
|
| 206 |
with col2:
|
| 207 |
-
|
|
|
|
| 208 |
record = {
|
| 209 |
-
"id":
|
| 210 |
-
"name":
|
| 211 |
-
"document":
|
| 212 |
-
"evaluationText":
|
| 213 |
-
"evaluationScore":
|
| 214 |
}
|
| 215 |
|
| 216 |
success, response = call_stored_procedure(record)
|
|
@@ -220,6 +124,27 @@ else:
|
|
| 220 |
else:
|
| 221 |
st.error(f"❌ Failed to execute stored procedure: {response}")
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
# Logout button
|
| 224 |
if st.button("🚪 Logout"):
|
| 225 |
st.session_state.logged_in = False
|
|
@@ -231,8 +156,4 @@ else:
|
|
| 231 |
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
| 232 |
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
| 233 |
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
| 234 |
-
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
| 235 |
-
|
| 236 |
-
# Preview section
|
| 237 |
-
st.markdown("---")
|
| 238 |
-
st.markdown("### Select a record to preview.")
|
|
|
|
| 2 |
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/"
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
return False, f"Error deleting records: {str(e)}"
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Streamlit app
|
| 53 |
st.title("🌟 Cosmos DB Record Management")
|
| 54 |
|
|
|
|
| 80 |
edited_df = st.sidebar.data_editor(records_df, num_rows="dynamic", key="sidebar_editor")
|
| 81 |
|
| 82 |
# Main Content Area
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
with col1:
|
| 86 |
+
st.subheader("📝 Enter Record Details")
|
| 87 |
+
id = st.text_input("ID")
|
| 88 |
+
name = st.text_input("Name")
|
| 89 |
+
document = st.text_area("Document")
|
| 90 |
+
evaluation_text = st.text_area("Evaluation Text")
|
| 91 |
+
evaluation_score = st.number_input("Evaluation Score", min_value=0, max_value=100, step=1)
|
| 92 |
+
|
| 93 |
if st.button("💾 Insert Record"):
|
| 94 |
record = {
|
| 95 |
+
"id": id,
|
| 96 |
+
"name": name,
|
| 97 |
+
"document": document,
|
| 98 |
+
"evaluationText": evaluation_text,
|
| 99 |
+
"evaluationScore": evaluation_score
|
| 100 |
}
|
| 101 |
|
| 102 |
success, response = insert_record(record)
|
|
|
|
| 105 |
st.json(response)
|
| 106 |
else:
|
| 107 |
st.error(f"❌ Failed to insert record: {response}")
|
|
|
|
| 108 |
|
|
|
|
| 109 |
with col2:
|
| 110 |
+
st.subheader("🔧 Call Stored Procedure")
|
| 111 |
+
if st.button("Call Procedure"):
|
| 112 |
record = {
|
| 113 |
+
"id": id,
|
| 114 |
+
"name": name,
|
| 115 |
+
"document": document,
|
| 116 |
+
"evaluationText": evaluation_text,
|
| 117 |
+
"evaluationScore": evaluation_score
|
| 118 |
}
|
| 119 |
|
| 120 |
success, response = call_stored_procedure(record)
|
|
|
|
| 124 |
else:
|
| 125 |
st.error(f"❌ Failed to execute stored procedure: {response}")
|
| 126 |
|
| 127 |
+
st.subheader("📊 All Records")
|
| 128 |
+
st.dataframe(edited_df)
|
| 129 |
+
|
| 130 |
+
col3, col4 = st.columns(2)
|
| 131 |
+
with col3:
|
| 132 |
+
if st.button("🗑️ Delete Selected"):
|
| 133 |
+
selected_ids = edited_df[edited_df['select']]['id'].tolist() if 'select' in edited_df.columns else []
|
| 134 |
+
if selected_ids:
|
| 135 |
+
success, message = delete_records(selected_ids)
|
| 136 |
+
if success:
|
| 137 |
+
st.success(message)
|
| 138 |
+
else:
|
| 139 |
+
st.error(message)
|
| 140 |
+
st.rerun()
|
| 141 |
+
else:
|
| 142 |
+
st.warning("No records selected for deletion.")
|
| 143 |
+
|
| 144 |
+
with col4:
|
| 145 |
+
if st.download_button("📥 Download Data", edited_df.to_csv(index=False), "cosmos_db_data.csv", "text/csv"):
|
| 146 |
+
st.success("Data downloaded successfully!")
|
| 147 |
+
|
| 148 |
# Logout button
|
| 149 |
if st.button("🚪 Logout"):
|
| 150 |
st.session_state.logged_in = False
|
|
|
|
| 156 |
st.sidebar.text(f"Endpoint: {ENDPOINT}")
|
| 157 |
st.sidebar.text(f"Subscription ID: {SUBSCRIPTION_ID}")
|
| 158 |
st.sidebar.text(f"Database: {DATABASE_NAME}")
|
| 159 |
+
st.sidebar.text(f"Container: {CONTAINER_NAME}")
|
|
|
|
|
|
|
|
|
|
|
|