How to Launch Streamlit App from Google Colab Notebook

Hello everyone!

I recently came across a way to launch a streamlit app from a Google Colab Notebook and wanted to share this with you.

Google Colab is a free cloud service for machine learning and artificial intelligence made available by Google. It provides a Jupyter notebook environment which can be used to code, debug, analyze data and more. It is a great platform for quickly prototyping and experimenting, making development much faster and efficient.

Run a Streamlit App on Google Colab Notebook
Launch google Colab, and create a new Notebook https://colab.research.google.com/.

Install Streamlit library

!pip install -q streamlit

Create a streamlit app example

%%writefile app.py

import streamlit as st

st.write('Hello, *World!* :sunglasses:')

Install localtunnel to serve the Streamlit app

!npm install localtunnel

Run the Streamlit app in the background

!streamlit run app.py &>/content/logs.txt &

Expose the Streamlit app on port 8501
Then just click in the url showed.

!npx localtunnel --port 8501

If you find this helpful Select :heart: on the post. Please feel free to leave a comment if you have any questions.

Thank you! :wave:

13 Likes


I get this everytime. What should i do now?

2 Likes


4 Likes

Here is a Google Colab Example with Streamlit:

In this Colab Example there is a code line to create a log.txt file: !streamlit run /content/app.py &>/content/logs.txt &
Within the log.txt file there are the URLs which can be used for creating the tunel. Copy only the External URL IP without the Port in the Endpoint IP field as discribed above.
Hope this is helpful :wink:

4 Likes

very good

Run the below cell before running your streamlit app on localrunnel

import urllib
print("Password/Enpoint IP for localtunnel is:",urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip("\n"))

This will give you the EnpointIP your Internet is running on. The copy that IP & paste it into the Friendly Reminder page.
Then you should be directed into your streamlit web app page.

This is a great way to test your web app before actual deployment. I hope that helps

5 Likes

Hi, this works great! Thanks a lot!

Just one question. In the streamlit app, when I try to use a function defined in a different cell of the Notebook it fails.

NameError: name “name_of_the_function” is not defined

Any idea about how could I solve this?

Thanks!

Hi JavierF,
you need to have all the function definitions that are necessary for your streamlit inside your app.py script.
(you can also define them outside the script, but in that case you need to import your module where the function is defined into the app.py)
Hope that works for you, have a great day

I have been using this solution for the past days without any issues. But today it is not working anymore. You click on the url and it remains loading forever… I tried several browsers, and installed previous versions of streamlit.

How can I solve this problem ??

Hi, this solution is not work anymore, when click Submit button on Friendly Remider page, it load infinite

Still not working in Colab for the same reason @Hoang_Ha_Dang mentioned… any progress?

Hello people,

A few months ago I added a Colab to my project, moseca, since will be better to use with GPU, you can find the “Open in Colab” button in README.

Feel free to copy it as a template for your projects!

1 Like

Here is an updated version with Cloudflare tunnel instead. Thanks, @HubertW for the inspiration.

Hallo, I wanna ask about solve my problem.
I was follow your instruction step by step. but I got a problem


I got 404 on my webpage… How I can solve this problem?

Best Regrads,
Vibes

it worked before but I do not know why it does not work anymore. I have tried different browsers and it always showed loading and finally, it broke.
If anyone could give some advice or suggestions, thanks a lot.

thanks soooo much for posting this. I’ve been trying to figure this out for a hot minute.

I tried adding the --server.address=localhost argument, and it’s working

!streamlit run app.py --server.address=localhost &>/content/logs.txt &

1 Like

Hi @Hoang_Ha_Dang here is the solution which I tested on 8/01/2024.

import urllib
print("Password/Enpoint IP for localtunnel is:",urllib.request.urlopen('https://ipv4.icanhazip.com').read().decode('utf8').strip("\n"))

The IP you get it from this just paste it on the security page of the local tunnel.

1 Like

That is working perfectly!
Thank you :slight_smile:

Brilliant, Thanks :heart: