Deploying a Dash App on Google App Engine
Once you have built your Dash application, the final step is to deploy it to the web. This guide provides the steps to deploy your app on Google App Engine (GAE), a Platform as a Service (PaaS) that handles most of the infrastructure for you.
To deploy your app, you will need three key files in your project folder:
main.py(containing your Dash app code)requirements.txt(listing the project's dependencies)app.yaml(the configuration file for App Engine)
1. The Application File (main.py)
Make sure your application file contains the line server = app.server. This is crucial because the Gunicorn application server (used by App Engine) needs to access the underlying Flask server instance.
# In your main.py
app = dash.Dash(__name__)
server = app.server # This line is required for GAE deployment
2. The Dependencies File (requirements.txt)
GCP needs a list of all the libraries your app uses. For the simple examples in this tutorial, a minimal file is all you need:
dash
pandas
gunicorn
gunicorn is the production-ready web server that App Engine will use to run your application.
For a more complex data science dashboard that includes libraries for data manipulation, machine learning, and advanced visualizations, your requirements.txt file would look more like this comprehensive example:
wheel==0.37.1
dash==2.0.0
dash-core-components==2.0.0
dash-html-components==2.0.0
dash_bootstrap_components==1.0.0
dash-renderer==1.2.0
Flask==1.1.1
Flask-Compress==1.4.0
future==0.18.2
Jinja2==2.10.3
MarkupSafe==1.1.1
numpy
pandas
pytz==2020.1
retrying==1.3.3
six==1.13.0
Werkzeug==0.16.0
gunicorn==20.1.0
plotly==5.0.0
kaleido==0.2.1
wget==3.2.0
pycountry==22.1.10
regex==2022.1.18
pycountry_convert==0.7.2
wordcloud==1.8.1
Pillow==9.0.1
nltk==3.6.7
seaborn==0.11.2
spacy==3.2.0
itsdangerous==1.1.0
markupsafe==1.1.1
For a more complex data science dashboard that includes libraries for data manipulation, machine learning, and advanced visualizations, your requirements.txt file would be much larger.
3. The Configuration File (app.yaml)
This is the most important file for App Engine. It tells Google Cloud how to run your project.
runtime: python39 # Or any other supported Python 3 version
entrypoint: gunicorn -b :$PORT main:server
runtime: Specifies the Python version. It's best to use a modern, supported version likepython39orpython310.entrypoint: This is the command that App Engine will use to start your app. It tells Gunicorn to look in themain.pyfile for a variable namedserver. The-b :$PORTpart is important, as it binds the server to the port provided by Google Cloud's environment.
Steps to Deploy
Using the Google Cloud Shell is the easiest way to deploy, as it has all the necessary tools pre-installed.
- Open Google Cloud Shell and create a project folder (e.g.,
my-dash-app). - Create a virtual environment outside your project folder to avoid uploading it during deployment.
python3 -m venv myenv - Activate the environment:
source myenv/bin/activate - Create your files: Inside your
my-dash-appfolder, createmain.py,requirements.txt, andapp.yamlwith the content from above. - Install dependencies locally:
pip install -r requirements.txt - Test locally: Run
python3 main.pyand use the "Web Preview" in Cloud Shell to make sure it works as expected. - Deploy to App Engine: From inside your project folder, run the deploy command:
gcloud app deploy
After following the prompts, your application will be deployed to a public URL.