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

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.

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.

1_5HrNFeTE2MtZDf8mAtEE8Q.png

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

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


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!

Avatar

Ifeanyi Nneji

Junior Data Scientist, Zummit Africa

@neji_14
Aspiring Data Scientist | Machine Learning Enthusiast
Stats
53

Influence

4k

Total Hits

1

Posts