Milhaud commited on
Commit
0c0232f
·
1 Parent(s): fc0164d

feat: real-time html preview

Browse files
Files changed (1) hide show
  1. app.py +37 -1
app.py CHANGED
@@ -297,6 +297,34 @@ def predict_decompose_group(svg_file, svg_text, object_name):
297
  )
298
 
299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
  def create_animation_preview(animation_desc: str, svg_content: str) -> tuple:
301
  """Create animation preview from description and SVG content."""
302
  if not svg_content.strip():
@@ -476,7 +504,8 @@ with demo:
476
  output_html = gr.Textbox(
477
  label="Output HTML",
478
  lines=10,
479
- placeholder="Generated HTML will appear here.",
 
480
  )
481
 
482
  # Event handlers
@@ -515,6 +544,13 @@ with demo:
515
  output_html, # Raw HTML output
516
  ],
517
  )
 
 
 
 
 
 
 
518
 
519
  if __name__ == "__main__":
520
  demo.launch(share=True)
 
297
  )
298
 
299
 
300
+ def update_preview_from_html(html_content: str) -> str:
301
+ """Update animation preview from manually edited HTML content."""
302
+ if not html_content.strip():
303
+ return create_error_html("⚠️ HTML content is empty")
304
+
305
+ try:
306
+ # Create iframe HTML - only escape quotes for srcdoc attribute
307
+ safe_html_content = html_content.replace('"', '"')
308
+ preview_html = f"""
309
+ <div style='padding: 20px; background: #fff; border: 1px solid #eee; border-radius: 8px; display: block;'>
310
+ <div style='display: block; align-items: center; margin-bottom: 10px;'>
311
+ <small style='color: #888;'>Preview (Live from HTML Output)</small>
312
+ </div>
313
+ <div id='animation-container' style='min-height: 300px; display: block; justify-content: center; align-items: center; background: #fafafa; border-radius: 4px; padding: 20px;'>
314
+ <iframe srcdoc="{safe_html_content}"
315
+ width="100%" height="600px"
316
+ style="border:none; overflow:hidden;"
317
+ sandbox="allow-scripts allow-same-origin">
318
+ </iframe>
319
+ </div>
320
+ </div>
321
+ """
322
+ return preview_html
323
+
324
+ except Exception as e:
325
+ return create_error_html(f"❌ Error updating preview: {str(e)}")
326
+
327
+
328
  def create_animation_preview(animation_desc: str, svg_content: str) -> tuple:
329
  """Create animation preview from description and SVG content."""
330
  if not svg_content.strip():
 
504
  output_html = gr.Textbox(
505
  label="Output HTML",
506
  lines=10,
507
+ placeholder="Generated HTML will appear here. You can edit this HTML and see live preview.",
508
+ interactive=True, # Make sure it's editable
509
  )
510
 
511
  # Event handlers
 
544
  output_html, # Raw HTML output
545
  ],
546
  )
547
+
548
+ # Real-time preview update when HTML is manually edited
549
+ output_html.change(
550
+ fn=update_preview_from_html,
551
+ inputs=[output_html],
552
+ outputs=[animation_preview],
553
+ )
554
 
555
  if __name__ == "__main__":
556
  demo.launch(share=True)