question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dash does not work properly with DispatcherMiddleware

See original GitHub issue

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 -

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
boarnoahcommented, Aug 8, 2018

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.

1reaction
pjaolcommented, Mar 15, 2021

I realize this is closed but I’ve been banging my head against a wall trying to figure this out

DO NOT HAVE A TRAILING SLASH in your DispatchMiddleware mount points e.g.

# include trailing slash
dash_app = dash.Dash(__name__, requests_pathname_prefix="/dash/")

app.wsgi_app = DispatcherMiddleware(
    flask_app,
    # do not include trailing slash
    {'/dash': dash_app.server},
)

Failing to do so will result in 308 redirects

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flask's DispatcherMiddleware not playing nice with Plotly's ...
This is how I overcame this problem. Not pretty but effective. You need run.py: from werkzeug.wsgi import DispatcherMiddleware from ...
Read more >
Flask's DispatcherMiddleware not playing nice with Plotly's ...
Hi! This question is a copy of this SO question – consider posting the answer there and noting here if you've done so)....
Read more >
Two Flask apps, frontend and admin, on one domain using ...
Using Werkzeug's dispatcher middleware we combine two Flask apps into a larger one with ... The DispatcherMiddleware object does not have a method...
Read more >
How to Run Multiple Flask Applications from the Same Server
So if you have separate Flask, Django, or Dash applications you can run them in the same interpreter side by side if you...
Read more >
Python – Running a Dash app within a Flask app
How can I run my Dash app within my existing Flask app? @app.route('/plotly_dashboard') def ... AttributeError: 'Dash' object has no attribute 'route' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found