Image Generated by Google Gemini Advanced

How to deploy your Streamlit Web App to Google Cloud Run with ease?

Maddula Sampath Kumar
Google Cloud - Community

--

A Comprehensive Guide for Building and Deploying ML/Deep Learning Web Apps Using Streamlit

In this post, you will learn the following:

  1. What is Streamlit and why should you use it?
  2. How to build a simple Streamlit application?
  3. What is Cloud Run and why use it?
  4. How to deploy a Streamlit application code directly to CloudRun?

Prerequisites:

  • Google Chrome
  • Google Cloud Project

For development environments or IDEs, the recommended option is to use Google Cloud Shell, a simple and convenient tool. Alternatively, you can set up local access to your project using the gcloud CLI and the latest version of Python.

What is Streamlit ? Why use Streamlit?

https://streamlit.io/

Streamlit, a renowned open-source Python framework, allows data scientists and AI/ML practitioners to create dynamic data applications with minimal coding effort. However, when comparing Streamlit to well-established frameworks like Django, Flask, and Tornado, the answer regarding its superiority becomes more nuanced.

From my past experiences with Django and preference for Flask, I found Streamlit to be lacking in terms of speed and responsiveness. However, Streamlit’s true strength lies in its ability to facilitate highly interactive applications.

While Streamlit may not excel in raw speed, it offers a significant advantage for beginners and data scientists whose primary focus is on building AI models, enhancing data quality, and engaging in iterative prototyping. Its slower interactive responses are better suited for these tasks compared to the time-consuming process of mastering comprehensive web frameworks and selecting appropriate JavaScript packages.

TL;DR, Streamlit simplifies the development process, enabling professionals to devote their time and energy to their core areas of expertise rather than getting bogged down in complex web development tasks.

What is Google Cloud Run? Why use Google Cloud Run?

Cloud Run is a serverless platform on Google Cloud, that lets you run your applications without having to manage infrastructure. You can deploy your code to Cloud Run, and it will automatically scale up or down based on traffic.

There are many benefits to using Google Cloud Run, including:

  • Easy to use: Cloud Run is easy to set up and use, even if you’re not a DevOps expert.
  • Serverless: Cloud Run takes care of all the infrastructure management for you, so you can focus on your code.
  • Scalable: Cloud Run automatically scales your application up or down based on traffic, so you don’t have to worry about capacity planning.
  • Cost-effective: Cloud Run is a pay-as-you-go service, so you only pay for the resources that you use.

For demos & low demand use cases, Streamlit’s rapid development velocity combined with Cloud Run’s scalability makes them a great pair for deploying AI/ML web apps with ease.

How to build a simple Streamlit Application?

Let us start with a quick peek for what we are building.

Create a file named `requirements.txt`, with the following content

streamlit

Create a file named `streamlit_app.py`, with the following content

import streamlit as st

st.title("Sample AI App")

st.text("This is a sample app.")

Install the requirement and run the app

# (Optional) Create and use virtual environment
python3 -m venv .venv
source .venv/bin/activate

# Install the application requirements
pip install -r requirements.txt

# Run the application
streamlit run streamlit_app.py --server.port 8080

To preview your application from Cloud Shell, use the setting in the top-right corner to select Preview on Port 8080

How to deploy to cloud run ?

Cloud Run’s default application endpoint resembles. `gunicorn — bind :8080 main:app`. Users can customise this, create a `Procfile` file with the following content:

web: streamlit run streamlit_app.py - server.port=8080 - server.address=0.0.0.0 - server.enableCORS=false - browser.gatherUsageStats=false

Create a `deploy.sh` file with the following content and update the value for `PROJECT` value

#!/bin/bash
# Purpose: To deploy the App to Cloud Run.

# Google Cloud Project ID
PROJECT=my-google-cloud-project

# Google Cloud Region
LOCATION=europe-north1

# Deploy app from source code
gcloud run deploy simple-app --source . --region=$LOCATION --project=$PROJECT --allow-unauthenticated

Use `bash deploy.sh` command to run the above script and get a Service URL from the output. If you’re a first-time user of Cloud Run, the `gcloud` command in the provided script might prompt you with additional questions, as shown below screenshot.

After deploying to Cloud Run, access your application using the `Service URL`. Alternatively, use Cloud Run page to access & updates your application details like Public url, Logs, metrics & other.

Note: deploy.sh script creates an application with unauthenticated access, making it accessible on the public internet. To ensure the security of your application, it is essential to take additional measures. Refer to https://cloud.google.com/run/docs/authenticating/overview for more information on securing your application’s access.

Congratulations 🎉! Your Streamlit application has been deployed to Cloud Run.

You can find this code in https://github.com/GoogleCloudPlatform/devrel-demos/tree/main/ai-ml/gemini-chatbot-app/lesson01

To cleanup your application, go to https://console.cloud.google.com/run to select your application and delete.

To learn more,

--

--

Maddula Sampath Kumar
Google Cloud - Community

Software Developer - Developer Relations in Google Cloud, Poland. To more about me, check https://www.linkedin.com/in/msampathkumar/