broadfield-dev commited on
Commit
09b1f7a
·
verified ·
1 Parent(s): df7cc34

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -80
app.py CHANGED
@@ -2,7 +2,7 @@ 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}
@@ -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 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
@@ -82,68 +59,45 @@ def interface():
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
  ]
 
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
  ]