Spaces:
Sleeping
Sleeping
File size: 918 Bytes
5536629 580fa5d 5536629 580fa5d 5536629 580fa5d 5536629 580fa5d 5536629 |
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 27 28 29 30 31 32 33 34 35 |
import gradio as gr
import sys
from io import StringIO
def run_code(code):
try:
# Redirect stdout to a buffer
output_buffer = StringIO()
sys.stdout = output_buffer
# Execute the code in a separate namespace
namespace = {}
exec(code, namespace)
# Get the output from the buffer
output = output_buffer.getvalue()
except Exception as e:
output = f"Error: {str(e)}"
finally:
# Reset stdout to its original value
sys.stdout = sys.__stdout__
return output
# Define the Gradio interface
with gr.Blocks() as demo:
code_input = gr.Textbox(label="Enter Python Code", lines=10)
run_button = gr.Button("Run Code")
output_text = gr.Textbox(label="Output", lines=10)
# Define the event handler
run_button.click(fn=run_code, inputs=code_input, outputs=output_text)
# Launch the Gradio app
demo.launch() |