refactor: execute js/py code (#118)
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
import io
|
||||
import sys
|
||||
|
||||
def run(code: str):
|
||||
"""Execute the python code.
|
||||
Args:
|
||||
code: Python code to execute, such as `print("hello world")`
|
||||
"""
|
||||
return eval(code)
|
||||
old_stdout = sys.stdout
|
||||
new_stdout = io.StringIO()
|
||||
sys.stdout = new_stdout
|
||||
|
||||
exec(code)
|
||||
|
||||
sys.stdout = old_stdout
|
||||
return new_stdout.getvalue()
|
||||
Reference in New Issue
Block a user