Spaces:
Sleeping
Sleeping
import gradio as gr | |
import subprocess | |
def compile_and_run_code(code): | |
try: | |
# Save the code to a temporary file | |
with open("temp_code.py", "w") as file: | |
file.write(code) | |
# Execute the code and capture the output | |
result = subprocess.run( | |
["python", "temp_code.py"], | |
capture_output=True, | |
text=True | |
) | |
# Return the output or errors | |
if result.returncode == 0: | |
return result.stdout | |
else: | |
return result.stderr | |
except Exception as e: | |
return str(e) | |
# Create the Gradio interface | |
interface = gr.Interface( | |
fn=compile_and_run_code, | |
inputs=gr.Textbox(lines=20, placeholder="Write your Python code here..."), | |
outputs=gr.Textbox(label="Output"), | |
title="Python Code Compiler", | |
description="Write and run Python code directly in this simple interface." | |
) | |
if __name__ == "__main__": | |
interface.launch() | |