Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ def run_python(code):
|
|
| 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,7 +22,6 @@ def run_code(code, lang):
|
|
| 22 |
else:
|
| 23 |
return run_web(code)
|
| 24 |
|
| 25 |
-
# Starter templates
|
| 26 |
starter_python = """# Python example
|
| 27 |
a = 10
|
| 28 |
b = 20
|
|
@@ -33,87 +32,57 @@ starter_web = """<!DOCTYPE html>
|
|
| 33 |
<html>
|
| 34 |
<head>
|
| 35 |
<style>
|
| 36 |
-
body { font-family: 'Poppins', sans-serif; background:#
|
| 37 |
-
h1 { color:#
|
|
|
|
|
|
|
| 38 |
</style>
|
| 39 |
</head>
|
| 40 |
<body>
|
| 41 |
<h1>Hello World!</h1>
|
|
|
|
| 42 |
</body>
|
| 43 |
</html>"""
|
| 44 |
|
| 45 |
with gr.Blocks(css="""
|
| 46 |
-
body { margin:0; padding:0; font-family:'Poppins', sans-serif; background:#
|
| 47 |
-
header { background:#
|
| 48 |
.gradio-container { max-width:1200px; margin:auto; padding:20px; }
|
| 49 |
.gr-row { display:flex; gap:20px; margin-top:20px; }
|
| 50 |
-
.
|
|
|
|
| 51 |
#editor-container .CodeMirror { height:500px; border-radius:8px; }
|
|
|
|
|
|
|
|
|
|
| 52 |
""") as demo:
|
| 53 |
|
|
|
|
| 54 |
gr.Markdown("<header>Fun Code Playground – Powered by Simple & Static</header>")
|
| 55 |
|
| 56 |
-
lang_selector = gr.Radio(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
<button onclick="window.toggleTheme()" style="
|
| 61 |
-
background-color:#61dafb;
|
| 62 |
-
color:#fff;
|
| 63 |
-
padding:8px 16px;
|
| 64 |
-
border:none;
|
| 65 |
-
border-radius:6px;
|
| 66 |
-
cursor:pointer;
|
| 67 |
-
margin-bottom:10px;">
|
| 68 |
-
Toggle Light/Dark Theme
|
| 69 |
-
</button>
|
| 70 |
-
""")
|
| 71 |
-
|
| 72 |
-
# CodeMirror editor
|
| 73 |
-
code_editor = gr.HTML(
|
| 74 |
-
f"""
|
| 75 |
-
<textarea id="editor">{starter_web}</textarea>
|
| 76 |
-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/codemirror.min.css">
|
| 77 |
-
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/theme/dracula.min.css">
|
| 78 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/codemirror.min.js"></script>
|
| 79 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/mode/python/python.min.js"></script>
|
| 80 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.14/mode/htmlmixed/htmlmixed.min.js"></script>
|
| 81 |
-
<script>
|
| 82 |
-
window.editor = CodeMirror.fromTextArea(document.getElementById('editor'), {{
|
| 83 |
-
lineNumbers: true,
|
| 84 |
-
mode: "htmlmixed",
|
| 85 |
-
theme: "default",
|
| 86 |
-
}});
|
| 87 |
-
window.current_theme = "light";
|
| 88 |
-
|
| 89 |
-
window.toggleTheme = function() {{
|
| 90 |
-
if(window.current_theme === "light") {{
|
| 91 |
-
document.body.style.background = "#1e1e1e";
|
| 92 |
-
document.body.style.color = "#f0f0f0";
|
| 93 |
-
document.getElementById("editor-container").getElementsByClassName("CodeMirror")[0].CodeMirror.setOption("theme","dracula");
|
| 94 |
-
window.current_theme = "dark";
|
| 95 |
-
}} else {{
|
| 96 |
-
document.body.style.background = "#f5f6f8";
|
| 97 |
-
document.body.style.color = "#222";
|
| 98 |
-
document.getElementById("editor-container").getElementsByClassName("CodeMirror")[0].CodeMirror.setOption("theme","default");
|
| 99 |
-
window.current_theme = "light";
|
| 100 |
-
}}
|
| 101 |
-
}};
|
| 102 |
-
</script>
|
| 103 |
-
""",
|
| 104 |
-
elem_id="editor-container"
|
| 105 |
)
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
#
|
| 110 |
def update_template(lang):
|
| 111 |
-
return starter_python if lang=="Python" else starter_web
|
| 112 |
-
lang_selector.change(update_template, inputs=lang_selector, outputs=
|
| 113 |
|
| 114 |
-
# Live
|
| 115 |
-
|
| 116 |
-
return run_code(code, lang)
|
| 117 |
-
code_editor.change(live_update, inputs=[code_editor, lang_selector], outputs=output_frame)
|
| 118 |
|
| 119 |
demo.launch()
|
|
|
|
| 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; background:#1e1e1e;" 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
|
|
|
|
| 32 |
<html>
|
| 33 |
<head>
|
| 34 |
<style>
|
| 35 |
+
body { font-family: 'Poppins', sans-serif; background:#121212; color:#e0e0e0; }
|
| 36 |
+
h1 { color:#d16ba5; text-align:center; }
|
| 37 |
+
button { background-color:#d16ba5; color:#fff; padding:12px 24px; border:none; border-radius:6px; cursor:pointer;}
|
| 38 |
+
button:hover { background-color:#e27db9; }
|
| 39 |
</style>
|
| 40 |
</head>
|
| 41 |
<body>
|
| 42 |
<h1>Hello World!</h1>
|
| 43 |
+
<button onclick="alert('You clicked me!')">Click Me!</button>
|
| 44 |
</body>
|
| 45 |
</html>"""
|
| 46 |
|
| 47 |
with gr.Blocks(css="""
|
| 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:10px; overflow:auto; }
|
| 54 |
#editor-container .CodeMirror { height:500px; border-radius:8px; }
|
| 55 |
+
.gr-textbox { font-family:'Fira Code', 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; }
|
| 58 |
""") as demo:
|
| 59 |
|
| 60 |
+
# Header
|
| 61 |
gr.Markdown("<header>Fun Code Playground – Powered by Simple & Static</header>")
|
| 62 |
|
| 63 |
+
lang_selector = gr.Radio(
|
| 64 |
+
choices=["Python", "Web (HTML/CSS/JS)"],
|
| 65 |
+
value="Web (HTML/CSS/JS)",
|
| 66 |
+
label="Select Language"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Editor + Output side by side
|
| 70 |
+
with gr.Row():
|
| 71 |
+
code_input = gr.Textbox(
|
| 72 |
+
lines=20,
|
| 73 |
+
value=starter_web,
|
| 74 |
+
label="Type your code here",
|
| 75 |
+
placeholder="Start coding...",
|
| 76 |
+
interactive=True,
|
| 77 |
+
)
|
| 78 |
+
output_frame = gr.HTML(label="Output", elem_id="output-panel")
|
| 79 |
|
| 80 |
+
# Starter template updater
|
| 81 |
def update_template(lang):
|
| 82 |
+
return starter_python if lang == "Python" else starter_web
|
| 83 |
+
lang_selector.change(update_template, inputs=lang_selector, outputs=code_input)
|
| 84 |
|
| 85 |
+
# Live update
|
| 86 |
+
code_input.change(run_code, inputs=[code_input, lang_selector], outputs=output_frame)
|
|
|
|
|
|
|
| 87 |
|
| 88 |
demo.launch()
|