akhaliq HF Staff commited on
Commit
1397ad0
·
1 Parent(s): 1e99d2a

live preview first and split files

Browse files
Files changed (1) hide show
  1. app.py +226 -3
app.py CHANGED
@@ -6335,6 +6335,8 @@ with gr.Blocks(
6335
 
6336
  with gr.Column() as main_column:
6337
  with gr.Tabs():
 
 
6338
  with gr.Tab("Code"):
6339
  code_output = gr.Code(
6340
  language="html",
@@ -6343,7 +6345,7 @@ with gr.Blocks(
6343
  label="Generated code"
6344
  )
6345
 
6346
-
6347
 
6348
  # Transformers.js multi-file editors (hidden by default)
6349
  with gr.Group(visible=False) as tjs_group:
@@ -6354,8 +6356,47 @@ with gr.Blocks(
6354
  tjs_js_code = gr.Code(language="javascript", lines=20, interactive=True, label="index.js")
6355
  with gr.Tab("style.css"):
6356
  tjs_css_code = gr.Code(language="css", lines=20, interactive=True, label="style.css")
6357
- with gr.Tab("Preview"):
6358
- sandbox = gr.HTML(label="Live preview")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6359
  # Removed Import Logs tab for cleaner UI
6360
  # History tab hidden per user request
6361
  # with gr.Tab("History"):
@@ -6499,6 +6540,165 @@ with gr.Blocks(
6499
  outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code],
6500
  )
6501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6502
  def sync_tjs_from_code(code_text, language):
6503
  if language != "transformers.js":
6504
  return [gr.update(), gr.update(), gr.update(), gr.update()]
@@ -6641,6 +6841,18 @@ with gr.Blocks(
6641
  toggle_editors,
6642
  inputs=[language_dropdown, code_output],
6643
  outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]
 
 
 
 
 
 
 
 
 
 
 
 
6644
  ).then(
6645
  show_deploy_components,
6646
  None,
@@ -6896,6 +7108,17 @@ with gr.Blocks(
6896
  toggle_editors,
6897
  inputs=[language_dropdown, code_output],
6898
  outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]
 
 
 
 
 
 
 
 
 
 
 
6899
  ).then(
6900
  show_deploy_components,
6901
  None,
 
6335
 
6336
  with gr.Column() as main_column:
6337
  with gr.Tabs():
6338
+ with gr.Tab("Preview"):
6339
+ sandbox = gr.HTML(label="Live preview")
6340
  with gr.Tab("Code"):
6341
  code_output = gr.Code(
6342
  language="html",
 
6345
  label="Generated code"
6346
  )
6347
 
6348
+
6349
 
6350
  # Transformers.js multi-file editors (hidden by default)
6351
  with gr.Group(visible=False) as tjs_group:
 
6356
  tjs_js_code = gr.Code(language="javascript", lines=20, interactive=True, label="index.js")
6357
  with gr.Tab("style.css"):
6358
  tjs_css_code = gr.Code(language="css", lines=20, interactive=True, label="style.css")
6359
+
6360
+ # Static HTML multi-file editors (hidden by default). Use separate tab groups for different file counts.
6361
+ with gr.Group(visible=False) as static_group_2:
6362
+ with gr.Tabs():
6363
+ with gr.Tab("index.html") as static_tab_2_1:
6364
+ static_code_2_1 = gr.Code(language="html", lines=20, interactive=True, label="index.html")
6365
+ with gr.Tab("file 2") as static_tab_2_2:
6366
+ static_code_2_2 = gr.Code(language="html", lines=18, interactive=True, label="file 2")
6367
+
6368
+ with gr.Group(visible=False) as static_group_3:
6369
+ with gr.Tabs():
6370
+ with gr.Tab("index.html") as static_tab_3_1:
6371
+ static_code_3_1 = gr.Code(language="html", lines=20, interactive=True, label="index.html")
6372
+ with gr.Tab("file 2") as static_tab_3_2:
6373
+ static_code_3_2 = gr.Code(language="html", lines=18, interactive=True, label="file 2")
6374
+ with gr.Tab("file 3") as static_tab_3_3:
6375
+ static_code_3_3 = gr.Code(language="html", lines=18, interactive=True, label="file 3")
6376
+
6377
+ with gr.Group(visible=False) as static_group_4:
6378
+ with gr.Tabs():
6379
+ with gr.Tab("index.html") as static_tab_4_1:
6380
+ static_code_4_1 = gr.Code(language="html", lines=20, interactive=True, label="index.html")
6381
+ with gr.Tab("file 2") as static_tab_4_2:
6382
+ static_code_4_2 = gr.Code(language="html", lines=18, interactive=True, label="file 2")
6383
+ with gr.Tab("file 3") as static_tab_4_3:
6384
+ static_code_4_3 = gr.Code(language="html", lines=18, interactive=True, label="file 3")
6385
+ with gr.Tab("file 4") as static_tab_4_4:
6386
+ static_code_4_4 = gr.Code(language="html", lines=18, interactive=True, label="file 4")
6387
+
6388
+ with gr.Group(visible=False) as static_group_5plus:
6389
+ with gr.Tabs():
6390
+ with gr.Tab("index.html") as static_tab_5_1:
6391
+ static_code_5_1 = gr.Code(language="html", lines=20, interactive=True, label="index.html")
6392
+ with gr.Tab("file 2") as static_tab_5_2:
6393
+ static_code_5_2 = gr.Code(language="html", lines=18, interactive=True, label="file 2")
6394
+ with gr.Tab("file 3") as static_tab_5_3:
6395
+ static_code_5_3 = gr.Code(language="html", lines=18, interactive=True, label="file 3")
6396
+ with gr.Tab("file 4") as static_tab_5_4:
6397
+ static_code_5_4 = gr.Code(language="html", lines=18, interactive=True, label="file 4")
6398
+ with gr.Tab("file 5") as static_tab_5_5:
6399
+ static_code_5_5 = gr.Code(language="html", lines=18, interactive=True, label="file 5")
6400
  # Removed Import Logs tab for cleaner UI
6401
  # History tab hidden per user request
6402
  # with gr.Tab("History"):
 
6540
  outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code],
6541
  )
