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