akhaliq HF Staff commited on
Commit
50d0f64
Β·
1 Parent(s): e841faa

json static

Browse files
Files changed (1) hide show
  1. app.py +176 -1
app.py CHANGED
@@ -6556,6 +6556,175 @@ def wrap_html_in_static_app(html_code):
6556
  # For static Spaces, just use the HTML code as-is
6557
  return html_code
6558
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6559
  def deploy_to_spaces_static(code):
6560
  if not code or not code.strip():
6561
  return # Do nothing if code is empty
@@ -8134,7 +8303,8 @@ with gr.Blocks(
8134
  "streamlit": "docker", # Use 'docker' for Streamlit Spaces
8135
  "html": "static",
8136
  "transformers.js": "static", # Transformers.js uses static SDK
8137
- "svelte": "static" # Svelte uses static SDK
 
8138
  }
8139
  sdk = language_to_sdk_map.get(language, "gradio")
8140
 
@@ -8532,6 +8702,11 @@ with gr.Blocks(
8532
  # Fallback: single-file static HTML (upload index.html only)
8533
  file_name = "index.html"
8534
 
 
 
 
 
 
8535
  # Upload temporary media files to HF and replace URLs (only for Static HTML, not Transformers.js)
8536
  if sdk == "static" and language == "html":
8537
  print("[Deploy] Uploading temporary media files to HF and updating URLs for single-file static HTML app")
 
6556
  # For static Spaces, just use the HTML code as-is
6557
  return html_code
6558
 
6559
+ def prettify_comfyui_json_for_html(json_content: str) -> str:
6560
+ """Convert ComfyUI JSON to prettified HTML display"""
6561
+ try:
6562
+ import json
6563
+ # Parse and prettify the JSON
6564
+ parsed_json = json.loads(json_content)
6565
+ prettified_json = json.dumps(parsed_json, indent=2, ensure_ascii=False)
6566
+
6567
+ # Create HTML wrapper with syntax highlighting
6568
+ html_content = f"""<!DOCTYPE html>
6569
+ <html lang="en">
6570
+ <head>
6571
+ <meta charset="UTF-8">
6572
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6573
+ <title>ComfyUI Workflow</title>
6574
+ <style>
6575
+ body {{
6576
+ font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
6577
+ background-color: #1e1e1e;
6578
+ color: #d4d4d4;
6579
+ margin: 0;
6580
+ padding: 20px;
6581
+ line-height: 1.4;
6582
+ }}
6583
+ .header {{
6584
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
6585
+ color: white;
6586
+ padding: 20px;
6587
+ border-radius: 10px;
6588
+ margin-bottom: 20px;
6589
+ text-align: center;
6590
+ }}
6591
+ .header h1 {{
6592
+ margin: 0;
6593
+ font-size: 2em;
6594
+ }}
6595
+ .header a {{
6596
+ color: #ffffff;
6597
+ text-decoration: none;
6598
+ font-weight: bold;
6599
+ opacity: 0.9;
6600
+ }}
6601
+ .header a:hover {{
6602
+ opacity: 1;
6603
+ text-decoration: underline;
6604
+ }}
6605
+ .json-container {{
6606
+ background-color: #2d2d30;
6607
+ border-radius: 8px;
6608
+ padding: 20px;
6609
+ overflow-x: auto;
6610
+ border: 1px solid #3e3e42;
6611
+ }}
6612
+ pre {{
6613
+ margin: 0;
6614
+ white-space: pre-wrap;
6615
+ word-wrap: break-word;
6616
+ }}
6617
+ .json-key {{
6618
+ color: #9cdcfe;
6619
+ }}
6620
+ .json-string {{
6621
+ color: #ce9178;
6622
+ }}
6623
+ .json-number {{
6624
+ color: #b5cea8;
6625
+ }}
6626
+ .json-boolean {{
6627
+ color: #569cd6;
6628
+ }}
6629
+ .json-null {{
6630
+ color: #569cd6;
6631
+ }}
6632
+ .copy-btn {{
6633
+ background: #007acc;
6634
+ color: white;
6635
+ border: none;
6636
+ padding: 10px 20px;
6637
+ border-radius: 5px;
6638
+ cursor: pointer;
6639
+ margin-bottom: 10px;
6640
+ font-family: inherit;
6641
+ }}
6642
+ .copy-btn:hover {{
6643
+ background: #005a9e;
6644
+ }}
6645
+ .download-btn {{
6646
+ background: #28a745;
6647
+ color: white;
6648
+ border: none;
6649
+ padding: 10px 20px;
6650
+ border-radius: 5px;
6651
+ cursor: pointer;
6652
+ margin-bottom: 10px;
6653
+ margin-left: 10px;
6654
+ font-family: inherit;
6655
+ }}
6656
+ .download-btn:hover {{
6657
+ background: #218838;
6658
+ }}
6659
+ </style>
6660
+ </head>
6661
+ <body>
6662
+ <div class="header">
6663
+ <h1>ComfyUI Workflow</h1>
6664
+ <p>Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>
6665
+ </div>
6666
+
6667
+ <button class="copy-btn" onclick="copyToClipboard()">πŸ“‹ Copy JSON</button>
6668
+ <button class="download-btn" onclick="downloadJSON()">πŸ’Ύ Download JSON</button>
6669
+
6670
+ <div class="json-container">
6671
+ <pre id="json-content">{prettified_json}</pre>
6672
+ </div>
6673
+
6674
+ <script>
6675
+ function copyToClipboard() {{
6676
+ const jsonContent = document.getElementById('json-content').textContent;
6677
+ navigator.clipboard.writeText(jsonContent).then(() => {{
6678
+ const btn = document.querySelector('.copy-btn');
6679
+ const originalText = btn.textContent;
6680
+ btn.textContent = 'βœ… Copied!';
6681
+ setTimeout(() => {{
6682
+ btn.textContent = originalText;
6683
+ }}, 2000);
6684
+ }});
6685
+ }}
6686
+
6687
+ function downloadJSON() {{
6688
+ const jsonContent = document.getElementById('json-content').textContent;
6689
+ const blob = new Blob([jsonContent], {{ type: 'application/json' }});
6690
+ const url = URL.createObjectURL(blob);
6691
+ const a = document.createElement('a');
6692
+ a.href = url;
6693
+ a.download = 'comfyui_workflow.json';
6694
+ document.body.appendChild(a);
6695
+ a.click();
6696
+ document.body.removeChild(a);
6697
+ URL.revokeObjectURL(url);
6698
+ }}
6699
+
6700
+ // Add syntax highlighting
6701
+ function highlightJSON() {{
6702
+ const content = document.getElementById('json-content');
6703
+ let html = content.innerHTML;
6704
+
6705
+ // Highlight different JSON elements
6706
+ html = html.replace(/"([^"]+)":/g, '<span class="json-key">"$1":</span>');
6707
+ html = html.replace(/: "([^"]*)"/g, ': <span class="json-string">"$1"</span>');
6708
+ html = html.replace(/: (-?\d+\.?\d*)/g, ': <span class="json-number">$1</span>');
6709
+ html = html.replace(/: (true|false)/g, ': <span class="json-boolean">$1</span>');
6710
+ html = html.replace(/: null/g, ': <span class="json-null">null</span>');
6711
+
6712
+ content.innerHTML = html;
6713
+ }}
6714
+
6715
+ // Apply syntax highlighting after page load
6716
+ window.addEventListener('load', highlightJSON);
6717
+ </script>
6718
+ </body>
6719
+ </html>"""
6720
+ return html_content
6721
+ except json.JSONDecodeError:
6722
+ # If it's not valid JSON, return as-is
6723
+ return json_content
6724
+ except Exception as e:
6725
+ print(f"Error prettifying ComfyUI JSON: {e}")
6726
+ return json_content
6727
+
6728
  def deploy_to_spaces_static(code):
6729
  if not code or not code.strip():
6730
  return # Do nothing if code is empty
 
8303
  "streamlit": "docker", # Use 'docker' for Streamlit Spaces
8304
  "html": "static",
8305
  "transformers.js": "static", # Transformers.js uses static SDK
8306
+ "svelte": "static", # Svelte uses static SDK
8307
+ "comfyui": "static" # ComfyUI uses static SDK
8308
  }
8309
  sdk = language_to_sdk_map.get(language, "gradio")
8310
 
 
8702
  # Fallback: single-file static HTML (upload index.html only)
8703
  file_name = "index.html"
8704
 
8705
+ # Special handling for ComfyUI: prettify JSON and wrap in HTML
8706
+ if language == "comfyui":
8707
+ print("[Deploy] Converting ComfyUI JSON to prettified HTML display")
8708
+ code = prettify_comfyui_json_for_html(code)
8709
+
8710
  # Upload temporary media files to HF and replace URLs (only for Static HTML, not Transformers.js)
8711
  if sdk == "static" and language == "html":
8712
  print("[Deploy] Uploading temporary media files to HF and updating URLs for single-file static HTML app")