Deploy a Python Visualization Panel App to Google Cloud App Engine | by Sophia Yang | Towards Data Science

Related article: Python Visualization Panel App to Google Cloud Run: Google Cloud Run, Google Cloud Build, and Terraform
Is it possible to deploy a Python app or dashboard to the cloud without knowing anything about Docker or Kubernetes and without using a Dockerfile? Yes, it is possible and it is surprisingly simple using the Google Cloud App Engine! This article will walk you through how to deploy a Panel app to Google Cloud with only three short scripts and show how to automate your workflow with Github Actions.
Here is what my very simple example app looks like. It is hosted here (I will keep it live for 3 months). For more examples and inspirations on Panel apps, check out awesome-panel.org, panel.holoviz.org, and my previous blog post on HoloViz tools.
All the code mentioned in this article is available at this repo: https://github.com/sophiamyang/panel_gcp.
The App Engine documentation (https://cloud.google.com/appengine/docs/standard/python3/quickstart) describes the four steps you need to take to enable running a project:
Select or create a Google Cloud Project (which we will assume the project name and ID are both “your-project”)
Enable App Engine API
Enable Cloud Build API
Enable Billing
Install Google Cloud SDK: The instructions differ by operating system, or you can simply do
conda install -c conda-forge google-cloud-sdk
Initialize gcloud: gcloud init
Set project to “your-project” (or whatever name/ID you used for yours):
gcloud config set project your-project
Make a new directory and either create the following three files in this directory or get them from my repo: https://github.com/sophiamyang/panel_gcp
This is the Python file that creates the Panel App. To run this app locally, you can simply do conda install panel hvplot and runpanel serve app.py
This file lists all the package dependencies of our Panel app.
This is the App Engine configuration file. Panel apps are a type of Bokeh app, and Bokeh apps need env:flex and entrypoint: panel serve app.py --adddress 0.0.0.0 --port 8080 --allow-websocket-origin="*" specified because the App Engine Flexible Environment supports WebSockets. Other types of apps may not need the flex environment and entrypoint.
Initialize your App Engine app: gcloud app create
Deploy your app: gcloud app deploy
After gcloud works hard to set up your project for a few minutes, you should be able to see your dashboard live on your-project.ue.r.appspot.com.
That’s it! You can now share that URL with anyone you want to use your app.If there are any issues with your deployment, it might be helpful to check the logs and debug: https://console.cloud.google.com/debug
To check the overall info: https://console.cloud.google.com/appengine
To see the versions: https://console.cloud.google.com/appengine/versions
In practice, as you develop and improve your Panel application, you’ll often want to redeploy different versions of it over time while keeping a record of each version of your files. To streamline this process, you might find Github Actions quite useful. With Github Actions, we can set up a workflow that whenever we push changes, tag releases, or get a PR, some build, test, or deployment processes will run.
First, we need to follow the steps on these docs to create a service account, add a key to your service account, and download the key in a JSON file.
Enable App Engine Admin API
Now if you haven’t done this already, create a new Github repo and add all the three files in section 3 to this repo and push it to Github.
Go to your Github repo — Settings — Secrets and add two secrets
GCP_CREDENTIALS: copy and paste the key from the JSON file you just downloaded
GCP_PROJECT: the name of your project, which would be “your-project” in the example above.
Add a .github/workflows/python-app.yml file in your directory
This is a Github Actions configuration file. In this example, we deploy the app to Google Cloud App Engine whenever there is a push to the main branch.
Now your Github Actions should be ready to go. You can see the info of the runs on the `Actions` tab. Here I tried to add another app and the workflow automatically starts running.
Now you can see we have two apps running
Overall, this article uses three simple scripts to deploy a visualization Panel app to Google Cloud App Engine and uses another config file to set up Github Actions. You may wonder, why am I not using Google Cloud Run or AWS? I wrote another article on Google Cloud Run. Check it out: Python Visualization Panel App to Google Cloud Run: Google Cloud Run, Google Cloud Build, and Terraform. In terms of AWS, my friends at Anaconda are working on something really cool to help simplify the AWS deployment process, which is scheduled to become available sometime in 2022. Stay tuned!
For those who are interested in a video tutorial, here is the video version of this article:
References:
https://cloud.google.com/appengine/docs/standard/python3/runtime#application_startup
https://cloud.google.com/appengine/docs/flexible/python/quickstart
Acknowledgment: Thank you Jim Bednar and Philipp Rudiger for your support and guidance.
By Sophia Yang on December 18, 2021