Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,28 @@ import gradio as gr
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
return f"""
|
7 |
-
<
|
|
|
|
|
|
|
|
|
|
|
8 |
""", None
|
9 |
except Exception as e:
|
10 |
return "", str(e)
|
@@ -18,14 +37,13 @@ def interface():
|
|
18 |
ui_preview = gr.HTML(label="UI Preview")
|
19 |
error_output = gr.Textbox(label="Error", interactive=False)
|
20 |
|
21 |
-
# Handle JavaScript execution by using an iframe
|
22 |
code_input.change(
|
23 |
fn=update_ui,
|
24 |
inputs=code_input,
|
25 |
outputs=[ui_preview, error_output]
|
26 |
)
|
27 |
|
28 |
-
#
|
29 |
with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
|
30 |
with gr.Column():
|
31 |
example_buttons = []
|
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
+
# Strip out scripts and handle them separately to avoid security issues
|
6 |
+
from bs4 import BeautifulSoup
|
7 |
+
|
8 |
+
soup = BeautifulSoup(code, 'html.parser')
|
9 |
+
scripts = soup.find_all('script')
|
10 |
+
for script in scripts:
|
11 |
+
script.decompose() # Remove script tags from HTML to prevent direct execution
|
12 |
+
|
13 |
+
# Rebuild HTML without scripts for direct rendering
|
14 |
+
html_content = str(soup)
|
15 |
+
|
16 |
+
# Collect script content
|
17 |
+
js_content = '\n'.join([script.string for script in scripts if script.string])
|
18 |
+
|
19 |
+
# Return HTML wrapped in a div and scripts in a way that Gradio can handle
|
20 |
return f"""
|
21 |
+
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
22 |
+
{html_content}
|
23 |
+
</div>
|
24 |
+
<script>
|
25 |
+
{js_content}
|
26 |
+
</script>
|
27 |
""", None
|
28 |
except Exception as e:
|
29 |
return "", str(e)
|
|
|
37 |
ui_preview = gr.HTML(label="UI Preview")
|
38 |
error_output = gr.Textbox(label="Error", interactive=False)
|
39 |
|
|
|
40 |
code_input.change(
|
41 |
fn=update_ui,
|
42 |
inputs=code_input,
|
43 |
outputs=[ui_preview, error_output]
|
44 |
)
|
45 |
|
46 |
+
# Example codes remain the same, but ensure the snake game is full HTML
|
47 |
with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
|
48 |
with gr.Column():
|
49 |
example_buttons = []
|