Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -134,32 +134,27 @@ def delete_record(name, id):
|
|
| 134 |
except Exception as e:
|
| 135 |
return False, f"An unexpected error occurred: {traceback.format_exc()}"
|
| 136 |
|
| 137 |
-
# New function to archive
|
| 138 |
-
def
|
| 139 |
try:
|
| 140 |
-
base_dir = "./
|
| 141 |
if os.path.exists(base_dir):
|
| 142 |
shutil.rmtree(base_dir)
|
| 143 |
os.makedirs(base_dir)
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
with open(os.path.join(container_dir, f"{container_name}.json"), 'w') as f:
|
| 160 |
-
json.dump(items, f, indent=2)
|
| 161 |
-
|
| 162 |
-
archive_name = f"cosmos_archive_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
| 163 |
shutil.make_archive(archive_name, 'zip', base_dir)
|
| 164 |
|
| 165 |
return get_base64_download_link(f"{archive_name}.zip", f"{archive_name}.zip")
|
|
@@ -226,6 +221,14 @@ def main():
|
|
| 226 |
if st.session_state.selected_container:
|
| 227 |
container = database.get_container_client(st.session_state.selected_container)
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
limit_to_1000 = st.sidebar.checkbox("🔢 Limit to top 1000 documents", value=True)
|
| 230 |
documents = get_documents(container, limit=1000 if limit_to_1000 else None)
|
| 231 |
|
|
@@ -237,7 +240,63 @@ def main():
|
|
| 237 |
st.subheader(f"📄 Document Details: {selected_document}")
|
| 238 |
selected_doc = next((doc for doc in documents if doc.get('id') == selected_document), None)
|
| 239 |
if selected_doc:
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
else:
|
| 242 |
st.sidebar.info("No documents found in this container.")
|
| 243 |
|
|
@@ -252,7 +311,7 @@ def main():
|
|
| 252 |
github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
|
| 253 |
source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
|
| 254 |
new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
|
| 255 |
-
|
| 256 |
col1, col2 = st.columns(2)
|
| 257 |
with col1:
|
| 258 |
if st.button("📥 Clone Repository"):
|
|
@@ -308,4 +367,3 @@ def main():
|
|
| 308 |
|
| 309 |
if __name__ == "__main__":
|
| 310 |
main()
|
| 311 |
-
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
return False, f"An unexpected error occurred: {traceback.format_exc()}"
|
| 136 |
|
| 137 |
+
# New function to archive current container
|
| 138 |
+
def archive_current_container(database_name, container_name, client):
|
| 139 |
try:
|
| 140 |
+
base_dir = "./cosmos_archive_current_container"
|
| 141 |
if os.path.exists(base_dir):
|
| 142 |
shutil.rmtree(base_dir)
|
| 143 |
os.makedirs(base_dir)
|
| 144 |
+
|
| 145 |
+
db_client = client.get_database_client(database_name)
|
| 146 |
+
container_client = db_client.get_container_client(container_name)
|
| 147 |
+
items = list(container_client.read_all_items())
|
| 148 |
+
|
| 149 |
+
container_dir = os.path.join(base_dir, container_name)
|
| 150 |
+
os.makedirs(container_dir)
|
| 151 |
+
|
| 152 |
+
for item in items:
|
| 153 |
+
item_id = item.get('id', f"unknown_{datetime.now().strftime('%Y%m%d%H%M%S')}")
|
| 154 |
+
with open(os.path.join(container_dir, f"{item_id}.json"), 'w') as f:
|
| 155 |
+
json.dump(item, f, indent=2)
|
| 156 |
+
|
| 157 |
+
archive_name = f"{container_name}_archive_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
shutil.make_archive(archive_name, 'zip', base_dir)
|
| 159 |
|
| 160 |
return get_base64_download_link(f"{archive_name}.zip", f"{archive_name}.zip")
|
|
|
|
| 221 |
if st.session_state.selected_container:
|
| 222 |
container = database.get_container_client(st.session_state.selected_container)
|
| 223 |
|
| 224 |
+
# Add Export button
|
| 225 |
+
if st.button("📦 Export Container Data"):
|
| 226 |
+
download_link = archive_current_container(st.session_state.selected_database, st.session_state.selected_container, st.session_state.client)
|
| 227 |
+
if download_link.startswith('<a'):
|
| 228 |
+
st.markdown(download_link, unsafe_allow_html=True)
|
| 229 |
+
else:
|
| 230 |
+
st.error(download_link)
|
| 231 |
+
|
| 232 |
limit_to_1000 = st.sidebar.checkbox("🔢 Limit to top 1000 documents", value=True)
|
| 233 |
documents = get_documents(container, limit=1000 if limit_to_1000 else None)
|
| 234 |
|
|
|
|
| 240 |
st.subheader(f"📄 Document Details: {selected_document}")
|
| 241 |
selected_doc = next((doc for doc in documents if doc.get('id') == selected_document), None)
|
| 242 |
if selected_doc:
|
| 243 |
+
# Add Viewer/Editor selection
|
| 244 |
+
view_options = ['Show as Markdown', 'Show as Code Editor', 'Show as Edit and Save', 'Clone Document', 'New Record']
|
| 245 |
+
selected_view = st.selectbox("Select Viewer/Editor", view_options)
|
| 246 |
+
|
| 247 |
+
if selected_view == 'Show as Markdown':
|
| 248 |
+
# Show as Markdown
|
| 249 |
+
items = get_documents(container)
|
| 250 |
+
for item in items:
|
| 251 |
+
st.markdown(f"### ID: {item.get('id', 'Unknown')}")
|
| 252 |
+
content = item.get('content', '')
|
| 253 |
+
if isinstance(content, dict) or isinstance(content, list):
|
| 254 |
+
content = json.dumps(content, indent=2)
|
| 255 |
+
st.markdown(content)
|
| 256 |
+
elif selected_view == 'Show as Code Editor':
|
| 257 |
+
# Show as Code Editor
|
| 258 |
+
items = get_documents(container)
|
| 259 |
+
for item in items:
|
| 260 |
+
st.code(json.dumps(item, indent=2), language='python')
|
| 261 |
+
elif selected_view == 'Show as Edit and Save':
|
| 262 |
+
# Show as Edit and Save
|
| 263 |
+
doc_str = st.text_area("Edit Document", value=json.dumps(selected_doc, indent=2), height=300)
|
| 264 |
+
if st.button("💾 Save"):
|
| 265 |
+
try:
|
| 266 |
+
updated_doc = json.loads(doc_str)
|
| 267 |
+
success, message = update_record(updated_doc)
|
| 268 |
+
if success:
|
| 269 |
+
st.success(message)
|
| 270 |
+
else:
|
| 271 |
+
st.error(message)
|
| 272 |
+
except json.JSONDecodeError as e:
|
| 273 |
+
st.error(f"Invalid JSON: {str(e)}")
|
| 274 |
+
elif selected_view == 'Clone Document':
|
| 275 |
+
# Clone Document
|
| 276 |
+
if st.button("📄 Clone Document"):
|
| 277 |
+
cloned_doc = selected_doc.copy()
|
| 278 |
+
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
|
| 279 |
+
cloned_doc['id'] = f"{cloned_doc['id']}_clone_{timestamp}"
|
| 280 |
+
success, message = insert_record(cloned_doc)
|
| 281 |
+
if success:
|
| 282 |
+
st.success(f"Document cloned with new id: {cloned_doc['id']}")
|
| 283 |
+
else:
|
| 284 |
+
st.error(message)
|
| 285 |
+
elif selected_view == 'New Record':
|
| 286 |
+
# New Record
|
| 287 |
+
new_doc_str = st.text_area("New Document", value='{}', height=300)
|
| 288 |
+
if st.button("➕ Create New Document"):
|
| 289 |
+
try:
|
| 290 |
+
new_doc = json.loads(new_doc_str)
|
| 291 |
+
if 'id' not in new_doc:
|
| 292 |
+
new_doc['id'] = f"new_doc_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
| 293 |
+
success, message = insert_record(new_doc)
|
| 294 |
+
if success:
|
| 295 |
+
st.success(f"New document created with id: {new_doc['id']}")
|
| 296 |
+
else:
|
| 297 |
+
st.error(message)
|
| 298 |
+
except json.JSONDecodeError as e:
|
| 299 |
+
st.error(f"Invalid JSON: {str(e)}")
|
| 300 |
else:
|
| 301 |
st.sidebar.info("No documents found in this container.")
|
| 302 |
|
|
|
|
| 311 |
github_token = os.environ.get("GITHUB") # Read GitHub token from environment variable
|
| 312 |
source_repo = st.text_input("Source GitHub Repository URL", value="https://github.com/AaronCWacker/AIExamples-8-24-Streamlit")
|
| 313 |
new_repo_name = st.text_input("New Repository Name (for cloning)", value=f"AIExample-Clone-{datetime.now().strftime('%Y%m%d_%H%M%S')}")
|
| 314 |
+
|
| 315 |
col1, col2 = st.columns(2)
|
| 316 |
with col1:
|
| 317 |
if st.button("📥 Clone Repository"):
|
|
|
|
| 367 |
|
| 368 |
if __name__ == "__main__":
|
| 369 |
main()
|
|
|