tiahchia commited on
Commit
a28fac5
·
verified ·
1 Parent(s): 7914ef5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -14,7 +14,7 @@ def run_python(code):
14
  output = f.getvalue()
15
  return f'<pre style="color:#00ff00; font-family: monospace;">{html.escape(output)}</pre>'
16
 
17
- # --- Web runner ---
18
  def run_web(code):
19
  # If user includes <html> or <!DOCTYPE>, use it as-is
20
  if "<html" in code.lower() or "<!doctype" in code.lower():
@@ -39,7 +39,26 @@ def run_web(code):
39
  </body>
40
  </html>
41
  """
42
- return f'<iframe style="width:100%; height:100%; border:none; border-radius:8px; background:#1e1e1e;" srcdoc="{html_content}"></iframe>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
  # --- JavaScript runner ---
45
  def run_js(code):
 
14
  output = f.getvalue()
15
  return f'<pre style="color:#00ff00; font-family: monospace;">{html.escape(output)}</pre>'
16
 
17
+ # --- Web runner (Blob URL method) ---
18
  def run_web(code):
19
  # If user includes <html> or <!DOCTYPE>, use it as-is
20
  if "<html" in code.lower() or "<!doctype" in code.lower():
 
39
  </body>
40
  </html>
41
  """
42
+
43
+ # --- Blob-based iframe injection ---
44
+ # This approach lets images, links, and external scripts work properly.
45
+ escaped = html_content.replace("\\", "\\\\").replace("`", "\\`")
46
+ return f"""
47
+ <div id='preview-container' style='width:100%;height:100%;'>
48
+ <script>
49
+ const htmlContent = `{escaped}`;
50
+ const blob = new Blob([htmlContent], {{ type: 'text/html' }});
51
+ const url = URL.createObjectURL(blob);
52
+ const iframe = document.createElement('iframe');
53
+ iframe.style.width = '100%';
54
+ iframe.style.height = '100%';
55
+ iframe.style.border = 'none';
56
+ iframe.style.borderRadius = '8px';
57
+ iframe.src = url;
58
+ document.currentScript.parentElement.appendChild(iframe);
59
+ </script>
60
+ </div>
61
+ """
62
 
63
  # --- JavaScript runner ---
64
  def run_js(code):