Spaces:
Runtime error
Runtime error
Create code_runner.py
Browse files- tools/code_runner.py +10 -0
tools/code_runner.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
|
3 |
+
def run_python_code(code: str):
|
4 |
+
try:
|
5 |
+
with open("temp.py", "w") as f:
|
6 |
+
f.write(code)
|
7 |
+
result = subprocess.check_output(["python3", "temp.py"], stderr=subprocess.STDOUT, timeout=5)
|
8 |
+
return result.decode()
|
9 |
+
except Exception as e:
|
10 |
+
return str(e)
|