Black Lives Matter. Please consider donating to Black Girls Code today.

Dash + Flask with ProxyMiddleware instead of DispatcherMiddleware

Hello! I am currently using DispatcherMiddleware as per the docs to connect a few Dash apps with a Flask app, which looks something like this:

# ... A few Flask extensions (db, server configs, mail)...
# All share the same user database

# A login manager
login_manager.init_app(flask_app)
login_manager.init_app(data_app.server)
login_manager.init_app(viz_app.server)

# Example from docs to connect WSGI apps
application = DispatcherMiddleware(flask_app, {
    "/data": data_app.server,
    "/viz": viz_app.server,
}

The user login is performed in the Flask server. The apps check if the user is authenticated, if yes the dash app becomes available, if not they redirect back to Flask. All is good so far. What I’d like to do is have each Dash app run on its own and have Flask (or some other solution) redirect to them along with a token or string (e.g. username) or credentials.

I’ve tried the simple:

# Flask runs on 8000, e.g. with: gunicorn wsgi:application
application = ProxyMiddleware(flask_app, {
    "/demo/": {
        "target": "http://127.0.0.1:8050/demo"  # with AND without /demo at the end
    }
})

And I have tried both with and without requests_pathname_prefix="/demo/" when defining the Dash apps. I end up getting Uncaught SyntaxError: Unexpected token < in the browser console (tries to find files in Flask rather than Dash, i.e. http://127.0.0.1:8000) or a GET/404 for the dash files (when omitting requests_pathname_prefix). Of course I also tried various combinations of removing or adding / slashes in the URL and the configurations.

Any suggestions? Thanks :slight_smile:

Hmmmmm… Bumpity bump?

What do you mean by “What I’d like to do is have each Dash app run on its own and have Flask redirect to them”? Isn’t that the case with this DispatcherMiddleware setup? I have almost the same configuration and my issue is that the Dash apps are running on their own so I am struggling to do authentication. If I require authentication on / (the flask server) it works but then I can access /app1 where it’s the Dash app running.
How did you manage to require authentication at the root level? Once in the Dash app there is no facility to check the headers, redirect, etc