Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,28 +2,11 @@ import gradio as gr
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
-
#
|
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 |
-
{
|
23 |
</div>
|
24 |
-
<script>
|
25 |
-
{js_content}
|
26 |
-
</script>
|
27 |
""", None
|
28 |
except Exception as e:
|
29 |
return "", str(e)
|
@@ -37,63 +20,85 @@ def interface():
|
|
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 |
-
#
|
47 |
with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
|
48 |
with gr.Column():
|
49 |
example_buttons = []
|
50 |
example_codes = [
|
51 |
-
|
|
|
|
|
52 |
("""
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
<
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
}
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
</
|
97 |
""", "Snake Game"),
|
98 |
]
|
99 |
|
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
+
# Directly return the code wrapped in a div for styling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
return f"""
|
7 |
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
8 |
+
{code}
|
9 |
</div>
|
|
|
|
|
|
|
10 |
""", None
|
11 |
except Exception as e:
|
12 |
return "", str(e)
|
|
|
20 |
ui_preview = gr.HTML(label="UI Preview")
|
21 |
error_output = gr.Textbox(label="Error", interactive=False)
|
22 |
|
23 |
+
# Instead of using _js, we'll directly handle the JavaScript with Python
|
24 |
code_input.change(
|
25 |
fn=update_ui,
|
26 |
inputs=code_input,
|
27 |
outputs=[ui_preview, error_output]
|
28 |
)
|
29 |
|
30 |
+
# Add example section
|
31 |
with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
|
32 |
with gr.Column():
|
33 |
example_buttons = []
|
34 |
example_codes = [
|
35 |
+
('<h1>Hello World</h1>', "Basic Heading"),
|
36 |
+
('<p style="color: red;">This is a paragraph.</p>', "Styled Paragraph"),
|
37 |
+
('<button onclick="alert(\'Button clicked!\')">Click me</button>', "Interactive Button"),
|
38 |
("""
|
39 |
+
<style>
|
40 |
+
.box {
|
41 |
+
width: 100px;
|
42 |
+
height: 100px;
|
43 |
+
background-color: blue;
|
44 |
+
}
|
45 |
+
</style>
|
46 |
+
<div class="box"></div>
|
47 |
+
""", "CSS Box"),
|
48 |
+
("""
|
49 |
+
<style>
|
50 |
+
.container {
|
51 |
+
display: flex;
|
52 |
+
justify-content: space-between;
|
53 |
+
}
|
54 |
+
</style>
|
55 |
+
<div class="container">
|
56 |
+
<div style="background-color: red; width: 50px; height: 50px;"></div>
|
57 |
+
<div style="background-color: green; width: 50px; height: 50px;"></div>
|
58 |
+
</div>
|
59 |
+
""", "Flexbox Layout"),
|
60 |
+
("""
|
61 |
+
<style>
|
62 |
+
body { margin: 0; padding: 0; }
|
63 |
+
#gameCanvas { border: 1px solid black; }
|
64 |
+
</style>
|
65 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
66 |
+
<script>
|
67 |
+
let canvas = document.getElementById('gameCanvas');
|
68 |
+
let ctx = canvas.getContext('2d');
|
69 |
+
let snake = [{x: 10, y: 10}];
|
70 |
+
let food = {x: 15, y: 15};
|
71 |
+
let gameLoop;
|
72 |
+
|
73 |
+
function init() {
|
74 |
+
gameLoop = setInterval(game, 100);
|
75 |
+
}
|
76 |
+
|
77 |
+
function game() {
|
78 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
79 |
+
snake.unshift({x: snake[0].x, y: snake[0].y});
|
80 |
+
snake[0].x += 1; // Simple movement for demonstration
|
81 |
+
if(snake[0].x > 39) snake[0].x = 0; // Wrap around
|
82 |
+
|
83 |
+
ctx.fillStyle = "green";
|
84 |
+
snake.forEach(part => {
|
85 |
+
ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
|
86 |
+
});
|
87 |
+
|
88 |
+
ctx.fillStyle = "red";
|
89 |
+
ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
|
90 |
+
|
91 |
+
if(snake[0].x == food.x && snake[0].y == food.y) {
|
92 |
+
food.x = Math.floor(Math.random() * 40);
|
93 |
+
food.y = Math.floor(Math.random() * 40);
|
94 |
+
} else {
|
95 |
+
snake.pop();
|
96 |
}
|
97 |
+
}
|
98 |
+
|
99 |
+
// Start the game
|
100 |
+
init();
|
101 |
+
</script>
|
102 |
""", "Snake Game"),
|
103 |
]
|
104 |
|