How to embed a Dash app into an existing Flask app
Dash (by Plotly) is an open source, simple-to-use Python framework for building beautiful data-driven web applications.
If you’re interested in knowing more about the framework, head over to “Introducing Dash”, the official release announcement, or check out the many examples from their gallery.
What’s relevant though is that Dash uses Flask for the back end.
If you’re familiar with Flask, you will know that it has a minimalistic core, it’s highly customizable and comes with easy-to-add extensions.
And so, it’s only natural to think that Dash apps should be easy to integrate into an existing Flask app.
Table of Contents
- Existing solutions
- A self-contained example
- Factor out the Dash app into its own subfolder
- Multiple Dash apps on a single Flask server
- Deploy to Heroku
- Outstanding points
- Notes on setup, direnv and virtualenvwrapper
- Log of updates
Existing solutions
Resources on Dash integration with an existing Flask app are sparse and incomplete.
Searching for “dash integration flask” brings up code from the official docs on deployment. B̵u̵t̵ ̵i̵t̵’̵s̵ ̵h̵a̵r̵d̵l̵y̵ ̵e̵v̵e̵n̵ ̵a̵n̵ ̵e̵x̵a̵m̵p̵l̵e̵. The Dash team did a great job and addressed requests by the community. Notheless, the new examples are only using a bare-bones Flask setup.
Other search results point to StackOverflow questions like Running a Dash app within a Flask app or to the Dash’s github issue that requests A simple working example for embedding dash in flask under a path.
These resources are still insufficient and usually have several of the following problems:
- examples are not self-contained, difficult to test, disorganized
- require an unrealistically simplified Flask app
- do not address specific basic requirements like authentication
And so, if you also have spent hours searching, reading and testing through dozens of nested links, without much success, you probably came to the same conclusion.
We need a documented strategy for embedding a Dash app into an existing Flask app!
A self-contained example
I created an easily testable and self-contained example which integrates a Dash app into a realistic Flask project.
To test it
Clone the github repository:
git clone https://github.com/okomarov/dash_on_flask
cd dash_on_flask
touch .envAdd config details to the .env file (not committed):
export FLASK_APP=dashapp
export FLASK_ENV=developmentexport DATABASE_URL=sqlite:///${PWD}/app.dbexport SECRET_KEY=secret_key_change_as_you_wish_make_it_long_123Now, either with Docker:
docker-compose build
docker-compose runOr… load config details, create a virtual environment (optional, will get back to this), install python dependencies, initialize the database, and run the app:
source .env
pip install -r requirements.txtflask db init
flask db migrate -m 'init'
flask db upgradeflask runFinally, check the app at http://127.0.0.1:5000/dashboard!
You should be re-directed to a sign-in form, with a link to register first. That is, the dashboard requires authentication:

This is exactly what we want! Register first, then sign in, and go back to to the /dashboard.
Now we have an independently built Dash app, served by our Flask app, playing well with our authentication and other extensions.
Implementation details
The app adds the simple stock plotter displayed below (modified to use yahoo prices):

to a Flask app which uses:
- the application factory pattern and blueprints
- a database to manage users (sqlite with Flask-SQLAlchemy and Flask-Migrate)
- authentication (Flask-Login)
This is a standard setup with most Flask applications. Steps and code on how to implement the infrastructure used in my repo are adapted directly from the excellent Flask Mega Tutorial by Miguel Grinberg.
There are a couple of things to note. So let’s have a look at the folder structure:
.
├── app/
│ ├── __init__.py
│ ├── extensions.py
│ ├── forms.py
│ ├── models.py
│ ├── templates/
│ │ └── ...
│ └── webapp.py
├── app.db
├── config.py
├── dashapp.py
├── migrations/
│ └── ...
├── .env
└── requirements.txtFirst, the app/ folder contains everything related to our Flask app, or the server, as referred by the Dash documentation. We create the main Blueprint and the routes in webapp.py (edited for clarity):







