Spaces:
Sleeping
Sleeping
import gradio as gr | |
def run_code(code): | |
try: | |
# Execute the code in a separate namespace | |
namespace = {} | |
exec(code, namespace) | |
# Capture the output | |
output = namespace.get('output', None) | |
if output is None: | |
output = "No output captured." | |
except Exception as e: | |
output = f"Error: {str(e)}" | |
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() |