Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
|
|
|
| 3 |
def run_python(code):
|
| 4 |
try:
|
| 5 |
local_vars = {}
|
|
@@ -8,15 +10,16 @@ def run_python(code):
|
|
| 8 |
except Exception as e:
|
| 9 |
return str(e)
|
| 10 |
|
|
|
|
| 11 |
def run_web(code):
|
| 12 |
-
safe_code =
|
| 13 |
iframe_html = (
|
| 14 |
-
'<iframe '
|
| 15 |
-
'
|
| 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)
|
|
@@ -27,9 +30,9 @@ starter_web = """<!DOCTYPE html>
|
|
| 27 |
<html>
|
| 28 |
<head>
|
| 29 |
<style>
|
| 30 |
-
body { font-family: 'Poppins', sans-serif;
|
| 31 |
h1 { color:#ff6f61; text-align:center; }
|
| 32 |
-
button { background-color:#ff6f61; color:#fff; padding:12px 24px; border:none; border-radius:6px; cursor:pointer;
|
| 33 |
button:hover { background-color:#ff8a75; }
|
| 34 |
</style>
|
| 35 |
</head>
|
|
@@ -44,36 +47,47 @@ a = 42
|
|
| 44 |
b = 58
|
| 45 |
print("Sum is:", a + b)"""
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Blocks(css="""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 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(
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
-
code_input = gr.Textbox(
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
|
|
|
| 73 |
def update_template(lang):
|
| 74 |
-
return starter_python if lang
|
| 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()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import html
|
| 3 |
|
| 4 |
+
# Python runner
|
| 5 |
def run_python(code):
|
| 6 |
try:
|
| 7 |
local_vars = {}
|
|
|
|
| 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):
|
| 24 |
if lang == "Python":
|
| 25 |
return run_python(code)
|
|
|
|
| 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>
|
|
|
|
| 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()
|