tiahchia commited on
Commit
b004a26
·
verified ·
1 Parent(s): 07f8bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -1,20 +1,23 @@
1
  import gradio as gr
2
  import html
 
 
3
 
4
- # Python runner
5
  def run_python(code):
 
6
  try:
7
- local_vars = {}
8
- exec(code, {}, local_vars)
9
- # Capture all print output if any
10
- return "\n".join(f"{k} = {v}" for k, v in local_vars.items())
11
  except Exception as e:
12
- return str(e)
 
 
13
 
14
  # Web runner (HTML/CSS/JS)
15
  def run_web(code):
16
- safe_code = html.escape(code).replace('\n', '<br>')
17
- return f'<div style="font-family: monospace; white-space: pre-wrap;">{safe_code}</div>'
18
 
19
  # Main runner
20
  def run_code(code, lang):
@@ -25,17 +28,16 @@ def run_code(code, lang):
25
 
26
  # Starter templates
27
  starter_python = """# Python example
28
- a = 10
29
- b = 20
30
- sum = a + b
31
- sum"""
32
 
33
  starter_web = """<!DOCTYPE html>
34
  <html>
35
  <head>
36
  <style>
37
- body { font-family: 'Poppins', sans-serif; background:#1e1e1e; color:#e0e0e0; }
38
- h1 { color:#d16ba5; text-align:center; }
39
  </style>
40
  </head>
41
  <body>
@@ -46,14 +48,13 @@ h1 { color:#d16ba5; text-align:center; }
46
  with gr.Blocks(css="""
47
  /* Global Dark Theme */
48
  body { margin:0; padding:0; font-family:'Poppins', sans-serif; background:#121212; color:#e0e0e0; }
49
- header { background:#000000; padding:16px; color:#d16ba5; font-size:1.8em; 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:#1e1e1e; border-radius:8px; box-shadow:0 4px 8px rgba(0,0,0,0.5); height:500px; padding:16px; font-family: monospace; color: #00ff00; white-space: pre-wrap; overflow:auto; }
54
- #editor-container .CodeMirror { height:500px; border-radius:8px; }
55
  .gr-radio { color:#e0e0e0; margin-top:16px; }
56
- button, .gr-button { background-color:#d16ba5 !important; color:#fff !important; border-radius:6px !important; padding:10px 20px !important; }
57
  footer { display:none !important; } /* Hide Gradio footer */
58
  """) as demo:
59
 
 
1
  import gradio as gr
2
  import html
3
+ import io
4
+ from contextlib import redirect_stdout
5
 
6
+ # Python runner with real terminal output
7
  def run_python(code):
8
+ f = io.StringIO()
9
  try:
10
+ with redirect_stdout(f):
11
+ exec(code, {}, {})
 
 
12
  except Exception as e:
13
+ f.write(str(e))
14
+ output = f.getvalue()
15
+ return f'<pre style="color:#00ff00; font-family: monospace;">{html.escape(output)}</pre>'
16
 
17
  # Web runner (HTML/CSS/JS)
18
  def run_web(code):
19
+ # Display in iframe so code is rendered properly
20
+ return f'<iframe style="width:100%; height:100%; border:none; border-radius:8px; background:#1e1e1e;" srcdoc="{code}"></iframe>'
21
 
22
  # Main runner
23
  def run_code(code, lang):
 
28
 
29
  # Starter templates
30
  starter_python = """# Python example
31
+ print("Hello from Python!")
32
+ for i in range(5):
33
+ print(f"Line {i+1}")"""
 
34
 
35
  starter_web = """<!DOCTYPE html>
36
  <html>
37
  <head>
38
  <style>
39
+ body { font-family: monospace; background:#1e1e1e; color:#00ff00; }
40
+ h1 { color:#D16BA5; text-align:center; }
41
  </style>
42
  </head>
43
  <body>
 
48
  with gr.Blocks(css="""
49
  /* Global Dark Theme */
50
  body { margin:0; padding:0; font-family:'Poppins', sans-serif; background:#121212; color:#e0e0e0; }
51
+ header { background:#000000; padding:16px; color:#D16BA5; font-size:1.8em; text-align:center; }
52
  .gradio-container { max-width:1200px; margin:auto; padding:20px; }
53
  .gr-row { display:flex; gap:20px; margin-top:20px; }
 
54
  .output-panel { flex:1; background:#1e1e1e; border-radius:8px; box-shadow:0 4px 8px rgba(0,0,0,0.5); height:500px; padding:16px; font-family: monospace; color: #00ff00; white-space: pre-wrap; overflow:auto; }
55
+ .gr-textbox textarea { font-family: monospace; background:#1e1e1e; color:#e0e0e0; border:none; border-radius:8px; padding:16px; height:500px; overflow:auto; }
56
  .gr-radio { color:#e0e0e0; margin-top:16px; }
57
+ button, .gr-button { background-color:#D16BA5 !important; color:#fff !important; border-radius:6px !important; padding:10px 20px !important; }
58
  footer { display:none !important; } /* Hide Gradio footer */
59
  """) as demo:
60