Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import html
|
|
| 3 |
import io
|
| 4 |
from contextlib import redirect_stdout
|
| 5 |
|
| 6 |
-
# Python runner
|
| 7 |
def run_python(code):
|
| 8 |
f = io.StringIO()
|
| 9 |
try:
|
|
@@ -14,17 +14,18 @@ def run_python(code):
|
|
| 14 |
output = f.getvalue()
|
| 15 |
return f'<pre style="color:#00ff00; font-family: monospace;">{html.escape(output)}</pre>'
|
| 16 |
|
| 17 |
-
# Web runner
|
| 18 |
def run_web(code):
|
| 19 |
return f'<iframe style="width:100%; height:100%; border:none; border-radius:8px; background:#1e1e1e;" srcdoc="{code}"></iframe>'
|
| 20 |
|
| 21 |
-
# Main runner
|
| 22 |
def run_code(code, lang):
|
| 23 |
if lang == "Python":
|
| 24 |
return run_python(code)
|
| 25 |
else:
|
| 26 |
return run_web(code)
|
| 27 |
|
|
|
|
| 28 |
starter_python = """# Python example
|
| 29 |
print("Hello World!")
|
| 30 |
for i in range(5):
|
|
@@ -43,123 +44,90 @@ h1 { color:#F714C5; text-align:center; }
|
|
| 43 |
</body>
|
| 44 |
</html>"""
|
| 45 |
|
|
|
|
| 46 |
with gr.Blocks(css="""
|
| 47 |
-
/* Dark navy background with Canvas particles */
|
| 48 |
body {
|
| 49 |
margin:0; padding:0;
|
| 50 |
font-family:'Poppins', sans-serif;
|
| 51 |
overflow-x:hidden;
|
| 52 |
color:#e0e0e0;
|
| 53 |
background: linear-gradient(to bottom, #0a0a23, #00001a);
|
| 54 |
-
position: relative;
|
| 55 |
}
|
| 56 |
-
.gradio-container {
|
| 57 |
-
|
| 58 |
-
margin:auto;
|
| 59 |
-
padding:20px;
|
| 60 |
-
position: relative;
|
| 61 |
-
z-index:1;
|
| 62 |
-
}
|
| 63 |
-
.gr-column {
|
| 64 |
-
display:flex;
|
| 65 |
-
flex-direction: column;
|
| 66 |
-
gap:5px;
|
| 67 |
-
}
|
| 68 |
-
/* Full width editor/output */
|
| 69 |
#editor-container, #output-panel {
|
| 70 |
width:100% !important;
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
height:600px;
|
| 80 |
-
padding:16px;
|
| 81 |
-
font-family: monospace;
|
| 82 |
-
color: #00ff00;
|
| 83 |
-
white-space: pre-wrap;
|
| 84 |
overflow:auto;
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
#editor-container .CodeMirror {
|
| 87 |
-
height:
|
| 88 |
-
min-height:
|
| 89 |
-
max-height:600px !important;
|
| 90 |
-
padding:12px !important;
|
| 91 |
-
box-sizing:border-box;
|
| 92 |
-
border-radius:8px;
|
| 93 |
font-family: monospace;
|
| 94 |
font-size:14px;
|
| 95 |
white-space: pre-wrap;
|
| 96 |
overflow-wrap: break-word;
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
-
.gr-radio { color:#e0e0e0; margin-bottom:
|
| 99 |
-
button, .gr-button { background-color:#F714C5 !important; color:#fff !important; border-radius:6px !important; padding:
|
| 100 |
footer { display:none !important; }
|
| 101 |
-
header { background:#000000; padding:16px; color:#F714C5; font-size:1.
|
| 102 |
header img { vertical-align:middle; height:20px; margin-right:8px; }
|
| 103 |
canvas#particles { position:fixed; top:0; left:0; width:100%; height:100%; z-index:0; pointer-events:none; }
|
| 104 |
""") as demo:
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
gr.HTML("""
|
| 108 |
-
<canvas id="particles"></canvas>
|
| 109 |
<script>
|
| 110 |
const canvas = document.getElementById('particles');
|
| 111 |
const ctx = canvas.getContext('2d');
|
| 112 |
let stars = [];
|
| 113 |
-
function resize()
|
| 114 |
-
|
| 115 |
-
canvas.height = window.innerHeight;
|
| 116 |
-
}
|
| 117 |
-
window.addEventListener('resize', resize);
|
| 118 |
-
resize();
|
| 119 |
for(let i=0;i<150;i++){
|
| 120 |
-
stars.push({
|
| 121 |
-
|
| 122 |
-
y: Math.random()*canvas.height,
|
| 123 |
-
r: Math.random()*1.5,
|
| 124 |
-
dx: (Math.random()-0.5)*0.3,
|
| 125 |
-
dy: (Math.random()-0.5)*0.3
|
| 126 |
-
});
|
| 127 |
}
|
| 128 |
function animate(){
|
| 129 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 130 |
for(let s of stars){
|
| 131 |
-
ctx.beginPath();
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
s.x += s.dx;
|
| 136 |
-
s.y += s.dy;
|
| 137 |
-
if(s.x<0) s.x=canvas.width;
|
| 138 |
-
if(s.x>canvas.width) s.x=0;
|
| 139 |
-
if(s.y<0) s.y=canvas.height;
|
| 140 |
-
if(s.y>canvas.height) s.y=0;
|
| 141 |
}
|
| 142 |
requestAnimationFrame(animate);
|
| 143 |
}
|
| 144 |
animate();
|
| 145 |
-
</script>
|
| 146 |
-
""")
|
| 147 |
|
| 148 |
-
#
|
| 149 |
gr.Markdown("""
|
| 150 |
<h2 style="color:#F714C5; text-align:center; margin-bottom:10px;">
|
| 151 |
πΈ'π ππππ & ππππ ππ ππ’ ππππππππ, πππππ’ ππππππ!
|
| 152 |
</h2>
|
| 153 |
""")
|
| 154 |
|
| 155 |
-
# Language selector
|
| 156 |
lang_selector = gr.Radio(
|
| 157 |
choices=["Python", "Web (HTML/CSS/JS)"],
|
| 158 |
value="Web (HTML/CSS/JS)",
|
| 159 |
label="Select Language"
|
| 160 |
)
|
| 161 |
|
| 162 |
-
# Vertical layout:
|
| 163 |
with gr.Column():
|
| 164 |
output_frame = gr.HTML(label="Output", elem_id="output-panel")
|
| 165 |
code_input = gr.Code(
|
|
@@ -167,13 +135,13 @@ canvas#particles { position:fixed; top:0; left:0; width:100%; height:100%; z-ind
|
|
| 167 |
language="html",
|
| 168 |
label="Type your code here",
|
| 169 |
interactive=True,
|
| 170 |
-
lines=
|
| 171 |
)
|
| 172 |
|
| 173 |
-
# Render button
|
| 174 |
render_button = gr.Button("Render")
|
| 175 |
|
| 176 |
-
# Footer
|
| 177 |
gr.Markdown("""
|
| 178 |
<header>
|
| 179 |
<img src="https://simpleandstatic.com/favicon.ico" alt="Logo" />
|
|
@@ -181,12 +149,12 @@ canvas#particles { position:fixed; top:0; left:0; width:100%; height:100%; z-ind
|
|
| 181 |
</header>
|
| 182 |
""")
|
| 183 |
|
| 184 |
-
# Update starter template on language change
|
| 185 |
def update_template(lang):
|
| 186 |
return starter_python if lang == "Python" else starter_web
|
| 187 |
lang_selector.change(update_template, inputs=lang_selector, outputs=code_input)
|
| 188 |
|
| 189 |
-
# Run code
|
| 190 |
render_button.click(run_code, inputs=[code_input, lang_selector], outputs=output_frame)
|
| 191 |
|
| 192 |
demo.launch()
|
|
|
|
| 3 |
import io
|
| 4 |
from contextlib import redirect_stdout
|
| 5 |
|
| 6 |
+
# --- Python runner ---
|
| 7 |
def run_python(code):
|
| 8 |
f = io.StringIO()
|
| 9 |
try:
|
|
|
|
| 14 |
output = f.getvalue()
|
| 15 |
return f'<pre style="color:#00ff00; font-family: monospace;">{html.escape(output)}</pre>'
|
| 16 |
|
| 17 |
+
# --- Web runner ---
|
| 18 |
def run_web(code):
|
| 19 |
return f'<iframe style="width:100%; height:100%; border:none; border-radius:8px; background:#1e1e1e;" srcdoc="{code}"></iframe>'
|
| 20 |
|
| 21 |
+
# --- Main runner ---
|
| 22 |
def run_code(code, lang):
|
| 23 |
if lang == "Python":
|
| 24 |
return run_python(code)
|
| 25 |
else:
|
| 26 |
return run_web(code)
|
| 27 |
|
| 28 |
+
# --- Starter templates ---
|
| 29 |
starter_python = """# Python example
|
| 30 |
print("Hello World!")
|
| 31 |
for i in range(5):
|
|
|
|
| 44 |
</body>
|
| 45 |
</html>"""
|
| 46 |
|
| 47 |
+
# --- Gradio app ---
|
| 48 |
with gr.Blocks(css="""
|
|
|
|
| 49 |
body {
|
| 50 |
margin:0; padding:0;
|
| 51 |
font-family:'Poppins', sans-serif;
|
| 52 |
overflow-x:hidden;
|
| 53 |
color:#e0e0e0;
|
| 54 |
background: linear-gradient(to bottom, #0a0a23, #00001a);
|
|
|
|
| 55 |
}
|
| 56 |
+
.gradio-container { max-width:1200px; margin:auto; padding:20px; }
|
| 57 |
+
.gr-column { display:flex; flex-direction: column; gap:0px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
#editor-container, #output-panel {
|
| 59 |
width:100% !important;
|
| 60 |
+
margin:0 !important;
|
| 61 |
+
padding:8px !important;
|
| 62 |
+
border-radius:8px;
|
| 63 |
+
box-sizing:border-box;
|
| 64 |
}
|
| 65 |
+
#output-panel {
|
| 66 |
+
height:250px !important;
|
| 67 |
+
background:#1e1e1e;
|
| 68 |
+
color:#00ff00;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
overflow:auto;
|
| 70 |
+
box-shadow:0 4px 8px rgba(0,0,0,0.5);
|
| 71 |
+
font-family: monospace;
|
| 72 |
}
|
| 73 |
#editor-container .CodeMirror {
|
| 74 |
+
height:300px !important;
|
| 75 |
+
min-height:300px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
font-family: monospace;
|
| 77 |
font-size:14px;
|
| 78 |
white-space: pre-wrap;
|
| 79 |
overflow-wrap: break-word;
|
| 80 |
+
border-radius:8px;
|
| 81 |
+
padding:8px !important;
|
| 82 |
}
|
| 83 |
+
.gr-radio { color:#e0e0e0; margin-bottom:12px; }
|
| 84 |
+
button, .gr-button { background-color:#F714C5 !important; color:#fff !important; border-radius:6px !important; padding:8px 16px !important; }
|
| 85 |
footer { display:none !important; }
|
| 86 |
+
header { background:#000000; padding:16px; color:#F714C5; font-size:1.4em; text-align:center; margin-top:20px; }
|
| 87 |
header img { vertical-align:middle; height:20px; margin-right:8px; }
|
| 88 |
canvas#particles { position:fixed; top:0; left:0; width:100%; height:100%; z-index:0; pointer-events:none; }
|
| 89 |
""") as demo:
|
| 90 |
|
| 91 |
+
# --- Particle background ---
|
| 92 |
+
gr.HTML("""<canvas id="particles"></canvas>
|
|
|
|
| 93 |
<script>
|
| 94 |
const canvas = document.getElementById('particles');
|
| 95 |
const ctx = canvas.getContext('2d');
|
| 96 |
let stars = [];
|
| 97 |
+
function resize(){ canvas.width = window.innerWidth; canvas.height = window.innerHeight; }
|
| 98 |
+
window.addEventListener('resize', resize); resize();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
for(let i=0;i<150;i++){
|
| 100 |
+
stars.push({ x: Math.random()*canvas.width, y: Math.random()*canvas.height,
|
| 101 |
+
r: Math.random()*1.5, dx: (Math.random()-0.5)*0.3, dy: (Math.random()-0.5)*0.3 });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
function animate(){
|
| 104 |
ctx.clearRect(0,0,canvas.width,canvas.height);
|
| 105 |
for(let s of stars){
|
| 106 |
+
ctx.beginPath(); ctx.arc(s.x,s.y,s.r,0,Math.PI*2); ctx.fillStyle='white'; ctx.fill();
|
| 107 |
+
s.x+=s.dx; s.y+=s.dy;
|
| 108 |
+
if(s.x<0) s.x=canvas.width; if(s.x>canvas.width) s.x=0;
|
| 109 |
+
if(s.y<0) s.y=canvas.height; if(s.y>canvas.height) s.y=0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
requestAnimationFrame(animate);
|
| 112 |
}
|
| 113 |
animate();
|
| 114 |
+
</script>""")
|
|
|
|
| 115 |
|
| 116 |
+
# --- Header ---
|
| 117 |
gr.Markdown("""
|
| 118 |
<h2 style="color:#F714C5; text-align:center; margin-bottom:10px;">
|
| 119 |
πΈ'π ππππ & ππππ ππ ππ’ ππππππππ, πππππ’ ππππππ!
|
| 120 |
</h2>
|
| 121 |
""")
|
| 122 |
|
| 123 |
+
# --- Language selector ---
|
| 124 |
lang_selector = gr.Radio(
|
| 125 |
choices=["Python", "Web (HTML/CSS/JS)"],
|
| 126 |
value="Web (HTML/CSS/JS)",
|
| 127 |
label="Select Language"
|
| 128 |
)
|
| 129 |
|
| 130 |
+
# --- Vertical layout: output on top ---
|
| 131 |
with gr.Column():
|
| 132 |
output_frame = gr.HTML(label="Output", elem_id="output-panel")
|
| 133 |
code_input = gr.Code(
|
|
|
|
| 135 |
language="html",
|
| 136 |
label="Type your code here",
|
| 137 |
interactive=True,
|
| 138 |
+
lines=15
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# --- Render button ---
|
| 142 |
render_button = gr.Button("Render")
|
| 143 |
|
| 144 |
+
# --- Footer ---
|
| 145 |
gr.Markdown("""
|
| 146 |
<header>
|
| 147 |
<img src="https://simpleandstatic.com/favicon.ico" alt="Logo" />
|
|
|
|
| 149 |
</header>
|
| 150 |
""")
|
| 151 |
|
| 152 |
+
# --- Update starter template on language change ---
|
| 153 |
def update_template(lang):
|
| 154 |
return starter_python if lang == "Python" else starter_web
|
| 155 |
lang_selector.change(update_template, inputs=lang_selector, outputs=code_input)
|
| 156 |
|
| 157 |
+
# --- Run code ---
|
| 158 |
render_button.click(run_code, inputs=[code_input, lang_selector], outputs=output_frame)
|
| 159 |
|
| 160 |
demo.launch()
|