Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,160 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
}
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
-
|
49 |
-
gr.HTML(snake_game(), elem_id="snake-game-block")
|
50 |
|
51 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def update_ui(code):
|
4 |
+
try:
|
5 |
+
# Wrap the user-provided code in a styled container
|
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)
|
13 |
+
|
14 |
+
def interface():
|
15 |
+
with gr.Blocks() as demo:
|
16 |
+
gr.Markdown("# Learn Frontend UI")
|
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 |
+
# JavaScript execution handling
|
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
|
54 |
+
with gr.Accordion("HTML/CSS/JS Code Examples", open=False):
|
55 |
+
with gr.Column():
|
56 |
+
example_buttons = []
|
57 |
+
example_codes = [
|
58 |
+
('<h1>Hello World</h1>', "Basic Heading"),
|
59 |
+
('<p style="color: red;">This is a paragraph.</p>', "Styled Paragraph"),
|
60 |
+
('<button onclick="alert(\'Button clicked!\')">Click me</button>', "Interactive Button"),
|
61 |
+
("""
|
62 |
+
<style>
|
63 |
+
.box {
|
64 |
+
width: 100px;
|
65 |
+
height: 100px;
|
66 |
+
background-color: blue;
|
67 |
+
}
|
68 |
+
</style>
|
69 |
+
<div class="box"></div>
|
70 |
+
""", "CSS Box"),
|
71 |
+
("""
|
72 |
+
<style>
|
73 |
+
.container {
|
74 |
+
display: flex;
|
75 |
+
justify-content: space-between;
|
76 |
+
}
|
77 |
+
</style>
|
78 |
+
<div class="container">
|
79 |
+
<div style="background-color: red; width: 50px; height: 50px;"></div>
|
80 |
+
<div style="background-color: green; width: 50px; height: 50px;"></div>
|
81 |
+
</div>
|
82 |
+
""", "Flexbox Layout"),
|
83 |
+
("""
|
84 |
+
<style>
|
85 |
+
body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100%; background-color: #000; }
|
86 |
+
#gameCanvas { border: 1px solid #fff; }
|
87 |
+
</style>
|
88 |
+
<canvas id="gameCanvas" width="400" height="400"></canvas>
|
89 |
+
<script>
|
90 |
+
const canvas = document.getElementById('gameCanvas');
|
91 |
+
const ctx = canvas.getContext('2d');
|
92 |
+
const grid = 20;
|
93 |
+
let count = 0;
|
94 |
+
let snake = {
|
95 |
+
x: grid * 5,
|
96 |
+
y: grid * 5,
|
97 |
+
dx: grid,
|
98 |
+
dy: 0,
|
99 |
+
cells: [],
|
100 |
+
maxCells: 4
|
101 |
+
};
|
102 |
+
let apple = {x: 0, y: 0};
|
103 |
+
let score = 0;
|
104 |
+
|
105 |
+
function gameLoop() {
|
106 |
+
requestAnimationFrame(gameLoop);
|
107 |
+
if (++count < 4) return;
|
108 |
+
count = 0;
|
109 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
110 |
+
snake.x += snake.dx;
|
111 |
+
snake.y += snake.dy;
|
112 |
+
if (snake.x < 0) snake.x = canvas.width - grid;
|
113 |
+
else if (snake.x >= canvas.width) snake.x = 0;
|
114 |
+
if (snake.y < 0) snake.y = canvas.height - grid;
|
115 |
+
else if (snake.y >= canvas.height) snake.y = 0;
|
116 |
+
snake.cells.unshift({x: snake.x, y: snake.y});
|
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 |
+
if (snake.x === apple.x && snake.y === apple.y) {
|
129 |
+
snake.maxCells++;
|
130 |
+
score++;
|
131 |
+
apple.x = Math.floor(Math.random() * 20) * grid;
|
132 |
+
apple.y = Math.floor(Math.random() * 20) * grid;
|
133 |
+
}
|
134 |
+
ctx.fillStyle = 'white';
|
135 |
+
ctx.font = '20px Arial';
|
136 |
+
ctx.fillText('Score: ' + score, 10, 20);
|
137 |
+
}
|
138 |
+
document.addEventListener('keydown', function(e) {
|
139 |
+
if (e.which === 37 && snake.dx === 0) { snake.dx = -grid; snake.dy = 0; }
|
140 |
+
else if (e.which === 38 && snake.dy === 0) { snake.dx = 0; snake.dy = -grid; }
|
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 |
+
]
|
150 |
|
151 |
+
for code, label in example_codes:
|
152 |
+
button = gr.Button(label)
|
153 |
+
example_buttons.append(button)
|
154 |
+
button.click(fn=lambda x=code: x, outputs=code_input)
|
155 |
|
156 |
+
return demo
|
|
|
157 |
|
158 |
+
if __name__ == "__main__":
|
159 |
+
demo = interface()
|
160 |
+
demo.launch()
|