6542
 
6543
+ # Static HTML multi-file toggling and population
6544
+ def toggle_static_editors(language, code_text):
6545
+ # If not static HTML language, ensure single editor visible and all static groups hidden
6546
+ if language != "html":
6547
+ return [
6548
+ gr.update(visible=True), # code_output
6549
+ gr.update(visible=False), # static_group_2
6550
+ gr.update(visible=False), # static_group_3
6551
+ gr.update(visible=False), # static_group_4
6552
+ gr.update(visible=False), # static_group_5plus
6553
+ # All tab and code components get empty updates (tab, code, tab, code, ...)
6554
+ gr.update(), gr.update(), gr.update(), gr.update(), # 2-file group
6555
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), # 3-file group
6556
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), # 4-file group
6557
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update() # 5-file group
6558
+ ]
6559
+
6560
+ files = parse_multipage_html_output(code_text or "")
6561
+ files = validate_and_autofix_files(files)
6562
+
6563
+ if not isinstance(files, dict) or len(files) <= 1:
6564
+ # No multi-file content; keep single editor
6565
+ return [
6566
+ gr.update(visible=True), # code_output
6567
+ gr.update(visible=False), # static_group_2
6568
+ gr.update(visible=False), # static_group_3
6569
+ gr.update(visible=False), # static_group_4
6570
+ gr.update(visible=False), # static_group_5plus
6571
+ # All tab and code components get empty updates (tab, code, tab, code, ...)
6572
+ gr.update(), gr.update(), gr.update(), gr.update(), # 2-file group
6573
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), # 3-file group
6574
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), # 4-file group
6575
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update() # 5-file group
6576
+ ]
6577
+
6578
+ # We have multi-file static output: hide single editor, show appropriate static group
6579
+ # Order: index.html first, then others sorted by path
6580
+ ordered_paths = []
6581
+ if 'index.html' in files:
6582
+ ordered_paths.append('index.html')
6583
+ for p in sorted(files.keys()):
6584
+ if p == 'index.html':
6585
+ continue
6586
+ ordered_paths.append(p)
6587
+
6588
+ # Map extension to language
6589
+ def _lang_for(path: str):
6590
+ p = (path or '').lower()
6591
+ if p.endswith('.html'):
6592
+ return 'html'
6593
+ if p.endswith('.css'):
6594
+ return 'css'
6595
+ if p.endswith('.js'):
6596
+ return 'javascript'
6597
+ if p.endswith('.json'):
6598
+ return 'json'
6599
+ if p.endswith('.md') or p.endswith('.markdown'):
6600
+ return 'markdown'
6601
+ return 'html'
6602
+
6603
+ num_files = len(ordered_paths)
6604
+
6605
+ # Hide single editor, show appropriate group based on file count
6606
+ updates = [gr.update(visible=False)] # code_output
6607
+
6608
+ if num_files == 2:
6609
+ updates.extend([
6610
+ gr.update(visible=True), # static_group_2
6611
+ gr.update(visible=False), # static_group_3
6612
+ gr.update(visible=False), # static_group_4
6613
+ gr.update(visible=False), # static_group_5plus
6614
+ ])
6615
+ # Populate 2-file group (tab labels + code content)
6616
+ path1, path2 = ordered_paths[0], ordered_paths[1]
6617
+ updates.extend([
6618
+ gr.update(label=path1), gr.update(value=files.get(path1, ''), label=path1, language=_lang_for(path1)),
6619
+ gr.update(label=path2), gr.update(value=files.get(path2, ''), label=path2, language=_lang_for(path2)),
6620
+ # Empty updates for unused groups
6621
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
6622
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
6623
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
6624
+ ])
6625
+ elif num_files == 3:
6626
+ updates.extend([
6627
+ gr.update(visible=False), # static_group_2
6628
+ gr.update(visible=True), # static_group_3
6629
+ gr.update(visible=False), # static_group_4
6630
+ gr.update(visible=False), # static_group_5plus
6631
+ ])
6632
+ # Populate 3-file group (tab labels + code content)
6633
+ path1, path2, path3 = ordered_paths[0], ordered_paths[1], ordered_paths[2]
6634
+ updates.extend([
6635
+ # Empty updates for 2-file group
6636
+ gr.update(), gr.update(), gr.update(), gr.update(),
6637
+ # Populate 3-file group
6638
+ gr.update(label=path1), gr.update(value=files.get(path1, ''), label=path1, language=_lang_for(path1)),
6639
+ gr.update(label=path2), gr.update(value=files.get(path2, ''), label=path2, language=_lang_for(path2)),
6640
+ gr.update(label=path3), gr.update(value=files.get(path3, ''), label=path3, language=_lang_for(path3)),
6641
+ # Empty updates for unused groups
6642
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
6643
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
6644
+ ])
6645
+ elif num_files == 4:
6646
+ updates.extend([
6647
+ gr.update(visible=False), # static_group_2
6648
+ gr.update(visible=False), # static_group_3
6649
+ gr.update(visible=True), # static_group_4
6650
+ gr.update(visible=False), # static_group_5plus
6651
+ ])
6652
+ # Populate 4-file group (tab labels + code content)
6653
+ paths = ordered_paths[:4]
6654
+ updates.extend([
6655
+ # Empty updates for 2-file and 3-file groups
6656
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
6657
+ # Populate 4-file group
6658
+ gr.update(label=paths[0]), gr.update(value=files.get(paths[0], ''), label=paths[0], language=_lang_for(paths[0])),
6659
+ gr.update(label=paths[1]), gr.update(value=files.get(paths[1], ''), label=paths[1], language=_lang_for(paths[1])),
6660
+ gr.update(label=paths[2]), gr.update(value=files.get(paths[2], ''), label=paths[2], language=_lang_for(paths[2])),
6661
+ gr.update(label=paths[3]), gr.update(value=files.get(paths[3], ''), label=paths[3], language=_lang_for(paths[3])),
6662
+ # Empty updates for 5+ group
6663
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
6664
+ ])
6665
+ else: # 5+ files
6666
+ updates.extend([
6667
+ gr.update(visible=False), # static_group_2
6668
+ gr.update(visible=False), # static_group_3
6669
+ gr.update(visible=False), # static_group_4
6670
+ gr.update(visible=True), # static_group_5plus
6671
+ ])
6672
+ # Populate 5+ file group (show first 5) (tab labels + code content)
6673
+ paths = ordered_paths[:5]
6674
+ updates.extend([
6675
+ # Empty updates for 2-file, 3-file, and 4-file groups
6676
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
6677
+ gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(), gr.update(),
6678
+ # Populate 5+ file group
6679
+ gr.update(label=paths[0]), gr.update(value=files.get(paths[0], ''), label=paths[0], language=_lang_for(paths[0])),
6680
+ gr.update(label=paths[1]), gr.update(value=files.get(paths[1], ''), label=paths[1], language=_lang_for(paths[1])),
6681
+ gr.update(label=paths[2]), gr.update(value=files.get(paths[2], ''), label=paths[2], language=_lang_for(paths[2])),
6682
+ gr.update(label=paths[3]), gr.update(value=files.get(paths[3], ''), label=paths[3], language=_lang_for(paths[3])),
6683
+ gr.update(label=paths[4]), gr.update(value=files.get(paths[4], ''), label=paths[4], language=_lang_for(paths[4]))
6684
+ ])
6685
+
6686
+ return updates
6687
+
6688
+ # Respond to language change to show/hide static multi-file editors appropriately
6689
+ language_dropdown.change(
6690
+ toggle_static_editors,
6691
+ inputs=[language_dropdown, code_output],
6692
+ outputs=[
6693
+ code_output,
6694
+ static_group_2, static_group_3, static_group_4, static_group_5plus,
6695
+ static_tab_2_1, static_code_2_1, static_tab_2_2, static_code_2_2,
6696
+ static_tab_3_1, static_code_3_1, static_tab_3_2, static_code_3_2, static_tab_3_3, static_code_3_3,
6697
+ static_tab_4_1, static_code_4_1, static_tab_4_2, static_code_4_2, static_tab_4_3, static_code_4_3, static_tab_4_4, static_code_4_4,
6698
+ static_tab_5_1, static_code_5_1, static_tab_5_2, static_code_5_2, static_tab_5_3, static_code_5_3, static_tab_5_4, static_code_5_4, static_tab_5_5, static_code_5_5,
6699
+ ],
6700
+ )
6701
+
6702
  def sync_tjs_from_code(code_text, language):
