tiahchia commited on
Commit
cc730d7
·
verified ·
1 Parent(s): 2e59aad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -36
app.py CHANGED
@@ -1,40 +1,36 @@
1
  import gradio as gr
2
 
3
- # Python runner
4
  def run_python(code):
5
  try:
6
  local_vars = {}
7
  exec(code, {}, local_vars)
8
- # Convert output dict to readable string
9
  return "\n".join(f"{k} = {v}" for k, v in local_vars.items())
10
  except Exception as e:
11
  return str(e)
12
 
13
- # Web runner (HTML/CSS/JS)
14
  def run_web(code):
15
  safe_code = code.replace('"', '"').replace('\n', ' ')
16
- return (
17
- '<iframe style="width:100%; height:400px; border:1px solid #ccc; border-radius:10px; background:white;" '
18
- 'srcdoc="' + safe_code + '">'
19
- '</iframe>'
20
  )
 
21
 
22
- # Main runner that switches based on language
23
  def run_code(code, lang):
24
  if lang == "Python":
25
  return run_python(code)
26
  else:
27
  return run_web(code)
28
 
29
- # Starter templates
30
  starter_web = """<!DOCTYPE html>
31
  <html>
32
  <head>
33
  <style>
34
- body { font-family: 'Poppins', sans-serif; background-color: #f0f8ff; color: #333; }
35
- h1 { color: #ff6f61; text-align:center; }
36
- button { background-color: #ff6f61; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; }
37
- button:hover { background-color: #ff8a75; }
38
  </style>
39
  </head>
40
  <body>
@@ -44,39 +40,40 @@ button:hover { background-color: #ff8a75; }
44
  </html>"""
45
 
46
  starter_python = """# Try some Python code
47
- a = 5
48
- b = 10
49
- c = a + b
50
- c"""
51
 
52
- # Gradio interface
53
  with gr.Blocks(css="""
54
- body { background: linear-gradient(to right, #ffe8d6, #ffd6d6); font-family: 'Poppins', sans-serif; }
55
- .gr-row { gap: 20px; }
56
- .gr-textbox { border-radius: 10px; font-family: monospace; }
 
 
 
 
 
 
57
  """) as demo:
58
 
59
- gr.Markdown("## 🎨 Fun Code Playground - Python + Web Live Preview")
60
 
61
- # Language selector
62
- lang_selector = gr.Radio(choices=["Python", "Web (HTML/CSS/JS)"], value="Web (HTML/CSS/JS)", label="Select Language")
63
 
64
  with gr.Row():
65
- code_input = gr.Textbox(
66
- lines=20,
67
- value=starter_web,
68
- label="Type your code here",
69
- placeholder="Start coding...",
70
- interactive=True
71
- )
72
- output_frame = gr.HTML(label="Output")
73
 
74
- # Update starter template when language changes
75
  def update_template(lang):
76
- return starter_python if lang=="Python" else starter_web
77
- lang_selector.change(update_template, inputs=lang_selector, outputs=code_input)
78
 
79
- # Live update on code change
80
  code_input.change(run_code, inputs=[code_input, lang_selector], outputs=output_frame)
81
 
82
  demo.launch()
 
1
  import gradio as gr
2
 
 
3
  def run_python(code):
4
  try:
5
  local_vars = {}
6
  exec(code, {}, local_vars)
 
7
  return "\n".join(f"{k} = {v}" for k, v in local_vars.items())
8
  except Exception as e:
9
  return str(e)
10
 
 
11
  def run_web(code):
12
  safe_code = code.replace('"', '&quot;').replace('\n', ' ')
13
+ iframe_html = (
14
+ '<iframe '
15
+ 'style="width:100%; height:100%; border:none; border-radius:8px; background:#fff;" '
16
+ 'srcdoc="' + safe_code + '"></iframe>'
17
  )
18
+ return iframe_html
19
 
 
20
  def run_code(code, lang):
21
  if lang == "Python":
22
  return run_python(code)
23
  else:
24
  return run_web(code)
25
 
 
26
  starter_web = """<!DOCTYPE html>
27
  <html>
28
  <head>
29
  <style>
30
+ body { font-family: 'Poppins', sans-serif; margin:0; padding:20px; background:#fafafa; color:#333; }
31
+ h1 { color:#ff6f61; text-align:center; }
32
+ button { background-color:#ff6f61; color:#fff; padding:12px 24px; border:none; border-radius:6px; cursor:pointer; transition: background 0.3s;}
33
+ button:hover { background-color:#ff8a75; }
34
  </style>
35
  </head>
36
  <body>
 
40
  </html>"""
41
 
42
  starter_python = """# Try some Python code
43
+ a = 42
44
+ b = 58
45
+ print("Sum is:", a + b)"""
 
46
 
 
47
  with gr.Blocks(css="""
48
+ body { margin: 0; padding: 0; font-family: 'Poppins', sans-serif; background: #f5f6f8; color: #222; }
49
+ header { background: #20232a; padding: 16px; color: #61dafb; font-size: 1.5em; text-align: center; }
50
+ .gradio-container { max-width: 1200px; margin: auto; padding: 20px; }
51
+ .gr-row { display: flex; gap: 20px; margin-top: 20px; }
52
+ .code-panel { flex: 1; }
53
+ .output-panel { flex: 1; background: #ffffff; border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); height: 500px; }
54
+ .gr-textbox { font-family: 'Fira Code', monospace; background: #282c34; color: #abb2bf; border: none; border-radius: 8px; padding: 16px; height: 500px; overflow: auto; }
55
+ .gr-radio { margin-top: 16px; }
56
+ .theme-toggle { position: absolute; top: 16px; right: 16px; cursor: pointer; color: #61dafb; }
57
  """) as demo:
58
 
59
+ header = gr.Markdown("<header>Fun Code Playground Powered by Simple & Static</header>")
60
 
61
+ lang_selector = gr.Radio(choices=["Python", "Web (HTML/CSS/JS)"],
62
+ value="Web (HTML/CSS/JS)", label="Select Language", css=".gr-radio { color: #222; }")
63
 
64
  with gr.Row():
65
+ code_input = gr.Textbox(lines=20,
66
+ value=starter_web,
67
+ label="Type your code here",
68
+ placeholder="Start coding...",
69
+ interactive=True,
70
+ css=".gr-textbox { }")
71
+ output_frame = gr.HTML(label="Output", elem_id="output-panel", css=".output-panel { }")
 
72
 
 
73
  def update_template(lang):
74
+ return starter_python if lang == "Python" else starter_web
 
75
 
76
+ lang_selector.change(update_template, inputs=lang_selector, outputs=code_input)
77
  code_input.change(run_code, inputs=[code_input, lang_selector], outputs=output_frame)
78
 
79
  demo.launch()