Deploying a Streamlit App on Google Cloud Platform: App Engine vs. Cloud Run

Ali Shahed
ML Hobbyist
3 min readApr 10, 2023

--

Author: Ali Shahed

Introduction

Streamlit is an open-source Python library that simplifies the process of creating custom web applications for data science and machine learning. In this article, we’ll guide you through deploying a Streamlit app on Google Cloud Platform (GCP) using two popular compute services: App Engine and Cloud Run. By the end, you’ll understand the key differences between the two services and how to deploy a simple Streamlit app to both.

App Engine Deployment

App Engine is a fully managed Platform as a Service (PaaS) that allows developers to build and deploy applications without worrying about infrastructure management. It supports various runtime environments, including Python.

  • Install the required tools:
  1. Install the Google Cloud SDK: https://cloud.google.com/sdk/docs/install

2. Install Streamlit: pip install streamlit

  • Create a Streamlit app:

Write a simple Streamlit app and save it as app.py:

import streamlit as st

st.title("Hello, Streamlit on App Engine!")
st.write("This is a simple Streamlit app running on Google App Engine.")
  • Create an app.yaml configuration file:
runtime: python39

entrypoint: streamlit run app.py --server.enableCORS false --browser.serverAddress 0.0.0.0 --browser.gatherUsageStats false --server.port $PORT
  • Deploy the app to App Engine:
  1. Authenticate with Google Cloud: gcloud auth login
  2. Initialize your project: gcloud init
  3. Deploy the app: gcloud app deploy
  • Access the app:

Run gcloud app browse to open your app in a browser.

Cloud Run Deployment

Cloud Run is a managed Containers as a Service (CaaS) platform that lets you run stateless containers on a fully managed environment. It supports any language or framework packaged in a Docker container.

  • Install the required tools:
  1. Install the Google Cloud SDK: https://cloud.google.com/sdk/docs/install
  2. Install Docker: https://docs.docker.com/get-docker/
  3. Install Streamlit: pip install streamlit
  • Create a Streamlit app:

Follow the same steps as in the App Engine deployment.

  • Create a Dockerfile:
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["streamlit", "run", "app.py", "--server.enableCORS", "false", "--browser.serverAddress", "0.0.0.0", "--browser.gatherUsageStats", "false", "--server.port", "8080"]

Also, create a requirements.txt file containing streamlit.

  • Build and deploy the container to Cloud Run:
  1. Authenticate with Google Cloud: gcloud auth login
  2. Initialize your project: gcloud init
  3. Build the container: gcloud builds submit --tag gcr.io/PROJECT-ID/streamlit-app
  4. Deploy the container: gcloud run deploy --image gcr.io/PROJECT-ID/streamlit-app --platform managed --allow-unauthenticated
  • Access the app:

The deployment output will display the URL of your app.

Conclusion

In this article, we showed you how to deploy a Streamlit app to both App Engine and Cloud Run on the Google Cloud Platform. While App Engine is a PaaS suitable for web applications with built-in support for specific runtime environments, Cloud Run is a CaaS platform that offers more flexibility and can run any language or framework packaged in a Docker container. The choice between App Engine and Cloud Run depends on your application’s requirements, preferred development tools, and desired level of infrastructure management. With this guide, you can now deploy a Streamlit app to either service and make the best choice for your needs.

--

--

Ali Shahed
ML Hobbyist

PhD EE | Data Scientist | Machine Learning Engineer