tiahchia commited on
Commit
b0b7ebc
·
verified ·
1 Parent(s): a10fd3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -42
app.py CHANGED
@@ -10,14 +10,10 @@ def run_python(code):
10
  except Exception as e:
11
  return str(e)
12
 
13
- # Web runner (HTML/CSS/JS)
14
  def run_web(code):
15
  safe_code = html.escape(code).replace('\n', ' ')
16
- iframe_html = (
17
- '<iframe style="width:100%; height:100%; border:none; border-radius:8px; background:#fff;" '
18
- f'srcdoc="{safe_code}"></iframe>'
19
- )
20
- return iframe_html
21
 
22
  # Main runner
23
  def run_code(code, lang):
@@ -26,68 +22,89 @@ def run_code(code, lang):
26
  else:
27
  return run_web(code)
28
 
 
 
 
 
 
 
29
  starter_web = """<!DOCTYPE html>
30
  <html>
31
  <head>
32
  <style>
33
  body { font-family: 'Poppins', sans-serif; background:#fafafa; color:#333; }
34
  h1 { color:#ff6f61; text-align:center; }
35
- button { background-color:#ff6f61; color:#fff; padding:12px 24px; border:none; border-radius:6px; cursor:pointer;}
36
- button:hover { background-color:#ff8a75; }
37
  </style>
38
  </head>
39
  <body>
40
  <h1>Hello World!</h1>
41
- <button onclick="alert('You clicked me!')">Click Me!</button>
42
  </body>
43
  </html>"""
44
 
45
- starter_python = """# Try some Python code
46
- a = 42
47
- b = 58
48
- print("Sum is:", a + b)"""
49
-
50
- # Theme toggle handler
51
- def toggle_theme(current):
52
- return "dark" if current=="light" else "light"
53
-
54
  with gr.Blocks(css="""
55
- body { margin:0; padding:0; font-family: 'Poppins', sans-serif; background: #f5f6f8; color:#222; }
56
  header { background:#20232a; padding:16px; color:#61dafb; font-size:1.5em; text-align:center; }
57
  .gradio-container { max-width:1200px; margin:auto; padding:20px; }
58
  .gr-row { display:flex; gap:20px; margin-top:20px; }
59
- .code-panel { flex:1; }
60
  .output-panel { flex:1; background:#ffffff; border-radius:8px; box-shadow:0 4px 8px rgba(0,0,0,0.1); height:500px; padding:10px; overflow:auto; }
61
- .theme-toggle { cursor:pointer; color:#61dafb; margin-bottom:10px; font-weight:bold; }
62
  """) as demo:
63
 
64
- header = gr.Markdown("<header>Fun Code Playground – Powered by Simple & Static</header>")
65
 
66
- lang_selector = gr.Radio(
67
- choices=["Python","Web (HTML/CSS/JS)"],
68
- value="Web (HTML/CSS/JS)",
69
- label="Select Language"
70
- )
71
 
72
- # Theme toggle
73
- theme_toggle = gr.Button("Switch Theme (Light/Dark)", elem_id="theme-toggle")
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
- with gr.Row():
76
- code_input = gr.Textbox(
77
- lines=20,
78
- value=starter_web,
79
- label="Type your code here",
80
- placeholder="Start coding...",
81
- interactive=True
82
- )
83
- output_frame = gr.HTML(label="Output", elem_id="output-panel")
 
 
 
 
84
 
85
- # Update starter template on language change
 
 
 
 
 
 
 
 
86
  def update_template(lang):
87
  return starter_python if lang=="Python" else starter_web
88
- lang_selector.change(update_template, inputs=lang_selector, outputs=code_input)
89
 
90
- # Live update
91
- code_input.change(run_code, inputs=[code_input, lang_selector], outputs=output_frame)
 
 
 
 
 
92
 
93
  demo.launch()
 
 
10
  except Exception as e:
11
  return str(e)
12
 
13
+ # Web runner
14
  def run_web(code):
15
  safe_code = html.escape(code).replace('\n', ' ')
16
+ return f'<iframe style="width:100%; height:100%; border:none; border-radius:8px;" srcdoc="{safe_code}"></iframe>'
 
 
 
 
17
 
