Spaces:
Sleeping
Sleeping
Memex
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import io
|
| 3 |
+
import contextlib
|
| 4 |
+
import traceback
|
| 5 |
+
|
| 6 |
+
def run_python_code(code):
|
| 7 |
+
output = io.StringIO()
|
| 8 |
+
try:
|
| 9 |
+
with contextlib.redirect_stdout(output):
|
| 10 |
+
exec(code, {}) # Run in isolated global scope
|
| 11 |
+
return output.getvalue()
|
| 12 |
+
except Exception:
|
| 13 |
+
return traceback.format_exc()
|
| 14 |
+
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
gr.Markdown("## π Python Code Runner (Gradio)")
|
| 17 |
+
|
| 18 |
+
code_input = gr.Code(language="python", label="Write Python Code")
|
| 19 |
+
output_box = gr.Textbox(label="Output", lines=10)
|
| 20 |
+
|
| 21 |
+
run_button = gr.Button("Run Code")
|
| 22 |
+
run_button.click(fn=run_python_code, inputs=code_input, outputs=output_box)
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
demo.launch()
|