Dash 多页应用程序服务器问题 - dash.exceptions.NoLayoutException
Dash multipage app server issue - dash.exceptions.NoLayoutException
按照此处构建多页面仪表板应用程序的指南:https://dash.plotly.com/urls
正在尝试按照link中的示例进行复制和调整,在上面添加一层以执行。
我的项目目录是这样的:
-- wsgi.py
|-- multipageapp
|-- app.py
|-- index.py
|-- Procfile
|-- apps
|-- __init__.py
|-- app1.py
|-- app2.py
如果我进入 multipageapp 文件夹并从那里执行 index.py
文件,它工作正常。但是,我在执行 wsgi.py
文件时收到以下错误,我不确定我做错了什么,它似乎与示例中的内容相符:
Traceback (most recent call last):
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 1945, in full_dispatch_request
self.try_trigger_before_first_request_functions()
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 1993, in try_trigger_before_first_request_functions
func()
File "C:\Program Files\Anaconda3_4\lib\site-packages\dash\dash.py", line 1048, in _setup_server
_validate.validate_layout(self.layout, self._layout_value())
File "C:\Program Files\Anaconda3_4\lib\site-packages\dash\_validate.py", line 334, in validate_layout
"""
dash.exceptions.NoLayoutException: The layout was `None` at the time that
`run_server` was called.
Make sure to set the `layout` attribute of your application
before running the server.
这是wsgi.py
的内容
from multipageapp.app import server
if __name__ == "__main__":
server.run(debug=True)
这是Procfile
的内容
web: gunicorn index:app.server
这是app.py
的内容
import dash
app = dash.Dash(__name__, suppress_callback_exceptions=True)
server = app.server
这是index.py
的内容
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
from apps import app1, app2
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 == '/apps/app1':
return app1.layout
elif pathname == '/apps/app2':
return app2.layout
else:
return '404'
if __name__ == '__main__':
app.run_server(debug=True)
这是app1.py
的内容
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
layout = html.Div([
html.H3('App 1'),
dcc.Dropdown(
id='app-1-dropdown',
options=[
{'label': 'App 1 - {}'.format(i), 'value': i} for i in [
'NYC', 'MTL', 'LA'
]
]
),
html.Div(id='app-1-display-value'),
dcc.Link('Go to App 2', href='/apps/app2')
])
@app.callback(
Output('app-1-display-value', 'children'),
[Input('app-1-dropdown', 'value')])
def display_value(value):
return 'You have selected "{}"'.format(value)
这是app2.py
的内容
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
layout = html.Div([
html.H3('App 2'),
dcc.Dropdown(
id='app-2-dropdown',
options=[
{'label': 'App 2 - {}'.format(i), 'value': i} for i in [
'NYC', 'MTL', 'LA'
]
]
),
html.Div(id='app-2-display-value'),
dcc.Link('Go to App 1', href='/apps/app1')
])
@app.callback(
Output('app-2-display-value', 'children'),
[Input('app-2-dropdown', 'value')])
def display_value_two(value):
return 'You have selected "{}"'.format(value)
找到了让它工作的方法。我只是将 'index.py' 的内容移动到 'wsgi.py',删除了 'index.py' 并调整了导入以在
中包含文件夹 'multipageapp'
按照此处构建多页面仪表板应用程序的指南:https://dash.plotly.com/urls
正在尝试按照link中的示例进行复制和调整,在上面添加一层以执行。
我的项目目录是这样的:
-- wsgi.py
|-- multipageapp
|-- app.py
|-- index.py
|-- Procfile
|-- apps
|-- __init__.py
|-- app1.py
|-- app2.py
如果我进入 multipageapp 文件夹并从那里执行 index.py
文件,它工作正常。但是,我在执行 wsgi.py
文件时收到以下错误,我不确定我做错了什么,它似乎与示例中的内容相符:
Traceback (most recent call last):
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 1945, in full_dispatch_request
self.try_trigger_before_first_request_functions()
File "C:\Program Files\Anaconda3_4\lib\site-packages\flask\app.py", line 1993, in try_trigger_before_first_request_functions
func()
File "C:\Program Files\Anaconda3_4\lib\site-packages\dash\dash.py", line 1048, in _setup_server
_validate.validate_layout(self.layout, self._layout_value())
File "C:\Program Files\Anaconda3_4\lib\site-packages\dash\_validate.py", line 334, in validate_layout
"""
dash.exceptions.NoLayoutException: The layout was `None` at the time that
`run_server` was called.
Make sure to set the `layout` attribute of your application
before running the server.
这是wsgi.py
from multipageapp.app import server
if __name__ == "__main__":
server.run(debug=True)
这是Procfile
web: gunicorn index:app.server
这是app.py
import dash
app = dash.Dash(__name__, suppress_callback_exceptions=True)
server = app.server
这是index.py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
from apps import app1, app2
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 == '/apps/app1':
return app1.layout
elif pathname == '/apps/app2':
return app2.layout
else:
return '404'
if __name__ == '__main__':
app.run_server(debug=True)
这是app1.py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
layout = html.Div([
html.H3('App 1'),
dcc.Dropdown(
id='app-1-dropdown',
options=[
{'label': 'App 1 - {}'.format(i), 'value': i} for i in [
'NYC', 'MTL', 'LA'
]
]
),
html.Div(id='app-1-display-value'),
dcc.Link('Go to App 2', href='/apps/app2')
])
@app.callback(
Output('app-1-display-value', 'children'),
[Input('app-1-dropdown', 'value')])
def display_value(value):
return 'You have selected "{}"'.format(value)
这是app2.py
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from app import app
layout = html.Div([
html.H3('App 2'),
dcc.Dropdown(
id='app-2-dropdown',
options=[
{'label': 'App 2 - {}'.format(i), 'value': i} for i in [
'NYC', 'MTL', 'LA'
]
]
),
html.Div(id='app-2-display-value'),
dcc.Link('Go to App 1', href='/apps/app1')
])
@app.callback(
Output('app-2-display-value', 'children'),
[Input('app-2-dropdown', 'value')])
def display_value_two(value):
return 'You have selected "{}"'.format(value)
找到了让它工作的方法。我只是将 'index.py' 的内容移动到 'wsgi.py',删除了 'index.py' 并调整了导入以在
中包含文件夹 'multipageapp'