I have an existing Flask app, and I want to have a route to another app. More concretely, the second app is a Plotly Dash app. How can I run my Dash app within my existing Flask app?
@app.route('/plotly_dashboard')
def render_dashboard():
# go to dash app
I also tried adding a route to the Dash instance, since it's a Flask app, but I get the error:
AttributeError: 'Dash' object has no attribute 'route'
Ok for those who are lazy enough like me, here is the code
If you want to embed a Dash app into a Flask app which uses:
check out the full solution at the repo dash_on_flask.
Extensive explanations are here.
To solve this issue, here is what I did and was successful. This should be documented in official DASH documentation
Then you can control which dashboard it is headed from inside flask
you can create different functions with different graphs between the Flask code and keep calling the code in dash
From the docs:
Now that you have the Flask instance, you can add whatever routes and other functionality you need.
To the more general question "how can I serve two Flask instances next to each other", assuming you don't end up using one instance as in the above Dash answer, you would use
DispatcherMiddleware
to mount both applications.Set
url_base_pathname
in your Dash instance.Now you can redirect to your Plotly Dashboard app under any Flask routes you want.