Dash: Dash does not work properly with DispatcherMiddleware

Created on 8 Aug 2018  路  8Comments  路  Source: plotly/dash

Consider the following:

import flask
import dash
import dash_html_components as html
from werkzeug.wsgi import DispatcherMiddleware

app = flask.Flask(__name__)

flask_app = flask.Flask(__name__)
@flask_app.route('/')
def index():
    return 'Flask App'

dash_app = dash.Dash(__name__)
dash_app.layout=html.Div('Dash App')


app.wsgi_app = DispatcherMiddleware(
    flask_app,
    {'/dash': dash_app.server},
)

if __name__ == '__main__':
    app.run(debug=True, port=8050, host='0.0.0.0')

I would expect to see "Flask App" on localhost:8050/ and "Dash App" on localhost:8050/dash/.

Instead, the Dash app doesn't serve the _dash-* files from the right place:

127.0.0.1 - - [08/Aug/2018 10:43:52] "GET /dash/ HTTP/1.1" 200 -
127.0.0.1 - - [08/Aug/2018 10:43:52] "GET /_dash-layout HTTP/1.1" 404 -
127.0.0.1 - - [08/Aug/2018 10:43:52] "GET /_dash-dependencies HTTP/1.1" 404 -

Most helpful comment

Did you try configuring the dash_app's 'config.requests_pathname_prefix' to use the prefix "dash".
I recall having to configure it so (was reverse proxying dash from nginx).

EDIT: Just tested your code,
in this case set requests_pathname_prefix to a blank string.
So:
dash_app.config.requests_pathname_prefix = ""

Was able to get it to route properly.

All 8 comments

Did you try configuring the dash_app's 'config.requests_pathname_prefix' to use the prefix "dash".
I recall having to configure it so (was reverse proxying dash from nginx).

EDIT: Just tested your code,
in this case set requests_pathname_prefix to a blank string.
So:
dash_app.config.requests_pathname_prefix = ""

Was able to get it to route properly.

@boarnoah That worked for me, thank you.

Upon further review, that doesn't quite fix it:
python ... dash_app = dash.Dash(__name__) dash_app.layout=html.Div('Dash App') dash_app.config.requests_pathname_prefix = "" dash_app.scripts.config.serve_locally = True dash_app.css.config.serve_locally = True ...
Trying to serve the scripts locally breaks it again, in the same way.

Specifying requests_pathname_prefix works, but not when serving css and scripts locally.
Specifying routes_pathname_prefix (note, routes in addition to request) seems to fix this.

Not entirely sure whats going on (would be nice to find out!), here is where I found the issue discussed initially (https://community.plot.ly/t/deploy-dash-on-apache-server-solved/4855/21)

EDIT:
So to clarify in your situation:
dash_app.config.requests_pathname_prefix = ""
dash_app.config.routes_pathname_prefix = ""

Will let the app work when serving scripts and css locally.

A bit of testing suggests to me now that maybe only the following is now required, even when serving scripts/css are being served locally.

dash_app.config.routes_pathname_prefix = ""

Closing this for now

Also, I think a cleaner (and possible safer) way to achieve the same thing is through the Dash constructor:

dash_app = dash.Dash( __name__, requests_pathname_prefix='/dash/')

Now it returns AttributeError: ('Read-only: can only be set in the Dash constructor', 'requests_pathname_prefix')
What should I do?

Following the error message, try to use constructor kwargs to set requests_pathname_prefix and routes_pathname_prefix, like in @ned2's comment https://github.com/plotly/dash/issues/326#issuecomment-451864217

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ninjakx picture ninjakx  路  3Comments

mpkuse picture mpkuse  路  4Comments

lochbrunner picture lochbrunner  路  3Comments

Mondrik picture Mondrik  路  3Comments

ncdingari picture ncdingari  路  4Comments