Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -192,29 +192,29 @@ def delete_record(container, record, partition_key_field=None):
|
|
| 192 |
|
| 193 |
# Determine partition key value
|
| 194 |
if partition_key_field:
|
| 195 |
-
# Use specified partition key field from the record
|
| 196 |
partition_key_value = record.get(partition_key_field)
|
| 197 |
if partition_key_value is None:
|
| 198 |
return False, f"Partition key field '{partition_key_field}' not found in record {doc_id}. ๐"
|
| 199 |
else:
|
| 200 |
-
#
|
| 201 |
-
# (common when partition key is same as ID)
|
| 202 |
partition_key_value = doc_id
|
| 203 |
|
|
|
|
|
|
|
|
|
|
| 204 |
# Perform the deletion
|
| 205 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 206 |
-
return True, f"Record {doc_id} successfully deleted. ๐๏ธ"
|
| 207 |
|
| 208 |
except exceptions.CosmosResourceNotFoundError:
|
| 209 |
-
# Document doesn't exist,
|
| 210 |
-
return True, f"Record {doc_id} not found (already deleted
|
| 211 |
except exceptions.CosmosHttpResponseError as e:
|
| 212 |
-
#
|
| 213 |
return False, f"HTTP error deleting {doc_id}: {str(e)}. ๐จ"
|
| 214 |
except Exception as e:
|
| 215 |
# Unexpected errors with full traceback
|
| 216 |
return False, f"Unexpected error deleting {doc_id}: {str(traceback.format_exc())}. ๐ฑ"
|
| 217 |
-
|
| 218 |
|
| 219 |
|
| 220 |
|
|
@@ -637,13 +637,14 @@ def main():
|
|
| 637 |
if st.button("๐๏ธ Delete", key=f'delete_{st.session_state.current_index}'):
|
| 638 |
try:
|
| 639 |
current_doc = json.loads(doc_str)
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
|
|
|
| 647 |
except Exception as e:
|
| 648 |
st.error(f"Delete err: {str(e)}")
|
| 649 |
elif selected_view == 'Run AI':
|
|
|
|
| 192 |
|
| 193 |
# Determine partition key value
|
| 194 |
if partition_key_field:
|
|
|
|
| 195 |
partition_key_value = record.get(partition_key_field)
|
| 196 |
if partition_key_value is None:
|
| 197 |
return False, f"Partition key field '{partition_key_field}' not found in record {doc_id}. ๐"
|
| 198 |
else:
|
| 199 |
+
# Default to ID if no partition key field is specified
|
|
|
|
| 200 |
partition_key_value = doc_id
|
| 201 |
|
| 202 |
+
# Debug info
|
| 203 |
+
st.write(f"Attempting to delete: ID={doc_id}, Partition Key={partition_key_value}")
|
| 204 |
+
|
| 205 |
# Perform the deletion
|
| 206 |
container.delete_item(item=doc_id, partition_key=partition_key_value)
|
| 207 |
+
return True, f"Record {doc_id} successfully deleted from Cosmos DB. ๐๏ธ"
|
| 208 |
|
| 209 |
except exceptions.CosmosResourceNotFoundError:
|
| 210 |
+
# Document doesn't exist, which is fine for a delete operation
|
| 211 |
+
return True, f"Record {doc_id} not found in Cosmos DB (already deleted or never existed). ๐๏ธ"
|
| 212 |
except exceptions.CosmosHttpResponseError as e:
|
| 213 |
+
# Other HTTP errors (e.g., wrong partition key, permissions)
|
| 214 |
return False, f"HTTP error deleting {doc_id}: {str(e)}. ๐จ"
|
| 215 |
except Exception as e:
|
| 216 |
# Unexpected errors with full traceback
|
| 217 |
return False, f"Unexpected error deleting {doc_id}: {str(traceback.format_exc())}. ๐ฑ"
|
|
|
|
| 218 |
|
| 219 |
|
| 220 |
|
|
|
|
| 637 |
if st.button("๐๏ธ Delete", key=f'delete_{st.session_state.current_index}'):
|
| 638 |
try:
|
| 639 |
current_doc = json.loads(doc_str)
|
| 640 |
+
# Adjust this to your actual partition key field (e.g., "testPART", "category", or None if ID is the partition key)
|
| 641 |
+
partition_key_field = "testPART" # Confirm this matches your container's partition key
|
| 642 |
+
success, message = delete_record(container, current_doc, partition_key_field)
|
| 643 |
+
if success:
|
| 644 |
+
st.success(message) # Display as success even if not found
|
| 645 |
+
st.rerun()
|
| 646 |
+
else:
|
| 647 |
+
st.error(message)
|
| 648 |
except Exception as e:
|
| 649 |
st.error(f"Delete err: {str(e)}")
|
| 650 |
elif selected_view == 'Run AI':
|