Spaces:
Sleeping
Sleeping
File size: 1,018 Bytes
0d018ca |
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 |
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() |