Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,7 @@ import gradio as gr
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
-
#
|
6 |
-
# Gradio's HTML component can render HTML/CSS directly, but JS needs special handling
|
7 |
return f"""
|
8 |
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
9 |
{code}
|
@@ -21,32 +20,29 @@ def interface():
|
|
21 |
ui_preview = gr.HTML(label="UI Preview")
|
22 |
error_output = gr.Textbox(label="Error", interactive=False)
|
23 |
|
24 |
-
#
|
25 |
code_input.change(
|
26 |
fn=update_ui,
|
27 |
inputs=code_input,
|
28 |
outputs=[ui_preview, error_output],
|
29 |
-
|
30 |
function(code) {
|
31 |
// Create a temporary div to parse the HTML/CSS/JS
|
32 |
const tempDiv = document.createElement('div');
|
33 |
tempDiv.innerHTML = code;
|
34 |
-
|
35 |
// Handle JavaScript by creating new script elements
|
36 |
-
const scripts = tempDiv.
|
37 |
-
scripts.
|
38 |
const newScript = document.createElement('script');
|
39 |
newScript.textContent = script.textContent;
|
40 |
document.body.appendChild(newScript);
|
41 |
script.remove(); // Remove original script to avoid double execution
|
|
|
42 |
});
|
43 |
-
|
44 |
-
//
|
45 |
-
return
|
46 |
-
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
47 |
-
${tempDiv.innerHTML}
|
48 |
-
</div>
|
49 |
-
`;
|
50 |
}
|
51 |
"""
|
52 |
)
|
@@ -56,31 +52,7 @@ def interface():
|
|
56 |
with gr.Column():
|
57 |
example_buttons = []
|
58 |
example_codes = [
|
59 |
-
(
|
60 |
-
('<p style="color: red;">This is a paragraph.</p>', "Styled Paragraph"),
|
61 |
-
('<button onclick="alert(\'Button clicked!\')">Click me</button>', "Interactive Button"),
|
62 |
-
("""
|
63 |
-
<style>
|
64 |
-
.box {
|
65 |
-
width: 100px;
|
66 |
-
height: 100px;
|
67 |
-
background-color: blue;
|
68 |
-
}
|
69 |
-
</style>
|
70 |
-
<div class="box"></div>
|
71 |
-
""", "CSS Box"),
|
72 |
-
("""
|
73 |
-
<style>
|
74 |
-
.container {
|
75 |
-
display: flex;
|
76 |
-
justify-content: space-between;
|
77 |
-
}
|
78 |
-
</style>
|
79 |
-
<div class="container">
|
80 |
-
<div style="background-color: red; width: 50px; height: 50px;"></div>
|
81 |
-
<div style="background-color: green; width: 50px; height: 50px;"></div>
|
82 |
-
</div>
|
83 |
-
""", "Flexbox Layout"),
|
84 |
("""
|
85 |
<!DOCTYPE html>
|
86 |
<html lang="en">
|
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
+
# We'll return the HTML with CSS, and let JavaScript be handled by the browser
|
|
|
6 |
return f"""
|
7 |
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
8 |
{code}
|
|
|
20 |
ui_preview = gr.HTML(label="UI Preview")
|
21 |
error_output = gr.Textbox(label="Error", interactive=False)
|
22 |
|
23 |
+
# Use _js to handle JavaScript execution in the browser context
|
24 |
code_input.change(
|
25 |
fn=update_ui,
|
26 |
inputs=code_input,
|
27 |
outputs=[ui_preview, error_output],
|
28 |
+
_js="""
|
29 |
function(code) {
|
30 |
// Create a temporary div to parse the HTML/CSS/JS
|
31 |
const tempDiv = document.createElement('div');
|
32 |
tempDiv.innerHTML = code;
|
33 |
+
|
34 |
// Handle JavaScript by creating new script elements
|
35 |
+
const scripts = tempDiv.getElementsByTagName('script');
|
36 |
+
const scriptContents = Array.from(scripts).map(script => {
|
37 |
const newScript = document.createElement('script');
|
38 |
newScript.textContent = script.textContent;
|
39 |
document.body.appendChild(newScript);
|
40 |
script.remove(); // Remove original script to avoid double execution
|
41 |
+
return script.textContent;
|
42 |
});
|
43 |
+
|
44 |
+
// Return the HTML content without the script tags for Gradio to display
|
45 |
+
return tempDiv.innerHTML;
|
|
|
|
|
|
|
|
|
46 |
}
|
47 |
"""
|
48 |
)
|
|
|
52 |
with gr.Column():
|
53 |
example_buttons = []
|
54 |
example_codes = [
|
55 |
+
# ... (your existing examples here)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
("""
|
57 |
<!DOCTYPE html>
|
58 |
<html lang="en">
|