import gradio as gr import io import contextlib def execute_python_code(code): # Capture the output of the code execution output = io.StringIO() try: with contextlib.redirect_stdout(output): exec(code) result = output.getvalue() if not result: return "Code executed successfully (no output)" return result except Exception as e: return f"Error: {str(e)}" # Create the Gradio interface iface = gr.Interface( fn=execute_python_code, inputs=gr.Textbox(label="Python Code", lines=10, placeholder="Enter your Python code here..."), outputs=gr.Textbox(label="Execution Result", lines=10), title="Python Code Executor", description="Enter Python code and see the execution results. Note: This executes code in a limited environment.", examples=[ ["print('Hello, World!')"], ["for i in range(5):\n print(i)"], ["def square(x):\n return x*x\n\nprint(square(5))"] ] ) iface.launch()