Deploying a free Python Dash Open Source app from a Docker container with Gunicorn
--
Do you need to deploy a Dash app without plunking down the cash for Dash Enterprise? Here’s the straightforward steps I used to deploy a simple test app in a Docker container. The container runs on a RedHat virtual machine provisioned by my employer’s sysadmins.
1. Create App
Use https://dash.plotly.com/layout to create an initial app.py
file. However, we need to make a couple of changes.
First, in order to deploy this using Gunicorn, we need to have access to the Flask app underneath Dash. On the Dash deployment page (https://dash.plotly.com/deployment), you’ll see you need to add the line server = app.server
.
Some folks reported trouble deploying the app with the default host='127.0.0.1'
argument in the run_server
method, so let’s use host='0.0.0.0'
(per ) and use port 8080
instead of the default port 8050
.
Here’s the code for app.py
:
# -*- coding: utf-8 -*-
# Run this app with `python app.py` and
# visit http://[your-ip-or-domain-name]:8080/ in your web browser.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
external_stylesheets = [
'https://codepen.io/chriddyp/pen/bWLwgP.css'
]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)server = app.server # <== ADD THIS LINE# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
"Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
"Amount": [4, 1, 2, 2, 4, 5],
"City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"]
})
fig = px.bar(
df,
x="Fruit", y="Amount", color="City", barmode="group"
)
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''
Dash: A web application framework for Python. Customized right here!
'''), # <== ADDED SOME TEXT HERE
dcc.Graph(
id='example-graph',
figure=fig
)
])
if __name__ == '__main__':
# app.run_server(debug=True) # <== THIS MAY NOT DEPLOY
# USE DIFFERENT ARGUMENTS FOR run_server METHOD…