使用Taipy时触发RuntimeError问题求助
Taipy部署Docker时Werkzeug生产环境报错问题
错误信息
RuntimeError: The Werkzeug web server is not designed to run in production. Pass allow_unsafe_werkzeug=True to the run() method to disable this error.
环境配置
docker-compose.yml
version: "3.9" services: my_test: build: . container_name: my_test volumes: - .:/app ports: - "5000:5000"
Dockerfile
FROM python:3.9 WORKDIR /app COPY . /app RUN pip install -r requirements.txt CMD [ "python", "./app.py" ]
requirements.txt
docker gpt-engineer flask==2.2 marshmallow-sqlalchemy==0.26.1 taipy
app.py
from flask import Flask from taipy import Gui flask_app = Flask(__name__) @flask_app.route("/home") def home_page(): return "The home page." gui = Gui(page="# Taipy application", flask=flask_app) gui.run(allow_unsafe_werkzeug=True)
运行日志
ilai@ilai-dell-g15-5520:~/Code/taipy$ docker compose up [+] Running 2/2 ⠿ Network taipy_default Created 0.1s ⠿ Container my_test Created 0.1s Attaching to my_test my_test | * Server starting on http://127.0.0.1:5000 my_test | Traceback (most recent call last): my_test | File "/app/./app.py", line 11, in <module> my_test | gui.run(allow_unsafe_werkzeug=True) my_test | File "/usr/local/lib/python3.9/site-packages/taipy/gui/gui.py", line 1151, in run my_test | return self._server.runWithWS( my_test | File "/usr/local/lib/python3.9/site-packages/taipy/gui/server.py", line 253, in runWithWS my_test | self._ws.run(self._flask, host=host, port=port, debug=debug, use_reloader=use_reloader) my_test | File "/usr/local/lib/python3.9/site-packages/flask_socketio/__init__.py", line 641, in run my_test | raise RuntimeError('The Werkzeug web server is not ' my_test | RuntimeError: The Werkzeug web server is not designed to run in production. Pass allow_unsafe_werkzeug=True to the run() method to disable this error.
注:尝试不集成Flask运行,仍出现相同问题。
解决方案
错误根源是Taipy底层依赖的Flask-SocketIO未接收到allow_unsafe_werkzeug=True参数,同时Docker环境下服务器需绑定0.0.0.0才能外部访问。修改app.py中的gui.run()调用:
gui.run(allow_unsafe_werkzeug=True, host="0.0.0.0")
说明
- Taipy的
gui.run()会将额外的关键字参数传递给底层Flask-SocketIO的run()方法,因此allow_unsafe_werkzeug=True会被正确传递,解决生产环境报错。 - 绑定
0.0.0.0而非默认的127.0.0.1,确保Docker容器内的服务能被宿主机访问。
内容的提问来源于stack exchange,提问作者i h
相关产品推荐
相关产品推荐

