Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
|
3 |
def update_ui(code):
|
4 |
try:
|
5 |
-
#
|
6 |
return f"""
|
7 |
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
8 |
{code}
|
@@ -20,34 +20,11 @@ def interface():
|
|
20 |
ui_preview = gr.HTML(label="UI Preview")
|
21 |
error_output = gr.Textbox(label="Error", interactive=False)
|
22 |
|
23 |
-
# JavaScript
|
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.querySelectorAll('script');
|
36 |
-
scripts.forEach(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 |
-
});
|
42 |
-
|
43 |
-
// Wrap the content in a styled div for better presentation
|
44 |
-
return `
|
45 |
-
<div style="border: 1px solid #ccc; padding: 10px; height: 400px; overflow: auto;">
|
46 |
-
${tempDiv.innerHTML}
|
47 |
-
</div>
|
48 |
-
`;
|
49 |
-
}
|
50 |
-
"""
|
51 |
)
|
52 |
|
53 |
# Add example section
|
@@ -82,68 +59,45 @@ def interface():
|
|
82 |
""", "Flexbox Layout"),
|
83 |
("""
|
84 |
<style>
|
85 |
-
body { margin: 0; padding: 0;
|
86 |
-
#gameCanvas { border: 1px solid
|
87 |
</style>
|
88 |
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
89 |
<script>
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
let
|
94 |
-
let
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
cells: [],
|
100 |
-
maxCells: 4
|
101 |
-
};
|
102 |
-
let apple = {x: 0, y: 0};
|
103 |
-
let score = 0;
|
104 |
|
105 |
-
function
|
106 |
-
requestAnimationFrame(gameLoop);
|
107 |
-
if (++count < 4) return;
|
108 |
-
count = 0;
|
109 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
110 |
-
snake.x
|
111 |
-
snake.
|
112 |
-
if
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
if (snake.cells.length > snake.maxCells) snake.cells.pop();
|
118 |
-
ctx.fillStyle = 'red';
|
119 |
-
ctx.fillRect(apple.x, apple.y, grid - 1, grid - 1);
|
120 |
-
ctx.fillStyle = 'green';
|
121 |
-
snake.cells.forEach(function(cell, index) {
|
122 |
-
ctx.fillRect(cell.x, cell.y, grid - 1, grid - 1);
|
123 |
-
if (index !== 0 && cell.x === snake.x && cell.y === snake.y) {
|
124 |
-
snake.maxCells = 4;
|
125 |
-
score = 0;
|
126 |
-
}
|
127 |
});
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
133 |
}
|
134 |
-
ctx.fillStyle = 'white';
|
135 |
-
ctx.font = '20px Arial';
|
136 |
-
ctx.fillText('Score: ' + score, 10, 20);
|
137 |
}
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
else if (e.which === 39 && snake.dx === 0) { snake.dx = grid; snake.dy = 0; }
|
142 |
-
else if (e.which === 40 && snake.dy === 0) { snake.dx = 0; snake.dy = grid; }
|
143 |
-
});
|
144 |
-
apple.x = Math.floor(Math.random() * 20) * grid;
|
145 |
-
apple.y = Math.floor(Math.random() * 20) * grid;
|
146 |
-
requestAnimationFrame(gameLoop);
|
147 |
</script>
|
148 |
""", "Snake Game"),
|
149 |
]
|
|
|
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}
|
|
|
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
|
|
|
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 |
]
|