Deploy Dash App on Heroku
Goal of this tutorial
Guild through how to deploy a dash app on Heroku.
Here is the final output, and of course, you can give your app a different name!
https://my-first-dash-app-brook-random.herokuapp.com/
What’s Dash?
Dash is a Python framework for building web analytic applications.
Written on top of Flask, Plotly.js, and React.js, Dash is ideal for building data visualization apps with highly custom user interfaces in pure Python. It’s particularly suited for anyone who works with data in Python.
What’s Heroku?
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.
Heroku is one of the easiest platforms for deploying and managing public Flask applications.
Prerequisite:
Heroko account and install Heroku CLI (https://www.heroku.com/)
Steps
Create a folder and initiate git
1
2
3mkdir your_dash_app_folder
cd your_dash_app_folder
git initCreate a virtual environment. This will be the environment on which app will run.
With python 3:
1
$ python3 -m venv your_venv_name
With python 2
first install virtualenv1
$ virtualenv your_venv_name
Activate virtual environment
1
$ source your_venv_name/bin/activate # uses the virtualenv
Install dash, plotly and gunicorn, and any other packages required to run your app.
1
2
3$ pip install plotly
$ pip install dash
$ pip install gunicorn # a Python Web Server Gateway Interface HTTP serverAdd app.py file. An example would be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28import os
import dash
import dash_core_components as dcc
import dash_html_components as html
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.layout = html.Div([
html.H2('Hello World'),
dcc.Dropdown(
id='dropdown',
options=[{'label': i, 'value': i} for i in ['LA', 'NYC', 'MTL']],
value='LA'
),
html.Div(id='display-value')
])
[dash.dependencies.Input('dropdown', 'value')])
def display_value(value):
return 'You have selected "{}"'.format(value)
if __name__ == '__main__':
app.run_server(debug=True)
- Add .gitignore file
1
2
3
4
5venv
*.pyc
.DS_Store
.env - Add procfile(Note that app refers to the filename app.py. server refers to the variable server inside that file).
1
web: gunicorn app:server
Run locally
1
$ python app.py
Add requirements.txt
1
$ pip freeze > requirements.txt
Init and deploy by heroku
As for now Heroku allows one “dyno” and five free apps to run.1
2
3
4
5$ heroku create my-dash-app # change my-dash-app to a unique name
$ git add . # add all files to git
$ git commit -m 'Initial app boilerplate'
$ git push heroku master # deploy code to heroku
$ heroku ps:scale web=1 # run the app with a 1 heroku "dyno"Redeploy
1
2
3
4$ git status # view the changes
$ git add . # add all the changes
$ git commit -m 'a description of the changes'
$ git push heroku master
Trouble shooting
- what if App deployment fails? Try find out the deployment details by
heroku logs --tail
Reference:
https://dash.plotly.com/introduction
https://www.heroku.com/
https://dash.plotly.com/layout