Deploying Dash Apps to Heroku

By Angela C

September 30, 2021 in Dash Flask Python

Reading time: 2 minutes.

A Dash app is a Flask app so the same deployment options apply.

The recommended method for securely deploying Dash applications is using Dash Enterprise.

Dash Enterprise is Plotly’s commercial product for developing & deploying Dash Apps on your company’s on-premises Linux servers or VPC (AWS, Google Cloud, or Azure).

I have deployed to Linode and Python Anywhere before but now I am going to deploy to Heroku. You can have up to 5 apps deployed for free on Heroku.

Heroku is recommended by Dash for sharing public Dash apps for free.

The instructions for deploying a Dash app to Heroku are outlined on https://dash.plotly.com/deployment. If followed precisely you can get an app up and running quite quickly! My simple weather app is here at https://angela1c-weather-app.herokuapp.com. This app is very much a work in progress though…

For this you need Git, Python, the Heroku command line interface and Gunicorn to be installed.

  1. Create a new folder for the project
  2. Initialise the folder with ‘git’
  3. Create a virtual environment in the folder and activate it.
  4. Install the app’s dependencies within the virtual environment.
  5. Install gunicorn using pip install gunicorn. Gunicorn is a WSGI HTTP server that is used for deploying Flask apps into production.
  6. Create a Python application and name it app.py

The app.py file should include:

app = dash.Dash(__name__)
server = app.server

  1. Add the following files that are needed for deployment:

    • A requirements.txtfile containing a list of Python dependencies for the application

    • A Procfile containing web: gunicorn app:server. This says to start a gunicorn server for the app. app in the Procfile refers to the filename app.py while server refers to the variable server inside the app.py file.

    • .gitignore list of files that should not be tracked by Git.

  2. Install the Heroku command line interface. heroku --version to check if it is already installed

When you have the app up and running on the local host in the virtual environment, add the app’s dependencies to the requirements.txt file. You can pipe the output of pip freeze to the requirements.txt file with the following command: pip freeze > requirements.txt.

  1. Initialise Heroku, add files to Git and deploy
  • heroku create <my-app-name> # set your app name here.
  • git add . to add all files to git
  • git commit -m"a descriptive git message"
  • git push heroku master to deploy the app to Heroku Once that is complete set heroku ps:scale web=1 to run the heroku app with one Heroku ‘dyno’.

I have deployed an app to Heroku here. It is very much a work in progress but updating it is as easy to deploying it. It is similar to push changes to GitHub.

10 Modify an app locally and update deployed app.

  • git status to view any changes made since the last time it was pushed.
  • git add . to add all files.
  • git commit -m"short message describing the changes"
  • git push heroku master to push the changes to Heroku.