Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -221,8 +221,9 @@ def _convert_gr_history_to_api_messages(system_prompt, gr_history, current_user_
|
|
221 |
return messages
|
222 |
|
223 |
def _generate_ui_outputs_from_cache(owner, space_name):
|
224 |
-
# This function only reads the global variable, does not need 'global' declaration
|
225 |
-
|
|
|
226 |
preview_md_val = "*No files in cache to display.*"
|
227 |
formatted_md_val = f"# Space: {owner}/{space_name}\n## File Structure\n{bbb}\n📁 Root\n{bbb}\n\n*No files in cache.*" if owner or space_name else "*Load or define a Space to see its Markdown structure.*"
|
228 |
download_file = None
|
@@ -254,6 +255,8 @@ def _generate_ui_outputs_from_cache(owner, space_name):
|
|
254 |
return formatted_md_val, preview_md_val, gr.update(value=download_file, interactive=download_file is not None)
|
255 |
|
256 |
def handle_chat_submit(user_message, chat_history, api_key_input, provider_select, model_select, system_prompt, hf_owner_name, hf_repo_name, _current_formatted_markdown):
|
|
|
|
|
257 |
global parsed_code_blocks_state_cache
|
258 |
_chat_msg_in = ""
|
259 |
_chat_hist = list(chat_history)
|
@@ -264,8 +267,7 @@ def handle_chat_submit(user_message, chat_history, api_key_input, provider_selec
|
|
264 |
try:
|
265 |
parsed_from_md = build_logic_parse_markdown(_current_formatted_markdown)
|
266 |
new_cache_state = []
|
267 |
-
# This
|
268 |
-
# is correctly placed *before* this use in this function.
|
269 |
existing_structure_block = next((b for b in parsed_code_blocks_state_cache if b.get("is_structure_block")), None)
|
270 |
if existing_structure_block:
|
271 |
new_cache_state.append(existing_structure_block.copy())
|
@@ -294,9 +296,7 @@ def handle_chat_submit(user_message, chat_history, api_key_input, provider_selec
|
|
294 |
"is_structure_block": False
|
295 |
})
|
296 |
new_cache_state.sort(key=lambda b: (0, b["filename"]) if b.get("is_structure_block") else (1, b["filename"]))
|
297 |
-
# This
|
298 |
-
# The 'global' declaration needs to be before the first assignment *or* use.
|
299 |
-
# It is correctly placed at the start of the function.
|
300 |
parsed_code_blocks_state_cache = new_cache_state
|
301 |
|
302 |
except Exception as e:
|
@@ -312,7 +312,7 @@ def handle_chat_submit(user_message, chat_history, api_key_input, provider_selec
|
|
312 |
current_sys_prompt = system_prompt.strip() or DEFAULT_SYSTEM_PROMPT
|
313 |
|
314 |
current_files_context = ""
|
315 |
-
# This block
|
316 |
if parsed_code_blocks_state_cache:
|
317 |
current_files_context = "\n\n## Current Files in Space\n"
|
318 |
for block in parsed_code_blocks_state_cache:
|
@@ -355,15 +355,16 @@ def handle_chat_submit(user_message, chat_history, api_key_input, provider_selec
|
|
355 |
elif full_bot_response_content and not full_bot_response_content.startswith("Error: "):
|
356 |
_status = f"Streaming complete. Processing files from {model_select} response..."
|
357 |
|
358 |
-
# This
|
359 |
-
# after the global declaration at the top of the function.
|
360 |
parsing_res = _parse_chat_stream_logic(full_bot_response_content, existing_files_state=parsed_code_blocks_state_cache)
|
361 |
|
362 |
if parsing_res["error_message"]:
|
363 |
_status = f"Parsing Error: {parsing_res['error_message']}"
|
364 |
_detected_files_update = gr.Markdown(f"## Parsing Error\n`{escape_html_for_markdown(parsing_res['error_message'])}`")
|
365 |
else:
|
366 |
-
|
|
|
|
|
367 |
parsed_code_blocks_state_cache = parsing_res["parsed_code_blocks"]
|
368 |
|
369 |
_formatted_output_update, _detected_files_update, _download_btn_update = _generate_ui_outputs_from_cache(hf_owner_name, hf_repo_name)
|
@@ -399,6 +400,7 @@ def update_models_dropdown(provider_select):
|
|
399 |
return gr.update(choices=models, value=selected_value)
|
400 |
|
401 |
def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
|
|
402 |
global parsed_code_blocks_state_cache
|
403 |
_formatted_md_val, _detected_preview_val, _status_val = "*Loading files...*", "*Loading files...*", f"Loading Space: {ui_owner_name}/{ui_space_name}..."
|
404 |
_file_browser_update, _iframe_html_update, _download_btn_update = gr.update(visible=False, choices=[], value=None), gr.update(value=None, visible=False), gr.update(interactive=False, value=None)
|
@@ -429,6 +431,7 @@ def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
|
429 |
|
430 |
if error_occurred:
|
431 |
yield (f"*Error: {_status_val}*", f"*Error: {_status_val}*", _status_val, _file_browser_update, updated_owner_name_val, ui_space_name, _iframe_html_update, _download_btn_update, _build_status_clear, _edit_status_clear, _runtime_status_clear, _chat_history_clear)
|
|
|
432 |
parsed_code_blocks_state_cache = []
|
433 |
return
|
434 |
|
@@ -441,6 +444,7 @@ def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
|
441 |
|
442 |
if err_list_files and not file_list:
|
443 |
_status_val = f"File List Error: {err_list_files}"
|
|
|
444 |
parsed_code_blocks_state_cache = []
|
445 |
_formatted_md_val, _detected_preview_val, _download_btn_update = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name)
|
446 |
_file_browser_update = gr.update(visible=True, choices=[], value="Error loading files")
|
@@ -449,6 +453,7 @@ def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
|
449 |
|
450 |
if not file_list:
|
451 |
_status_val = f"Loaded Space: {owner_to_use}/{ui_space_name}. No files found ({err_list_files or 'Repository is empty'})."
|
|
|
452 |
parsed_code_blocks_state_cache = []
|
453 |
_formatted_md_val, _detected_preview_val, _download_btn_update = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name)
|
454 |
_file_browser_update = gr.update(visible=True, choices=[], value="No files found")
|
@@ -483,6 +488,7 @@ def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
|
483 |
print(f"Unexpected error loading {file_path}: {content_ex}")
|
484 |
continue
|
485 |
|
|
|
486 |
parsed_code_blocks_state_cache = loaded_files_for_cache
|
487 |
_formatted_md_val, _detected_preview_val, _download_btn_update = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name)
|
488 |
_status_val = f"Successfully loaded Space: {owner_to_use}/{ui_space_name}. Markdown ready. {len(file_list)} files listed."
|
@@ -491,6 +497,7 @@ def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
|
491 |
yield (_formatted_md_val, _detected_preview_val, _status_val, _file_browser_update, updated_owner_name_val, ui_space_name, _iframe_html_update, _download_btn_update, _build_status_clear, _edit_status_clear, _runtime_status_clear, _chat_history_clear)
|
492 |
|
493 |
def handle_build_space_button(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, space_sdk_ui, formatted_markdown_content):
|
|
|
494 |
global parsed_code_blocks_state_cache
|
495 |
_build_status, _iframe_html, _file_browser_update = "Starting space build process...", gr.update(value=None, visible=False), gr.update(visible=False, choices=[], value=None)
|
496 |
yield _build_status, _iframe_html, _file_browser_update, gr.update(value=ui_owner_name_part), gr.update(value=ui_space_name_part)
|
@@ -511,7 +518,8 @@ def handle_build_space_button(hf_api_key_ui, ui_space_name_part, ui_owner_name_p
|
|
511 |
|
512 |
try:
|
513 |
parsed_from_md_for_build = build_logic_parse_markdown(formatted_markdown_content)
|
514 |
-
|
|
|
515 |
if parsed_from_md_for_build.get("owner_md"):
|
516 |
ui_owner_name_part = parsed_from_md_for_build["owner_md"]
|
517 |
if parsed_from_md_for_build.get("repo_name_md"):
|
@@ -519,7 +527,8 @@ def handle_build_space_button(hf_api_key_ui, ui_space_name_part, ui_owner_name_p
|
|
519 |
|
520 |
structure_block_md = next((f for f in parsed_from_md_for_build.get("files", []) if f.get("path") == "File Structure (original)"), None)
|
521 |
if structure_block_md:
|
522 |
-
|
|
|
523 |
"filename": structure_block_md["path"],
|
524 |
"code": structure_block_md["content"],
|
525 |
"language": "plaintext",
|
@@ -530,14 +539,16 @@ def handle_build_space_button(hf_api_key_ui, ui_space_name_part, ui_owner_name_p
|
|
530 |
for f_info in parsed_from_md_for_build.get("files", []):
|
531 |
if f_info.get("path") and f_info["path"] != "File Structure (original)":
|
532 |
is_binary_repr = isinstance(f_info.get("content"), str) and (f_info["content"].startswith("[Binary file") or f_info["content"].startswith("[Error loading content:") or f_info["content"].startswith("[Binary or Skipped file]"))
|
533 |
-
|
|
|
534 |
"filename": f_info["path"],
|
535 |
"code": f_info.get("content", ""),
|
536 |
"language": "binary" if is_binary_repr else _infer_lang_from_filename(f_info["path"]),
|
537 |
"is_binary": is_binary_repr,
|
538 |
"is_structure_block": False
|
539 |
})
|
540 |
-
|
|
|
541 |
|
542 |
except Exception as e:
|
543 |
_build_status = f"Build Error: Failed to parse Markdown structure before building: {e}";
|
@@ -563,7 +574,9 @@ def handle_build_space_button(hf_api_key_ui, ui_space_name_part, ui_owner_name_p
|
|
563 |
yield _build_status, _iframe_html, _file_browser_update, owner_name_output, space_name_output
|
564 |
|
565 |
def handle_load_file_for_editing(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, selected_file_path):
|
566 |
-
|
|
|
|
|
567 |
_file_content_val, _edit_status_val, _commit_msg_val, _lang_update = "", "Error: No file selected.", gr.update(value=""), gr.update(language="python")
|
568 |
if not selected_file_path or selected_file_path in ["No files found", "Error loading files", "Error refreshing files"]:
|
569 |
yield _file_content_val, "Select a file from the dropdown.", _commit_msg_val, _lang_update
|
@@ -603,6 +616,7 @@ def handle_load_file_for_editing(hf_api_key_ui, ui_space_name_part, ui_owner_nam
|
|
603 |
yield _file_content_val, _edit_status_val, _commit_msg_val, _lang_update
|
604 |
|
605 |
def handle_commit_file_changes(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, file_to_edit_path, edited_content, commit_message):
|
|
|
606 |
global parsed_code_blocks_state_cache
|
607 |
_edit_status_val = "Processing commit..."
|
608 |
_file_browser_update_val = gr.update()
|
@@ -634,8 +648,7 @@ def handle_commit_file_changes(hf_api_key_ui, ui_space_name_part, ui_owner_name_
|
|
634 |
|
635 |
if "Successfully updated" in status_msg:
|
636 |
found_in_cache = False
|
637 |
-
#
|
638 |
-
# But if the file was new, it might be added later, which requires global.
|
639 |
for block in parsed_code_blocks_state_cache:
|
640 |
if block["filename"] == file_to_edit_path:
|
641 |
block["code"] = edited_content
|
@@ -645,8 +658,9 @@ def handle_commit_file_changes(hf_api_key_ui, ui_space_name_part, ui_owner_name_
|
|
645 |
found_in_cache = True
|
646 |
break
|
647 |
if not found_in_cache:
|
648 |
-
#
|
649 |
parsed_code_blocks_state_cache = [b for b in parsed_code_blocks_state_cache if b["filename"] != file_to_edit_path]
|
|
|
650 |
parsed_code_blocks_state_cache.append({
|
651 |
"filename": file_to_edit_path,
|
652 |
"code": edited_content,
|
@@ -654,6 +668,7 @@ def handle_commit_file_changes(hf_api_key_ui, ui_space_name_part, ui_owner_name_
|
|
654 |
"is_binary": False,
|
655 |
"is_structure_block": False
|
656 |
})
|
|
|
657 |
parsed_code_blocks_state_cache.sort(key=lambda b: (0, b["filename"]) if b.get("is_structure_block") else (1, b["filename"]))
|
658 |
|
659 |
_formatted_md_out, _detected_preview_out, _download_btn_out = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name_part)
|
@@ -668,6 +683,7 @@ def handle_commit_file_changes(hf_api_key_ui, ui_space_name_part, ui_owner_name_
|
|
668 |
yield _edit_status_val, _file_browser_update_val, _formatted_md_out, _detected_preview_out, _download_btn_out
|
669 |
|
670 |
def handle_delete_file(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, file_to_delete_path):
|
|
|
671 |
global parsed_code_blocks_state_cache
|
672 |
_edit_status_val = "Processing deletion..."
|
673 |
_file_browser_choices_update = gr.update()
|
@@ -704,7 +720,7 @@ def handle_delete_file(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, fi
|
|
704 |
new_file_list, err_list = list_space_files_for_browsing(hf_api_key_ui, ui_space_name_part, owner_to_use)
|
705 |
|
706 |
if "Successfully deleted" in deletion_status_msg:
|
707 |
-
#
|
708 |
parsed_code_blocks_state_cache = [b for b in parsed_code_blocks_state_cache if b["filename"] != file_to_delete_path]
|
709 |
_formatted_md_out, _detected_preview_out, _download_btn_out = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name_part)
|
710 |
|
|
|
221 |
return messages
|
222 |
|
223 |
def _generate_ui_outputs_from_cache(owner, space_name):
|
224 |
+
# This function only reads the global variable, does not need 'global' declaration to read.
|
225 |
+
# Added global for consistency, but technically reads don't require it.
|
226 |
+
global parsed_code_blocks_state_cache
|
227 |
preview_md_val = "*No files in cache to display.*"
|
228 |
formatted_md_val = f"# Space: {owner}/{space_name}\n## File Structure\n{bbb}\n📁 Root\n{bbb}\n\n*No files in cache.*" if owner or space_name else "*Load or define a Space to see its Markdown structure.*"
|
229 |
download_file = None
|
|
|
255 |
return formatted_md_val, preview_md_val, gr.update(value=download_file, interactive=download_file is not None)
|
256 |
|
257 |
def handle_chat_submit(user_message, chat_history, api_key_input, provider_select, model_select, system_prompt, hf_owner_name, hf_repo_name, _current_formatted_markdown):
|
258 |
+
# This global declaration is needed because the function ASSIGNS to parsed_code_blocks_state_cache later.
|
259 |
+
# It must be at the very top of the function before any use or assignment.
|
260 |
global parsed_code_blocks_state_cache
|
261 |
_chat_msg_in = ""
|
262 |
_chat_hist = list(chat_history)
|
|
|
267 |
try:
|
268 |
parsed_from_md = build_logic_parse_markdown(_current_formatted_markdown)
|
269 |
new_cache_state = []
|
270 |
+
# This line USES parsed_code_blocks_state_cache. It must come AFTER the global declaration.
|
|
|
271 |
existing_structure_block = next((b for b in parsed_code_blocks_state_cache if b.get("is_structure_block")), None)
|
272 |
if existing_structure_block:
|
273 |
new_cache_state.append(existing_structure_block.copy())
|
|
|
296 |
"is_structure_block": False
|
297 |
})
|
298 |
new_cache_state.sort(key=lambda b: (0, b["filename"]) if b.get("is_structure_block") else (1, b["filename"]))
|
299 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache. This is why the global declaration is needed.
|
|
|
|
|
300 |
parsed_code_blocks_state_cache = new_cache_state
|
301 |
|
302 |
except Exception as e:
|
|
|
312 |
current_sys_prompt = system_prompt.strip() or DEFAULT_SYSTEM_PROMPT
|
313 |
|
314 |
current_files_context = ""
|
315 |
+
# This block USES parsed_code_blocks_state_cache. It must come AFTER the global declaration.
|
316 |
if parsed_code_blocks_state_cache:
|
317 |
current_files_context = "\n\n## Current Files in Space\n"
|
318 |
for block in parsed_code_blocks_state_cache:
|
|
|
355 |
elif full_bot_response_content and not full_bot_response_content.startswith("Error: "):
|
356 |
_status = f"Streaming complete. Processing files from {model_select} response..."
|
357 |
|
358 |
+
# This line USES parsed_code_blocks_state_cache.
|
|
|
359 |
parsing_res = _parse_chat_stream_logic(full_bot_response_content, existing_files_state=parsed_code_blocks_state_cache)
|
360 |
|
361 |
if parsing_res["error_message"]:
|
362 |
_status = f"Parsing Error: {parsing_res['error_message']}"
|
363 |
_detected_files_update = gr.Markdown(f"## Parsing Error\n`{escape_html_for_markdown(parsing_res['error_message'])}`")
|
364 |
else:
|
365 |
+
# REMOVE THE REDUNDANT GLOBAL DECLARATION HERE.
|
366 |
+
# global parsed_code_blocks_state_cache # Redundant global, but harmless. The one at the top is effective.
|
367 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
368 |
parsed_code_blocks_state_cache = parsing_res["parsed_code_blocks"]
|
369 |
|
370 |
_formatted_output_update, _detected_files_update, _download_btn_update = _generate_ui_outputs_from_cache(hf_owner_name, hf_repo_name)
|
|
|
400 |
return gr.update(choices=models, value=selected_value)
|
401 |
|
402 |
def handle_load_existing_space(hf_api_key_ui, ui_owner_name, ui_space_name):
|
403 |
+
# This global declaration is needed because the function ASSIGNS to parsed_code_blocks_state_cache later.
|
404 |
global parsed_code_blocks_state_cache
|
405 |
_formatted_md_val, _detected_preview_val, _status_val = "*Loading files...*", "*Loading files...*", f"Loading Space: {ui_owner_name}/{ui_space_name}..."
|
406 |
_file_browser_update, _iframe_html_update, _download_btn_update = gr.update(visible=False, choices=[], value=None), gr.update(value=None, visible=False), gr.update(interactive=False, value=None)
|
|
|
431 |
|
432 |
if error_occurred:
|
433 |
yield (f"*Error: {_status_val}*", f"*Error: {_status_val}*", _status_val, _file_browser_update, updated_owner_name_val, ui_space_name, _iframe_html_update, _download_btn_update, _build_status_clear, _edit_status_clear, _runtime_status_clear, _chat_history_clear)
|
434 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
435 |
parsed_code_blocks_state_cache = []
|
436 |
return
|
437 |
|
|
|
444 |
|
445 |
if err_list_files and not file_list:
|
446 |
_status_val = f"File List Error: {err_list_files}"
|
447 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
448 |
parsed_code_blocks_state_cache = []
|
449 |
_formatted_md_val, _detected_preview_val, _download_btn_update = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name)
|
450 |
_file_browser_update = gr.update(visible=True, choices=[], value="Error loading files")
|
|
|
453 |
|
454 |
if not file_list:
|
455 |
_status_val = f"Loaded Space: {owner_to_use}/{ui_space_name}. No files found ({err_list_files or 'Repository is empty'})."
|
456 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
457 |
parsed_code_blocks_state_cache = []
|
458 |
_formatted_md_val, _detected_preview_val, _download_btn_update = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name)
|
459 |
_file_browser_update = gr.update(visible=True, choices=[], value="No files found")
|
|
|
488 |
print(f"Unexpected error loading {file_path}: {content_ex}")
|
489 |
continue
|
490 |
|
491 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
492 |
parsed_code_blocks_state_cache = loaded_files_for_cache
|
493 |
_formatted_md_val, _detected_preview_val, _download_btn_update = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name)
|
494 |
_status_val = f"Successfully loaded Space: {owner_to_use}/{ui_space_name}. Markdown ready. {len(file_list)} files listed."
|
|
|
497 |
yield (_formatted_md_val, _detected_preview_val, _status_val, _file_browser_update, updated_owner_name_val, ui_space_name, _iframe_html_update, _download_btn_update, _build_status_clear, _edit_status_clear, _runtime_status_clear, _chat_history_clear)
|
498 |
|
499 |
def handle_build_space_button(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, space_sdk_ui, formatted_markdown_content):
|
500 |
+
# This global declaration is needed because the function ASSIGNS to parsed_code_blocks_state_cache later.
|
501 |
global parsed_code_blocks_state_cache
|
502 |
_build_status, _iframe_html, _file_browser_update = "Starting space build process...", gr.update(value=None, visible=False), gr.update(visible=False, choices=[], value=None)
|
503 |
yield _build_status, _iframe_html, _file_browser_update, gr.update(value=ui_owner_name_part), gr.update(value=ui_space_name_part)
|
|
|
518 |
|
519 |
try:
|
520 |
parsed_from_md_for_build = build_logic_parse_markdown(formatted_markdown_content)
|
521 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
522 |
+
parsed_code_blocks_state_cache = []
|
523 |
if parsed_from_md_for_build.get("owner_md"):
|
524 |
ui_owner_name_part = parsed_from_md_for_build["owner_md"]
|
525 |
if parsed_from_md_for_build.get("repo_name_md"):
|
|
|
527 |
|
528 |
structure_block_md = next((f for f in parsed_from_md_for_build.get("files", []) if f.get("path") == "File Structure (original)"), None)
|
529 |
if structure_block_md:
|
530 |
+
# This line modifies the global list.
|
531 |
+
parsed_code_blocks_state_cache.append({
|
532 |
"filename": structure_block_md["path"],
|
533 |
"code": structure_block_md["content"],
|
534 |
"language": "plaintext",
|
|
|
539 |
for f_info in parsed_from_md_for_build.get("files", []):
|
540 |
if f_info.get("path") and f_info["path"] != "File Structure (original)":
|
541 |
is_binary_repr = isinstance(f_info.get("content"), str) and (f_info["content"].startswith("[Binary file") or f_info["content"].startswith("[Error loading content:") or f_info["content"].startswith("[Binary or Skipped file]"))
|
542 |
+
# This line modifies the global list.
|
543 |
+
parsed_code_blocks_state_cache.append({
|
544 |
"filename": f_info["path"],
|
545 |
"code": f_info.get("content", ""),
|
546 |
"language": "binary" if is_binary_repr else _infer_lang_from_filename(f_info["path"]),
|
547 |
"is_binary": is_binary_repr,
|
548 |
"is_structure_block": False
|
549 |
})
|
550 |
+
# This line modifies the global list.
|
551 |
+
parsed_code_blocks_state_cache.sort(key=lambda b: (0, b["filename"]) if b.get("is_structure_block") else (1, b["filename"]))
|
552 |
|
553 |
except Exception as e:
|
554 |
_build_status = f"Build Error: Failed to parse Markdown structure before building: {e}";
|
|
|
574 |
yield _build_status, _iframe_html, _file_browser_update, owner_name_output, space_name_output
|
575 |
|
576 |
def handle_load_file_for_editing(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, selected_file_path):
|
577 |
+
# This function only reads the global variable, does not strictly need global.
|
578 |
+
# Added global for consistency, but technically reads don't require it.
|
579 |
+
global parsed_code_blocks_state_cache
|
580 |
_file_content_val, _edit_status_val, _commit_msg_val, _lang_update = "", "Error: No file selected.", gr.update(value=""), gr.update(language="python")
|
581 |
if not selected_file_path or selected_file_path in ["No files found", "Error loading files", "Error refreshing files"]:
|
582 |
yield _file_content_val, "Select a file from the dropdown.", _commit_msg_val, _lang_update
|
|
|
616 |
yield _file_content_val, _edit_status_val, _commit_msg_val, _lang_update
|
617 |
|
618 |
def handle_commit_file_changes(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, file_to_edit_path, edited_content, commit_message):
|
619 |
+
# This global declaration is needed because the function ASSIGNS to parsed_code_blocks_state_cache later.
|
620 |
global parsed_code_blocks_state_cache
|
621 |
_edit_status_val = "Processing commit..."
|
622 |
_file_browser_update_val = gr.update()
|
|
|
648 |
|
649 |
if "Successfully updated" in status_msg:
|
650 |
found_in_cache = False
|
651 |
+
# This loop modifies items within the global list.
|
|
|
652 |
for block in parsed_code_blocks_state_cache:
|
653 |
if block["filename"] == file_to_edit_path:
|
654 |
block["code"] = edited_content
|
|
|
658 |
found_in_cache = True
|
659 |
break
|
660 |
if not found_in_cache:
|
661 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
662 |
parsed_code_blocks_state_cache = [b for b in parsed_code_blocks_state_cache if b["filename"] != file_to_edit_path]
|
663 |
+
# This line modifies the global list.
|
664 |
parsed_code_blocks_state_cache.append({
|
665 |
"filename": file_to_edit_path,
|
666 |
"code": edited_content,
|
|
|
668 |
"is_binary": False,
|
669 |
"is_structure_block": False
|
670 |
})
|
671 |
+
# This line modifies the global list.
|
672 |
parsed_code_blocks_state_cache.sort(key=lambda b: (0, b["filename"]) if b.get("is_structure_block") else (1, b["filename"]))
|
673 |
|
674 |
_formatted_md_out, _detected_preview_out, _download_btn_out = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name_part)
|
|
|
683 |
yield _edit_status_val, _file_browser_update_val, _formatted_md_out, _detected_preview_out, _download_btn_out
|
684 |
|
685 |
def handle_delete_file(hf_api_key_ui, ui_space_name_part, ui_owner_name_part, file_to_delete_path):
|
686 |
+
# This global declaration is needed because the function ASSIGNS to parsed_code_blocks_state_cache later.
|
687 |
global parsed_code_blocks_state_cache
|
688 |
_edit_status_val = "Processing deletion..."
|
689 |
_file_browser_choices_update = gr.update()
|
|
|
720 |
new_file_list, err_list = list_space_files_for_browsing(hf_api_key_ui, ui_space_name_part, owner_to_use)
|
721 |
|
722 |
if "Successfully deleted" in deletion_status_msg:
|
723 |
+
# This line ASSIGNS to parsed_code_blocks_state_cache.
|
724 |
parsed_code_blocks_state_cache = [b for b in parsed_code_blocks_state_cache if b["filename"] != file_to_delete_path]
|
725 |
_formatted_md_out, _detected_preview_out, _download_btn_out = _generate_ui_outputs_from_cache(owner_to_use, ui_space_name_part)
|
726 |
|