Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -165,6 +165,10 @@ def main():
|
|
| 165 |
st.session_state.selected_container = None
|
| 166 |
if 'selected_document_id' not in st.session_state:
|
| 167 |
st.session_state.selected_document_id = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
# 🔐 Automatic Login
|
| 170 |
if Key:
|
|
@@ -190,6 +194,7 @@ def main():
|
|
| 190 |
st.session_state.selected_database = selected_db
|
| 191 |
st.session_state.selected_container = None
|
| 192 |
st.session_state.selected_document_id = None
|
|
|
|
| 193 |
st.rerun()
|
| 194 |
|
| 195 |
if st.session_state.selected_database:
|
|
@@ -200,6 +205,7 @@ def main():
|
|
| 200 |
if selected_container != st.session_state.selected_container:
|
| 201 |
st.session_state.selected_container = selected_container
|
| 202 |
st.session_state.selected_document_id = None
|
|
|
|
| 203 |
st.rerun()
|
| 204 |
|
| 205 |
if st.session_state.selected_container:
|
|
@@ -213,10 +219,11 @@ def main():
|
|
| 213 |
else:
|
| 214 |
st.error(download_link)
|
| 215 |
|
| 216 |
-
# Fetch
|
| 217 |
-
documents = get_documents(container
|
|
|
|
| 218 |
|
| 219 |
-
if
|
| 220 |
documents_to_display = documents[:5]
|
| 221 |
st.info("Showing top 5 most recent documents.")
|
| 222 |
else:
|
|
@@ -228,8 +235,45 @@ def main():
|
|
| 228 |
view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
|
| 229 |
selected_view = st.selectbox("Select Viewer/Editor", view_options, index=2)
|
| 230 |
|
| 231 |
-
if selected_view == 'Show as
|
| 232 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
st.markdown("#### Edit the document fields below:")
|
| 234 |
|
| 235 |
# Create columns for each document
|
|
@@ -257,61 +301,56 @@ def main():
|
|
| 257 |
st.error(message)
|
| 258 |
except json.JSONDecodeError as e:
|
| 259 |
st.error(f"Invalid JSON: {str(e)} 🚫")
|
| 260 |
-
|
| 261 |
-
#
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
st.rerun()
|
| 311 |
-
else:
|
| 312 |
-
st.error(message)
|
| 313 |
-
except json.JSONDecodeError as e:
|
| 314 |
-
st.error(f"Invalid JSON: {str(e)} 🚫")
|
| 315 |
else:
|
| 316 |
st.sidebar.info("No documents found in this container. 📭")
|
| 317 |
|
|
@@ -382,6 +421,7 @@ def main():
|
|
| 382 |
st.session_state.selected_database = None
|
| 383 |
st.session_state.selected_container = None
|
| 384 |
st.session_state.selected_document_id = None
|
|
|
|
| 385 |
st.rerun()
|
| 386 |
|
| 387 |
if __name__ == "__main__":
|
|
|
|
| 165 |
st.session_state.selected_container = None
|
| 166 |
if 'selected_document_id' not in st.session_state:
|
| 167 |
st.session_state.selected_document_id = None
|
| 168 |
+
if 'current_index' not in st.session_state:
|
| 169 |
+
st.session_state.current_index = 0
|
| 170 |
+
if 'cloned_doc' not in st.session_state:
|
| 171 |
+
st.session_state.cloned_doc = None
|
| 172 |
|
| 173 |
# 🔐 Automatic Login
|
| 174 |
if Key:
|
|
|
|
| 194 |
st.session_state.selected_database = selected_db
|
| 195 |
st.session_state.selected_container = None
|
| 196 |
st.session_state.selected_document_id = None
|
| 197 |
+
st.session_state.current_index = 0
|
| 198 |
st.rerun()
|
| 199 |
|
| 200 |
if st.session_state.selected_database:
|
|
|
|
| 205 |
if selected_container != st.session_state.selected_container:
|
| 206 |
st.session_state.selected_container = selected_container
|
| 207 |
st.session_state.selected_document_id = None
|
| 208 |
+
st.session_state.current_index = 0
|
| 209 |
st.rerun()
|
| 210 |
|
| 211 |
if st.session_state.selected_container:
|
|
|
|
| 219 |
else:
|
| 220 |
st.error(download_link)
|
| 221 |
|
| 222 |
+
# Fetch documents
|
| 223 |
+
documents = get_documents(container)
|
| 224 |
+
total_docs = len(documents)
|
| 225 |
|
| 226 |
+
if total_docs > 5:
|
| 227 |
documents_to_display = documents[:5]
|
| 228 |
st.info("Showing top 5 most recent documents.")
|
| 229 |
else:
|
|
|
|
| 235 |
view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
|
| 236 |
selected_view = st.selectbox("Select Viewer/Editor", view_options, index=2)
|
| 237 |
|
| 238 |
+
if selected_view == 'Show as Markdown':
|
| 239 |
+
# 🖌️ Show each record as Markdown
|
| 240 |
+
for idx, doc in enumerate(documents_to_display):
|
| 241 |
+
st.markdown(f"### **🆔 ID: {doc.get('id', 'Unknown')}**")
|
| 242 |
+
content = doc.get('content', '')
|
| 243 |
+
if isinstance(content, dict) or isinstance(content, list):
|
| 244 |
+
content = json.dumps(content, indent=2)
|
| 245 |
+
st.markdown(content)
|
| 246 |
+
elif selected_view == 'Show as Code Editor':
|
| 247 |
+
# 💻 Show each record in a code editor with navigation
|
| 248 |
+
total_docs = len(documents)
|
| 249 |
+
doc = documents[st.session_state.current_index]
|
| 250 |
+
st.markdown(f"#### Document ID: {doc.get('id', '')}")
|
| 251 |
+
doc_str = st.text_area("Edit Document", value=json.dumps(doc, indent=2), height=300, key=f'code_editor_{st.session_state.current_index}')
|
| 252 |
+
col_prev, col_next = st.columns([1, 1])
|
| 253 |
+
with col_prev:
|
| 254 |
+
if st.button("⬅️ Previous"):
|
| 255 |
+
if st.session_state.current_index > 0:
|
| 256 |
+
st.session_state.current_index -= 1
|
| 257 |
+
st.rerun()
|
| 258 |
+
with col_next:
|
| 259 |
+
if st.button("➡️ Next"):
|
| 260 |
+
if st.session_state.current_index < total_docs - 1:
|
| 261 |
+
st.session_state.current_index += 1
|
| 262 |
+
st.rerun()
|
| 263 |
+
if st.button("💾 Save Changes", key=f'save_button_{st.session_state.current_index}'):
|
| 264 |
+
try:
|
| 265 |
+
updated_doc = json.loads(doc_str)
|
| 266 |
+
success, message = update_record(container, updated_doc)
|
| 267 |
+
if success:
|
| 268 |
+
st.success(f"Document {updated_doc['id']} saved successfully.")
|
| 269 |
+
st.session_state.selected_document_id = updated_doc['id']
|
| 270 |
+
st.rerun()
|
| 271 |
+
else:
|
| 272 |
+
st.error(message)
|
| 273 |
+
except json.JSONDecodeError as e:
|
| 274 |
+
st.error(f"Invalid JSON: {str(e)} 🚫")
|
| 275 |
+
elif selected_view == 'Show as Edit and Save':
|
| 276 |
+
# ✏️ Show as Edit and Save in columns
|
| 277 |
st.markdown("#### Edit the document fields below:")
|
| 278 |
|
| 279 |
# Create columns for each document
|
|
|
|
| 301 |
st.error(message)
|
| 302 |
except json.JSONDecodeError as e:
|
| 303 |
st.error(f"Invalid JSON: {str(e)} 🚫")
|
| 304 |
+
elif selected_view == 'Clone Document':
|
| 305 |
+
# 🧬 Clone Document per record
|
| 306 |
+
st.markdown("#### Clone a document:")
|
| 307 |
+
for idx, doc in enumerate(documents_to_display):
|
| 308 |
+
st.markdown(f"##### Document ID: {doc.get('id', '')}")
|
| 309 |
+
if st.button("📄 Clone Document", key=f'clone_button_{idx}'):
|
| 310 |
+
cloned_doc = doc.copy()
|
| 311 |
+
# Generate a unique ID
|
| 312 |
+
cloned_doc['id'] = generate_unique_id()
|
| 313 |
+
st.session_state.cloned_doc = cloned_doc
|
| 314 |
+
st.session_state.cloned_doc_str = json.dumps(cloned_doc, indent=2)
|
| 315 |
+
st.session_state.clone_mode = True
|
| 316 |
+
st.rerun()
|
| 317 |
+
if st.session_state.get('clone_mode', False):
|
| 318 |
+
st.markdown("#### Edit Cloned Document:")
|
| 319 |
+
cloned_doc_str = st.text_area("Cloned Document Content (in JSON format)", value=st.session_state.cloned_doc_str, height=300)
|
| 320 |
+
if st.button("💾 Save Cloned Document"):
|
| 321 |
+
try:
|
| 322 |
+
new_doc = json.loads(cloned_doc_str)
|
| 323 |
+
success, message = insert_record(container, new_doc)
|
| 324 |
+
if success:
|
| 325 |
+
st.success(f"Cloned document saved with id: {new_doc['id']} 🎉")
|
| 326 |
+
st.session_state.selected_document_id = new_doc['id']
|
| 327 |
+
st.session_state.clone_mode = False
|
| 328 |
+
st.session_state.cloned_doc = None
|
| 329 |
+
st.session_state.cloned_doc_str = ''
|
| 330 |
+
st.rerun()
|
| 331 |
+
else:
|
| 332 |
+
st.error(message)
|
| 333 |
+
except json.JSONDecodeError as e:
|
| 334 |
+
st.error(f"Invalid JSON: {str(e)} 🚫")
|
| 335 |
+
elif selected_view == 'New Record':
|
| 336 |
+
# 🆕 New Record
|
| 337 |
+
st.markdown("#### Create a new document:")
|
| 338 |
+
new_id = st.text_input("ID", value=generate_unique_id(), key='new_id')
|
| 339 |
+
new_doc_str = st.text_area("Document Content (in JSON format)", value='{}', height=300)
|
| 340 |
+
if st.button("➕ Create New Document"):
|
| 341 |
+
try:
|
| 342 |
+
new_doc = json.loads(new_doc_str)
|
| 343 |
+
new_doc['id'] = new_id # Use the provided ID
|
| 344 |
+
success, message = insert_record(container, new_doc)
|
| 345 |
+
if success:
|
| 346 |
+
st.success(f"New document created with id: {new_doc['id']} 🎉")
|
| 347 |
+
st.session_state.selected_document_id = new_doc['id']
|
| 348 |
+
# Switch to 'Show as Edit and Save' mode
|
| 349 |
+
st.rerun()
|
| 350 |
+
else:
|
| 351 |
+
st.error(message)
|
| 352 |
+
except json.JSONDecodeError as e:
|
| 353 |
+
st.error(f"Invalid JSON: {str(e)} 🚫")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
else:
|
| 355 |
st.sidebar.info("No documents found in this container. 📭")
|
| 356 |
|
|
|
|
| 421 |
st.session_state.selected_database = None
|
| 422 |
st.session_state.selected_container = None
|
| 423 |
st.session_state.selected_document_id = None
|
| 424 |
+
st.session_state.current_index = 0
|
| 425 |
st.rerun()
|
| 426 |
|
| 427 |
if __name__ == "__main__":
|