Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,9 @@ import gradio as gr
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
-
#
|
6 |
return f"""
|
7 |
-
<
|
8 |
-
{code}
|
9 |
-
</div>
|
10 |
""", None
|
11 |
except Exception as e:
|
12 |
return "", str(e)
|
@@ -17,10 +15,10 @@ def interface():
|
|
17 |
with gr.Row():
|
18 |
code_input = gr.Code(label="Write your HTML/CSS/JS here", language="html")
|
19 |
with gr.Column():
|
20 |
-
ui_preview = gr.HTML(label="UI Preview"
|
21 |
error_output = gr.Textbox(label="Error", interactive=False)
|
22 |
|
23 |
-
#
|
24 |
code_input.change(
|
25 |
fn=update_ui,
|
26 |
inputs=code_input,
|
@@ -32,73 +30,52 @@ def interface():
|
|
32 |
with gr.Column():
|
33 |
example_buttons = []
|
34 |
example_codes = [
|
35 |
-
(
|
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 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
<
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
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 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
""", "Snake Game"),
|
103 |
]
|
104 |
|
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
+
# Wrap the code in a secure iframe to run JavaScript safely
|
6 |
return f"""
|
7 |
+
<iframe srcdoc='{code}' style="border: 1px solid #ccc; padding: 10px; height: 400px; width: 100%;"></iframe>
|
|
|
|
|
8 |
""", None
|
9 |
except Exception as e:
|
10 |
return "", str(e)
|
|
|
15 |
with gr.Row():
|
16 |
code_input = gr.Code(label="Write your HTML/CSS/JS here", language="html")
|
17 |
with gr.Column():
|
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,
|
|
|
30 |
with gr.Column():
|
31 |
example_buttons = []
|
32 |
example_codes = [
|
33 |
+
# ... (other examples remain unchanged)
|
|
|
|
|
34 |
("""
|
35 |
+
<!DOCTYPE html>
|
36 |
+
<html lang="en">
|
37 |
+
<head>
|
38 |
+
<meta charset="UTF-8">
|
39 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
40 |
+
<style>
|
41 |
+
body { margin: 0; padding: 0; }
|
42 |
+
#gameCanvas { border: 1px solid black; }
|
43 |
+
</style>
|
44 |
+
</head>
|
45 |
+
<body>
|
46 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
47 |
+
<script>
|
48 |
+
let canvas = document.getElementById('gameCanvas');
|
49 |
+
let ctx = canvas.getContext('2d');
|
50 |
+
let snake = [{x: 10, y: 10}];
|
51 |
+
let food = {x: 15, y: 15};
|
52 |
+
let gameLoop;
|
53 |
+
function init() {
|
54 |
+
gameLoop = setInterval(game, 100);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
}
|
56 |
+
function game() {
|
57 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
58 |
+
snake.unshift({x: snake[0].x, y: snake[0].y});
|
59 |
+
snake[0].x += 1; // Simple movement for demonstration
|
60 |
+
if(snake[0].x > 39) snake[0].x = 0; // Wrap around
|
61 |
+
ctx.fillStyle = "green";
|
62 |
+
snake.forEach(part => {
|
63 |
+
ctx.fillRect(part.x * 10, part.y * 10, 10, 10);
|
64 |
+
});
|
65 |
+
ctx.fillStyle = "red";
|
66 |
+
ctx.fillRect(food.x * 10, food.y * 10, 10, 10);
|
67 |
+
if(snake[0].x == food.x && snake[0].y == food.y) {
|
68 |
+
food.x = Math.floor(Math.random() * 40);
|
69 |
+
food.y = Math.floor(Math.random() * 40);
|
70 |
+
} else {
|
71 |
+
snake.pop();
|
72 |
+
}
|
73 |
+
}
|
74 |
+
// Start the game
|
75 |
+
init();
|
76 |
+
</script>
|
77 |
+
</body>
|
78 |
+
</html>
|
79 |
""", "Snake Game"),
|
80 |
]
|
81 |
|