How to create and launch a Streamlit app directly from Google Colab

Ifeanyi Nneji
FAUN — Developer Community 🐾
3 min readJun 1, 2022

--

Image from Streamlit

Streamlit is an open source app framework in Python language. It helps in creating web apps for data science and machine learning in a short time and is compatible with major Python libraries such as scikit-learn, Keras, PyTorch, SymPy(latex), NumPy, pandas, Matplotlib etc.

So I was working on a project with the ResNet50 image classification model in Google colab and I wanted to create a Streamlit app.

I didn’t want to download the files directly to my physical machine just so I could make the app so I searched for ways on how to do it directly from Google colab and here’s how to do it:

  1. Install Streamlit in your instance of Google colab by running:
!pip install -q streamlit

2. Sign up for a ngrok account: http://ngrok.com

3. After signing up hit the sidebar and you’d see something like this in the picture.

Image by Author

Click on “Your Authtoken” then copy your token.

4. Then run this in Google colab:

 !./ngrok authtokens your_token 

Replace your_token with your copied token.

5. Run this next to install the pyngrok package:

!pip install pyngrok 

6. Run this next:

from pyngrok import ngrok 
public_url = ngrok.connect(port=’8501')
public_url

7. You can now proceed and create a .py file with the contents of your streamlit app.

To create the .py file run this in the next cell:

%%writefile streamlit_app.py 
import streamlit as st
st.markdown(""" This is a Streamlit App """)

In the same cell as the command above you can write all the contents of the app and then when that cell is run the streamlit_app.py file is created in your Google colab instance.

After all that you can now run the app from Google colab by running this command in another cell:

!streamlit run /content/streamlit_app.py & npx localtunnel — port 8501 

And the app should be up and running like in the image below.

Streamlit Running in Google Colab with Localtunnel

Advantages of Creating a Streamlit App through Google colab

1. It saves time and resources because you don't have to start installing dependencies on your local machine just to make the app run.

2. It makes testing the app easier because you can adjust and work with your model directly from Google colab and then make inference from the Streamlit app.

If you want to check out what I was working on the project link and GitHub links are provided below:

App Link : https://share.streamlit.io/nneji123/deep-learning-object-recognition-with-resnet50/main

GitHub Repository: Nneji123/Deep-Learning-Object-Recognition-with-ResNet50

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Developers: Learn and grow by keeping up with what matters, JOIN FAUN.

--

--