Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -196,18 +196,34 @@ def archive_all_data(client):
|
|
| 196 |
except Exception as e:
|
| 197 |
return f"An error occurred while archiving data: {str(e)}"
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
# Cosmos DB operations
|
| 200 |
def create_item(client, db_name, container_name):
|
| 201 |
st.subheader("Create New Item")
|
| 202 |
item_id = st.text_input("Item ID")
|
| 203 |
-
item_data = st.text_area("Item Data (
|
|
|
|
| 204 |
|
| 205 |
if st.button("💾 Save New Item"):
|
| 206 |
try:
|
| 207 |
container = client.get_database_client(db_name).get_container_client(container_name)
|
| 208 |
-
|
|
|
|
| 209 |
container.create_item(body=new_item)
|
| 210 |
st.success("New item created successfully!")
|
|
|
|
| 211 |
st.rerun()
|
| 212 |
except Exception as e:
|
| 213 |
st.error(f"Error creating item: {str(e)}")
|
|
@@ -220,12 +236,19 @@ def edit_item(client, db_name, container_name, item_id):
|
|
| 220 |
edited_item = {}
|
| 221 |
for key, value in item.items():
|
| 222 |
if key not in ['_rid', '_self', '_etag', '_attachments', '_ts']:
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
if st.button("💾 Save Changes"):
|
| 226 |
try:
|
| 227 |
container.upsert_item(body=edited_item)
|
| 228 |
st.success("Item updated successfully!")
|
|
|
|
| 229 |
st.rerun()
|
| 230 |
except Exception as e:
|
| 231 |
st.error(f"Error updating item: {str(e)}")
|
|
|
|
| 196 |
except Exception as e:
|
| 197 |
return f"An error occurred while archiving data: {str(e)}"
|
| 198 |
|
| 199 |
+
# New function to convert text to JSON
|
| 200 |
+
def text_to_json(text):
|
| 201 |
+
lines = text.split('\n')
|
| 202 |
+
json_data = {}
|
| 203 |
+
for line in lines:
|
| 204 |
+
if ':' in line:
|
| 205 |
+
key, value = line.split(':', 1)
|
| 206 |
+
json_data[key.strip()] = value.strip()
|
| 207 |
+
else:
|
| 208 |
+
# If there's no colon, use the whole line as a key with an empty string as value
|
| 209 |
+
json_data[line.strip()] = ""
|
| 210 |
+
return json_data
|
| 211 |
+
|
| 212 |
# Cosmos DB operations
|
| 213 |
def create_item(client, db_name, container_name):
|
| 214 |
st.subheader("Create New Item")
|
| 215 |
item_id = st.text_input("Item ID")
|
| 216 |
+
item_data = st.text_area("Item Data (Text format)",
|
| 217 |
+
value="🌟 Cosmos DB and GitHub Integration\nCreate New Item\nItem ID\nItem Data (Text format)\n💾 Save New Item")
|
| 218 |
|
| 219 |
if st.button("💾 Save New Item"):
|
| 220 |
try:
|
| 221 |
container = client.get_database_client(db_name).get_container_client(container_name)
|
| 222 |
+
json_data = text_to_json(item_data)
|
| 223 |
+
new_item = {"id": item_id, "content": json_data}
|
| 224 |
container.create_item(body=new_item)
|
| 225 |
st.success("New item created successfully!")
|
| 226 |
+
st.json(new_item) # Display the created item in JSON format
|
| 227 |
st.rerun()
|
| 228 |
except Exception as e:
|
| 229 |
st.error(f"Error creating item: {str(e)}")
|
|
|
|
| 236 |
edited_item = {}
|
| 237 |
for key, value in item.items():
|
| 238 |
if key not in ['_rid', '_self', '_etag', '_attachments', '_ts']:
|
| 239 |
+
if key == 'content':
|
| 240 |
+
# Convert the JSON content back to text for editing
|
| 241 |
+
text_content = "\n".join([f"{k}: {v}" for k, v in value.items()])
|
| 242 |
+
edited_text = st.text_area(key, text_content)
|
| 243 |
+
edited_item[key] = text_to_json(edited_text)
|
| 244 |
+
else:
|
| 245 |
+
edited_item[key] = st.text_input(key, value)
|
| 246 |
|
| 247 |
if st.button("💾 Save Changes"):
|
| 248 |
try:
|
| 249 |
container.upsert_item(body=edited_item)
|
| 250 |
st.success("Item updated successfully!")
|
| 251 |
+
st.json(edited_item) # Display the updated item in JSON format
|
| 252 |
st.rerun()
|
| 253 |
except Exception as e:
|
| 254 |
st.error(f"Error updating item: {str(e)}")
|