Documenting strategies for embedding Dash apps in an existing Flask app
See original GitHub issueThe community has been asking for a while about strategies for adding a Dash app to an existing Flask app (see plotly/dash#214). I think it would be good if we could get something into the documentation to help with this.
I’d be up for putting together a PR, but first I was hoping to get @plotly/dash’s thoughts on the different strategies we could endorse, as well as some ideas about when the different approaches might be useful.
I’m currently aware of two distinct strategies:
-
Passing your
Dash
instance constructor theserver
kwarg that is an existingFlask
instance as well as passing a value to theurl_base_pathname
kwarg so that multiple Dash apps can sit side by side without clashing. This is described very well in a comment on the above mentioned issue. -
Using Werkzeug’s
DispatcherMiddleware
to mount theFlask
instance of one or more existingDash
instances at specific routes of a parentFlask
instance. This is discussed in this Dash community forum thread.
My take is that the first option has less moving parts and so is a bit simpler, however the second has a bit more flexibility in that you can embed arbitrary existing Dash apps that can also be run independently (as opposed to requiring an existing flask instance to be passed in).
Anyone have more strategies to add or thoughts about these two?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
Top GitHub Comments
@ned2 I created a solution in https://github.com/okomarov/dash_on_flask for embedding a Dash application into a Flask app which:
Explanations at: https://medium.com/@olegkomarov_77860/how-to-embed-a-dash-app-into-an-existing-flask-app-ea05d7a2210b
Let me know if something is re-usable for your purposes.
@ned2, great idea I have successfully integrated Dash into a Flask app with multiple HTML pages as well as flask-login on the Dash app, however it took me quite some time to figure out and then clean up.
@sidd-hart, as mentioned above this can be done. I used flask-login and wtforms for my authentication and protected my dash app like so:
Obviously there is lots of Flask-Login code in the configuration as well but I’m sure you are aware of that already.
Hope this helps.