我正在使用Gunicorn部署我的Dash应用程序。升级到Gunicorn 20.0.0后,找不到我的应用程序。
onclick
gunicorn --bind=0.0.0.0 --timeout 600 index:app.server
This issue on Gunicorn's issue tracker似乎与该错误有关,但是我不知道该如何解决。如何让Gunicorn 20找到我的应用?
Failed to find application object 'app.server' in 'index'
[INFO] Shutting down: Master
[INFO] Reason: App failed to load.
:
index.py
import os
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
from pages import overview
from webapp import app
app.index_string = open(os.path.join("html", "index.html")).read()
app.layout = html.Div([
dcc.Location(id="url", refresh=False),
html.Div(id="page-content")
])
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def display_page(pathname):
if pathname == "/a-service/overview":
return overview.layout
else:
return overview.layout
if __name__ == "__main__":
app.run_server(debug=True, port=8051)
:
webapp.py
答案 0 :(得分:1)
Gunicorn 20更改了它解析和加载应用程序参数的方式。它曾经使用eval
,后面跟随属性访问。现在,它仅对给定模块中的单个名称进行简单查找。 Gunicorn能够理解Python语法(例如属性访问)的功能尚未得到记录或打算实现。
Dash有关部署的文档没有使用您正在使用的语法。他们说要执行以下操作,这将适用于任何版本的Gunicorn:
webapp.py
:
server = app.server
$ gunicorn webapp:server
您已经在server
模块中添加了webapp
别名,但是您的代码布局有些混乱,使您感到困惑。您将忽略在webapp
中进行的设置,而将index
用作入口点。您将所有内容放在单独的顶层模块中,而不是放在一个包中。
如果要从索引视图中拆分应用程序设置,则应遵循定义应用程序的标准Flask模式,然后将其导入所有包中。
project/
myapp/
__init__.py
webapp.py
index.py
webapp.py
:
app = dash.Dash(...)
server = app.server
# index imports app, so import it after app is defined to avoid a circular import
from myapp import index
$ gunicorn myapp.webapp:server
答案 1 :(得分:0)
您还应该将服务器从webapp.py导入到index.py,然后使用常规的gunicorn过程:
gunicorn index:server -b :8000
答案 2 :(得分:0)
结构为多页应用程序 (https://dash.plotly.com/urls) 的 Dash 项目将具有 app.py
(此处命名为 webapp.py
)和 index.py
。如链接指南中所述,入口点应为 index.py
以防止循环导入。
使用 index.py
作为入口点只需要两个更改:
app
和 server
导入 index.py
from webapp import app, server
gunicorn -b localhost:8000 index:server