Deploying a python app with plotly dash and AWS elastic beanstalk

Austin Lasseter
3 min readJun 28, 2018

In a new folder on your laptop, create a virtual environment:

virtualenv env

Activate the virtual environment (on a Mac):

source env/bin/activate

Activate the virtual environment (this is for Windows):

env\Scripts\activate.bat

Install plotly dash in your virtual environment:

pip install dash

Create a text file called requirements.txt with a list of programs required by your virtual environment, as follows:

pip freeze > requirements.txt

Create a python program called application.py with the following Dash code. Note that two items in here — application = app.server and application.run(port=8080) are specific to AWS Elastic Beanstalk. If you’re using a different deployment platform (like Heroku) you can follow the blog post I wrote here.

import dash
import dash_core_components as dcc
import dash_html_components as html
########### Initiate the app
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
application = app.server
app.title='Dash on AWS EB!'
########### Set up the layout
app.layout = html.Div(children=[
html.H1(children='Hello Dash'),
html.Div(children='''
This is Dash running on Elastic Beanstalk.
'''),
dcc.Graph(
id='example-graph',
figure={
'data': [
{'x': ['left', 'center', 'right'], 'y': [3,7,6], 'type': 'bar', 'name': 'category 1'},
{'x': ['left', 'center', 'right'], 'y': [4,2,5], 'type': 'bar', 'name': 'category 2'},
],
'layout': {
'plot_bgcolor': 'lightgray',
'title': 'Graph Title',
'xaxis':{'title':'x-axis label'},
'yaxis':{'title':'y-axis label'},
},
}
)
])
########### Run the app
if __name__ == '__main__':
application.run(debug=True, port=8080)

Try running your application locally, within the virtual environment, to make sure it’s okay:

python application.py

You can navigate to that URL (http://127.0.0.1:8080/) in your browser to confirm that the application runs smoothly. If it works you should see this:

You should now exit the virtual environment by typing deactivate in terminal.

The next step is to create a zip file containing the application and requirement files, as follows.

zip myfiles.zip requirements.txt application.py

You can of course also do this using Winzip and Finder (or Explorer if you’re on a PC):

Now log into AWS console, go to Elastic Beanstalk, and create a new application:

Give your new app a name and description, then click create:

You’ll see the following message: “No environments currently exist for this application.” Click “create one now.” Then select “Web server environment” as shown below.

In “Base Configuration”, choose “Python” from the list of preconfigured configurations:

Where it says “upload your code” select and upload the zip file you created earlier, and select “create environment”

After waiting a few minutes, you should see output like this:

Once the new environment transitions to “okay”, the URL of your app will be displayed in the top right of the EB console.

That’s it! You can view my finished application here. I also have a repository with additional examples of simple Plotly Dash apps, for easy imitation, here.

--

--