File size: 741 Bytes
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
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()