Deploying Streamlit Apps to GCP

Streamlit is a minimal, modern data visualization framework that's rapidly becoming the go-to dataapp framework in the Python ecosystem. This post introduces Streamlit, and shows you how to securely and scalably deploy your Streamlit apps with Google App Engine.

Deploying Streamlit Apps to GCP

What is Streamlit?

If you're familiar with the Data Science (DS) software ecosystem in Python, you'll likely have come across a handful of widely used dashboarding and data visualization tools designed for programmatic usage (e.g. to be embedded in notebooks or to be served as standalone web-apps). For the last few years, the likes of Dash, Bokeh and Voila have been some of the biggest open-source players in this space. Within the world of R, there's also the long-standing champion of dashboarding tools: Shiny.

With this relatively mature ecosystem in place, you may question the need for a yet another framework to join the pack. But that's exactly what the team over at Streamlit are doing: introducing a brand new framework for building data applications. What's more, they've created quite a bit of buzz around their project too, so much so that they recently closed a $21M Series A funding round to allow them to continue developing their framework.

Announcing Streamlit’s $21M Series A
In 2018, we began building the tools to visualize Python scripts that became Streamlit. By then, Python was the preeminent language of machine learning, bristling with powerful libraries like Keras…

So what has got folks so excited about Streamlit?

Real-time computer-vision visualization in Streamlit.

Well firstly, it is quite literally one of the most straightforward tools you're likely to come across. There's no boilerplate, and pretty much no framework-specific concepts you need to keep in mind. It feels very well designed. Altogether this simplicity makes it remarkably quick and easy to get an app built in (pretty much) seconds.

This minimalism lends itself to a second key point: event management is a breeze. This might sound dull, but it's kind of a big deal in this case. There's no framework-specific design patterns/callbacks needed to manage events in Streamlit. For example, say you want to run some code on the click of a button, you'd capture this in Streamlit with:

import streamlit as st

if st.button("Click Me"):
	print("Clicked")

Whenever the Click Me button is pressed, the block beneath it will be executed. The same concept is applied pretty much the same way to all of their interactive components. It does some clever tricks under the hood to make this work, but the end effect is the same: you can quickly and simply write apps with complex behaviour with very little change to your underlying code. In the same way that Fire makes it incredibly easy to create CLIs for your existing code, Streamlit makes it ludicrously easy to create interactive apps from existing code. As they put it: it's designed to be 'sprinkled' onto your code.

Finally, Streamlit is also pretty much painless to integrate with existing data visualization tools, and is super easy to deploy to boot – it plays nicely with modern deployment tools and technologies. You can embed your matplotlib charts alongside your dash plots to your heart's content, and then containerize it all ready for deployment in moments. All in all, these features conspire to make Streamlit one of the most productive, modern data visualization tools out there at the moment.

If you'd like to have a play with a Streamlit app, here's the version you'll be deploying later in this post.

Creating your app

While the docs for Streamlit are pretty good for getting started, there's not a huge amount out there on deploying your apps. This post aims to highlight how simple it is to deploy a Streamlit app as a live, public-facing website. It's going to be using one of the 'standard' demo apps provided by Streamlit: a visualization of Uber Pickups in New York City by time of day. However, feel free to create your own app and deploy that instead (the template you'll be using won't need to be changed!).

What you'll need

To get started, you'll need to make sure you have few things set up. You'll be deploying your app with Google App Engine, so this means you'll need to both register for a Google Cloud account (there's $300 of credit available on signup too!) and then install their gcloud command line tool. You can sign up for an account here (it's not an affiliate link, don't worry!):

GCP Free Tier - Free Extended Trials and Always Free | Google Cloud
GCP Free Tier expands our free program in two ways. $300 credit in a 3-month free trial and 20+ always free products. Learn more.

Google Cloud also provides a great 'Getting Started' guide to installing gcloud.

gcloud command-line tool overview | Cloud SDK Documentation

When this is done, make sure you have Docker installed and running. You won't need to interact with Docker to follow along with this post, but you will need it running! You'll also need to have a valid version of Python 3.6 or greater, too. It's a good idea to set up and activate a Python virtual environment for the project too.

Time to code

So, first thing's first. You're going to need to clone the deployment-ready Streamlit project template from GitHub. In your terminal, run:

git clone https://github.com/markdouthwaite/streamlit-project

Navigate into your new streamlit-project directory and run:

pip install -r requirements.txt

As you might expect, this'll install packages you'll need to run this app. With this setup, time to test your app! You can do that with:

make run-container

To build your app into a Docker image and launch a new container running your app at http://0.0.0.0:8080. It'll automatically open a new browser tab and load your app. You should see a lovely visualization like this:

Your nice new app!

Feel free to edit the app however you want. It's worth getting to grips with how powerful Streamlit can be. When you're done, just make sure that make run still successfully launches your app on http://0.0.0.0:8080, and then you can head to deployment.

Deploying your app

You'll be using Google App Engine to deploy the app. This is a managed service provided by Google Cloud that helps you deploy and scale your apps super easily. Google will manage scaling, server settings and other bits and pieces for you. All you need is a properly configured Docker image and you're pretty much there. Go to your Google Cloud Console, head to App Engine in the left hand navigation menu and check that your App Engine API is live. You should now be able to run:

make gcloud-deploy

This'll build your image, store it in Google Container Registry (GCR), and then configure and launch your app. It'll tell you when it's done. You should see something like (where {{YOUR-URL}} will be the URL for your deployed app):

Deployed service [default] to [{{YOUR-URL}}]

Navigate to the URL provided and look at your app in all its glory!

Next steps

With that done, your app is now publicly visible and can be shared with anyone you wish. You can also now edit and redeploy your app freely using make gcloud-deploy. You could even look at building a Continuous Integration and Continuous Delivery (CI/CD) pipeline with GitHub Actions to automatically re-deploy your app every time you push a change to GitHub. That's up to you though!

Trouble shooting

If you run into any issues with the above steps, the docs for App Engine are pretty good and well worth a look:

Building Custom Runtimes

If you're still struggling, feel free to reach out to me on Twitter.

Finishing up

So that's pretty much it. Deploying a new Streamlit app in a scalable, secure way. Simple, eh? As you dig in to Streamlit, you'll notice that it's still a bit restrictive in a few ways: layouts and styling are highly restricted by default, for example. However, their recent funding rounds have given them a lot of runway to develop some cool new features, and these things are on their feature roadmap, so it should just be a matter of time until they arrive. If you're interested in data visualization, Streamlit is definitely one to watch.

Streamlit — The fastest way to create data apps
Streamlit is an open-source app framework for Machine Learning and Data Science teams. Create beautiful data apps in hours, not weeks. All in pure Python. All for free.