How to Run Streamlit Apps From Google’s Colab

The simplicity that streamlit offers for building apps is so cool and useful. But sometimes you may be constrained by lack of compute or incompatible  working environment on your local computer. In most cases Colab solves that problem. But what if you want to share your streamlit app with others from Colab.

In this tutorial we will be learning how to run or deploy our streamlit app from Google’s Colaboratory.

Let us start.  This is a simple overview of what we will be doing.

Create An Account on Ngrok

Ngrok is a powerful solution  for providing secure tunnels from your local system to the public. So the first task is to signup to and create a free account.This will give you access to several features.

Get Your Authentication Tokens

To use ngrok,you will need to an authentication token which can be found on your dashboard of your ngrok account ( ).

This is what you will use to authenticate when working with ngrok. You can find your authtokens below the ‘Connect Your Account’ like this

./ngrok authtokens xxxxxxxxxxxxxxxxxxxx

This is what you will use to connect your account.


Working Inside Colab

In your colab instance you can install the necessary packages you need for your app. But the two most important for running streamlit is ofcourse streamlit and then pyngrok.

Since you are working inside Colab a.k.a. Jupyter Notebooks in the Cloud with Fast Compute, you will need to use the ! sign in front of any linux/unix command for installing and running unix based command. Hence in our case we can install streamlit and pyngrok as below


!pip install streamlit

!pip install pyngrok
Write Your App to Your Colab Sandbox

There are several ways to run your script from colab but the simplest is to write your code to a file and save it to your current instance using the %%writefile .  The %%writefile will write the current cell below to the filename specified.

In our case

import streamlit as st
PAGE_CONFIG = {"page_title":"","page_icon":":smiley:","layout":"centered"}

def main():
	st.title("Awesome Streamlit for ML")
	st.subheader("How to run streamlit from colab")

	menu = ["Home","About"]
	choice = st.sidebar.selectbox('Menu',menu)
	if choice == 'Home':
		st.subheader("Streamlit From Colab")	

if __name__ == '__main__':

To check if the file was written to your current colab sandbox, you can use the list command eg



Start Your App

Now that we have written our app, we can now start our app like we would have done if we were running it locally.  But the caveat is to run it in the background so that if the cell finish running, our app will continue to run as a background process behind.

We can do so using nohub or disown.

!nohub streamlit run &

In some cases if nohub fails to work you can use the basic linux command for running processes in the background.

!streamlit run &>/dev/null&

This will start streamlit on the normal default port of 8501 which we will use for the next section in pyngrok.

Create A Secure Tunnel Using Pyngrok

As we learnt earlier ngrok allows us to create a secure tunnel for accessing our local apps and webhooks. We will be using pyngrok which acts as a python wrapper around ngrok. Installing pyngrok both offers the python package as well as the CLI hence you can use any as you wish.

To create our tunnel we will be using pyngrok and passing in the port from streamlit (ie 8501) .

from pyngrok import ngrok
# Setup a tunnel to the streamlit port 8501
public_url = ngrok.connect(port='8501')

This will generate a public URL (something like ) that your app will be running on.

Viola our app works. We have successfully deployed our streamlit app from Google’s Colab using Ngrok.


Shutting Down Your App

In case you want to shut down your app, you will have to first check the process streamlit is running on and then kill that process.

!pgrep streamlit


!ps -eaf | grep streamlit

Then kill the process

kill 445

After that you can shutdown ngrok from python


You can also check out the video tutorial below


Thank you for your time
Jesus Saves
By Jesse.E.Agbe(JCharis)

Leave a Comment

Your email address will not be published. Required fields are marked *