mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 13:07:17 +08:00
🚀 部署(chat_page.py):添加对Python代码运行执行的功能 🔧 增加(python_code_render.py):删除无用的文件 ✨ 增加(python_execute.py):添加Python代码执行的功能
24 lines
662 B
Python
24 lines
662 B
Python
import io
|
|
import sys
|
|
import traceback
|
|
|
|
|
|
class PythonExecute:
|
|
def run(self, code):
|
|
# 创建一个 StringIO 对象来捕获输出
|
|
output = io.StringIO()
|
|
# 保存当前的 stdout
|
|
old_stdout = sys.stdout
|
|
sys.stdout = output
|
|
try:
|
|
# 执行代码
|
|
exec(code)
|
|
except Exception as e:
|
|
# 如果执行出错,返回错误的堆栈跟踪
|
|
sys.stdout = old_stdout
|
|
return traceback.format_exc()
|
|
finally:
|
|
# 恢复原来的 stdout
|
|
sys.stdout = old_stdout
|
|
# 获取捕获的输出并返回
|
|
return output.getvalue() |