broadfield-dev commited on
Commit
095df03
·
verified ·
1 Parent(s): e6d5c9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -20
app.py CHANGED
@@ -1,42 +1,74 @@
1
  import gradio as gr
 
2
 
3
  def update_ui(code):
4
  try:
5
- # Here, we're not parsing JavaScript out but passing the whole code through
6
- return code, ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  except Exception as e:
8
- return "", str(e)
9
 
10
  def interface():
11
  with gr.Blocks() as demo:
12
- gr.Markdown("# Learn Frontend UI with Snake Game")
13
  with gr.Row():
14
  code_input = gr.Code(label="Write your HTML/CSS/JS here", language="html")
15
  with gr.Column():
16
- # For JavaScript to work, you need to use the _js argument in the HTML component
17
  ui_preview = gr.HTML(label="UI Preview")
18
  error_output = gr.Textbox(label="Error", interactive=False)
 
 
19
 
20
- # We'll use the _js argument to inject JavaScript directly into the HTML component
21
  code_input.change(
22
- fn=update_ui,
23
- inputs=code_input,
24
- outputs=[ui_preview, error_output],
25
  _js="""
26
- function(code) {
27
- // This function runs in the browser context, not Python
28
  const tempDiv = document.createElement('div');
29
- tempDiv.innerHTML = code;
30
-
31
- // Extract scripts from the code
32
- const scripts = tempDiv.querySelectorAll('script');
33
- for (let script of scripts) {
34
- const newScript = document.createElement('script');
35
- newScript.textContent = script.textContent;
36
- script.parentNode.replaceChild(newScript, script);
 
37
  }
38
 
39
- // Return the HTML content without the script tags for Gradio to display
40
  return tempDiv.innerHTML;
41
  }
42
  """
 
1
  import gradio as gr
2
+ import re
3
 
4
  def update_ui(code):
5
  try:
6
+ # Extract HTML, CSS, and JavaScript using regex
7
+ html_content = re.search(r'<html[^>]*>[\s\S]*?<\/html>', code, re.IGNORECASE)
8
+ if html_content:
9
+ html_content = html_content.group(0)
10
+ else:
11
+ html_content = code # If no <html> tag, assume all is HTML content
12
+
13
+ # Extract CSS if present in a <style> tag
14
+ css_content = re.search(r'<style[^>]*>[\s\S]*?<\/style>', code, re.IGNORECASE)
15
+ css_string = ''
16
+ if css_content:
17
+ css_string = css_content.group(0)
18
+
19
+ # Remove <style> tags for direct application
20
+ css_only = re.sub(r'<style>|<\/style>', '', css_string) if css_string else ''
21
+
22
+ # Extract JavaScript if present in a <script> tag
23
+ js_content = re.search(r'<script[^>]*>[\s\S]*?<\/script>', code, re.IGNORECASE)
24
+ js_string = ''
25
+ if js_content:
26
+ js_string = js_content.group(0)
27
+
28
+ # Remove <script> tags for Gradio's js= parameter
29
+ js_only = re.sub(r'<script>|<\/script>', '', js_string) if js_string else ''
30
+
31
+ # Combine HTML with CSS for preview, but we'll handle JS separately
32
+ ui_output = f"""
33
+ <style>{css_only}</style>
34
+ {html_content}
35
+ """
36
+ return ui_output, js_only, None
37
  except Exception as e:
38
+ return "", "", str(e)
39
 
40
  def interface():
41
  with gr.Blocks() as demo:
42
+ gr.Markdown("# Learn Frontend UI")
43
  with gr.Row():
44
  code_input = gr.Code(label="Write your HTML/CSS/JS here", language="html")
45
  with gr.Column():
 
46
  ui_preview = gr.HTML(label="UI Preview")
47
  error_output = gr.Textbox(label="Error", interactive=False)
48
+ # Hidden output to store JavaScript for _js execution
49
+ js_output = gr.Textbox(visible=False)
50
 
51
+ # Use _js to execute JavaScript in the browser context
52
  code_input.change(
53
+ fn=update_ui,
54
+ inputs=code_input,
55
+ outputs=[ui_preview, js_output, error_output],
56
  _js="""
57
+ function(code, ui_output, js_code) {
58
+ // Create a temporary div to parse the HTML
59
  const tempDiv = document.createElement('div');
60
+ tempDiv.innerHTML = ui_output;
61
+
62
+ // If there's JavaScript code, execute it
63
+ if (js_code) {
64
+ const script = document.createElement('script');
65
+ script.textContent = js_code;
66
+ document.body.appendChild(script);
67
+ // Clean up script tag after execution to prevent duplicates
68
+ setTimeout(() => script.remove(), 0);
69
  }
70
 
71
+ // Return the HTML content for Gradio to display
72
  return tempDiv.innerHTML;
73
  }
74
  """