Siyuan Hu commited on
Commit
2d3811f
·
1 Parent(s): d96c704

feat(debug): add debug_compile function for PDF test + new tab preview

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -491,6 +491,53 @@ def _ensure_left_logo_or_disable(OUTPUT_DIR: Path, logs):
491
  logs.append(f"⚠️ Failed left_logo fallback: {e}")
492
  return False
493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
494
  # =====================
495
  # Gradio pipeline function (ISOLATED)
496
  # =====================
 
491
  logs.append(f"⚠️ Failed left_logo fallback: {e}")
492
  return False
493
 
494
+ def debug_compile():
495
+ """Generate a minimal LaTeX file, compile it to PDF, and return an HTML preview with an 'open in new tab' link."""
496
+ logs = [f"🐞 Debug start at {_now_str()}"]
497
+ # Prepare workspace
498
+ run_id, WORK_DIR, LOG_PATH, _ = _prepare_workspace(logs)
499
+ dbg_dir = WORK_DIR / "debug"
500
+ dbg_dir.mkdir(parents=True, exist_ok=True)
501
+ tex_path = dbg_dir / "test.tex"
502
+ tex_path.write_text(r"""
503
+ \documentclass{article}
504
+ \usepackage[T1]{fontenc}
505
+ \usepackage{lmodern}
506
+ \usepackage[margin=1in]{geometry}
507
+ \begin{document}
508
+ Hello, PDF!\\
509
+ This is a debug build generated at \today.
510
+ \end{document}
511
+ """.strip(), encoding="utf-8")
512
+
513
+ logs.append(f"Workspace: runs/{WORK_DIR.name}")
514
+ logs.append(f"Writing: debug/test.tex")
515
+
516
+ pdf_path = _compile_tex_to_pdf(tex_path, logs)
517
+ if not pdf_path or not pdf_path.exists():
518
+ logs.append("❌ Failed to compile debug PDF.")
519
+ _write_logs(LOG_PATH, logs)
520
+ return (
521
+ "<div style='color:#b00'><b>Debug PDF compile failed.</b></div>"
522
+ + f"<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px'>{'\n'.join(logs)}</pre>"
523
+ )
524
+
525
+ # Build preview + open-new-tab link
526
+ try:
527
+ b64 = base64.b64encode(pdf_path.read_bytes()).decode("utf-8")
528
+ open_tab = f"<a target='_blank' rel='noopener' href='data:application/pdf;base64,{b64}'>Open PDF in new tab</a>"
529
+ html = (
530
+ f"<div style='margin-bottom:8px'>{open_tab}</div>"
531
+ + _pdf_to_iframe_html(pdf_path, height="700px")
532
+ + f"<pre style='white-space:pre-wrap;background:#f7f7f8;padding:8px;border-radius:6px;margin-top:8px'>{'\n'.join(logs[-60:])}</pre>"
533
+ )
534
+ _write_logs(LOG_PATH, logs)
535
+ return html
536
+ except Exception as e:
537
+ logs.append(f"⚠️ Building preview failed: {e}")
538
+ _write_logs(LOG_PATH, logs)
539
+ return f"<div>Compiled but preview failed: {e}</div>"
540
+
541
  # =====================
542
  # Gradio pipeline function (ISOLATED)
543
  # =====================