Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,14 +10,10 @@ def run_python(code):
|
|
| 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 |
-
|
| 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:
|
| 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 |
-
|
| 62 |
""") as demo:
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
-
lang_selector = gr.Radio(
|
| 67 |
-
|
| 68 |
-
value="Web (HTML/CSS/JS)",
|
| 69 |
-
label="Select Language"
|
| 70 |
-
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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=
|
| 89 |
|
| 90 |
-
# Live update
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|