| import subprocess | |
| import uuid | |
| async def run_python(code: str): | |
| file_id = str(uuid.uuid4()) | |
| path = f"/tmp/{file_id}.py" | |
| with open(path, "w") as f: | |
| f.write(code) | |
| try: | |
| result = subprocess.run( | |
| ["python3", path], | |
| capture_output=True, | |
| text=True, | |
| timeout=8 | |
| ) | |
| return { | |
| "output": result.stdout or result.stderr | |
| } | |
| except Exception as e: | |
| return { | |
| "error": str(e) | |
| } |