Join us

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:

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:

Replace your_token with your copied token.

5. Run this next to install the pyngrok package:

6. Run this next:

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:

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:

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


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


1 year, 9 months ago  -  @radhika-keni   

@neji_14 :Thanks so mcuh for the fantastic post .
A quick question though - How to we make sure the colab session does not terminate ? If the colab session terminates , the steamlit app hosted through colab is no longer up and serving requests ! Your response would be much appreciated !!

😀 | â˜č     Reply

Start blogging about your favorite technologies, reach more readers and earn rewards!

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
User Popularity
195

Influence

20k

Total Hits

1

Posts