6703
  if language != "transformers.js":
6704
  return [gr.update(), gr.update(), gr.update(), gr.update()]
 
6841
  toggle_editors,
6842
  inputs=[language_dropdown, code_output],
6843
  outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]
6844
+ ).then(
6845
+ # After generation, toggle static multi-file editors for HTML
6846
+ toggle_static_editors,
6847
+ inputs=[language_dropdown, code_output],
6848
+ outputs=[
6849
+ code_output,
6850
+ static_group_2, static_group_3, static_group_4, static_group_5plus,
6851
+ static_tab_2_1, static_code_2_1, static_tab_2_2, static_code_2_2,
6852
+ static_tab_3_1, static_code_3_1, static_tab_3_2, static_code_3_2, static_tab_3_3, static_code_3_3,
6853
+ static_tab_4_1, static_code_4_1, static_tab_4_2, static_code_4_2, static_tab_4_3, static_code_4_3, static_tab_4_4, static_code_4_4,
6854
+ static_tab_5_1, static_code_5_1, static_tab_5_2, static_code_5_2, static_tab_5_3, static_code_5_3, static_tab_5_4, static_code_5_4, static_tab_5_5, static_code_5_5,
6855
+ ]
6856
  ).then(
6857
  show_deploy_components,
6858
  None,
 
7108
  toggle_editors,
7109
  inputs=[language_dropdown, code_output],
7110
  outputs=[code_output, tjs_group, tjs_html_code, tjs_js_code, tjs_css_code]
7111
+ ).then(
7112
+ toggle_static_editors,
7113
+ inputs=[language_dropdown, code_output],
7114
+ outputs=[
7115
+ code_output,
7116
+ static_group_2, static_group_3, static_group_4, static_group_5plus,
7117
+ static_tab_2_1, static_code_2_1, static_tab_2_2, static_code_2_2,
7118
+ static_tab_3_1, static_code_3_1, static_tab_3_2, static_code_3_2, static_tab_3_3, static_code_3_3,
7119
+ static_tab_4_1, static_code_4_1, static_tab_4_2, static_code_4_2, static_tab_4_3, static_code_4_3, static_tab_4_4, static_code_4_4,
7120
+ static_tab_5_1, static_code_5_1, static_tab_5_2, static_code_5_2, static_tab_5_3, static_code_5_3, static_tab_5_4, static_code_5_4, static_tab_5_5, static_code_5_5,
7121
+ ]
7122
  ).then(
7123
  show_deploy_components,
7124
  None,