18
  # Main runner
19
  def run_code(code, lang):
 
22
  else:
23
  return run_web(code)
24
 
25
+ starter_python = """# Python example
26
+ a = 10
27
+ b = 20
28
+ sum = a + b
29
+ sum"""
30
+
31
  starter_web = """<!DOCTYPE html>
32
  <html>
33
  <head>
34
  <style>
35
  body { font-family: 'Poppins', sans-serif; background:#fafafa; color:#333; }
36
  h1 { color:#ff6f61; text-align:center; }
 
 
37
  </style>
38
  </head>
39
  <body>
40
  <h1>Hello World!</h1>
 
41
  </body>
42
  </html>"""
43
 
 
 
 
 
 
 
 
 
 
44
  with gr.Blocks(css="""
45
+ body { margin:0; padding:0; font-family:'Poppins', sans-serif; background:#f5f6f8; color:#222; }
46
  header { background:#20232a; padding:16px; color:#61dafb; font-size:1.5em; text-align:center; }
47
  .gradio-container { max-width:1200px; margin:auto; padding:20px; }
48
  .gr-row { display:flex; gap:20px; margin-top:20px; }
 
49
  .output-panel { flex:1; background:#ffffff; border-radius:8px; box-shadow:0 4px 8px rgba(0,0,0,0.1); height:500px; padding:10px; overflow:auto; }
50
+ #editor-container .CodeMirror { height:500px; border-radius:8px; }
51
  """) as demo:
52
 
53
+ gr.Markdown("<header>Fun Code Playground – Powered by Simple & Static</header>")
54
 
55
+ lang_selector = gr.Radio(["Python","Web (HTML/CSS/JS)"], value="Web (HTML/CSS/JS)", label="Select Language")
56
+ theme_toggle = gr.Button("Toggle Light/Dark Theme")
 
 
 
57
 
58
+ code_editor = gr.HTML(
59
+ f"""
60
+ <textarea id="editor">{starter_web}</textarea>
61
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/codemirror.min.css">
62
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/theme/dracula.min.css">
63
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/codemirror.min.js"></script>
64
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/mode/python/python.min.js"></script>
65
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/mode/htmlmixed/htmlmixed.min.js"></script>
66
+ <script>
67
+ window.editor = CodeMirror.fromTextArea(document.getElementById('editor'), {{
68
+ lineNumbers: true,
69
+ mode: "htmlmixed",
70
+ theme: "default",
71
+ }});
72
+ window.current_theme = "light";
73
 
74
+ function toggleTheme() {{
75
+ if(window.current_theme === "light") {{
76
+ document.body.style.background = "#1e1e1e";
77
+ document.body.style.color = "#f0f0f0";
78
+ document.getElementById("editor-container").getElementsByClassName("CodeMirror")[0].CodeMirror.setOption("theme","dracula");
79
+ window.current_theme = "dark";
80
+ }} else {{
81
+ document.body.style.background = "#f5f6f8";
82
+ document.body.style.color = "#222";
83
+ document.getElementById("editor-container").getElementsByClassName("CodeMirror")[0].CodeMirror.setOption("theme","default");
84
+ window.current_theme = "light";
85
+ }}
86
+ }}
87
 
88
+ window.toggleTheme = toggleTheme;
89
+ </script>
90
+ """,
91
+ elem_id="editor-container"
92
+ )
93
+
94
+ output_frame = gr.HTML(label="Output", elem_id="output-panel")
95
+
96
+ # Update editor template based on language
97
  def update_template(lang):
98
  return starter_python if lang=="Python" else starter_web
99
+ lang_selector.change(update_template, inputs=lang_selector, outputs=code_editor)
100
 
101
+ # Live output update
102
+ def live_update(code, lang):
103
+ return run_code(code, lang)
104
+ code_editor.change(live_update, inputs=[code_editor, lang_selector], outputs=output_frame)
105
+
106
+ # Connect theme toggle button
107
+ theme_toggle.click(None, [], [], _js="window.toggleTheme()")
108
 
109
  demo.launch()
110
+