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
Install Streamlit library
!pip install -q streamlit
Create a streamlit app example
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
!npx localtunnel --port 8501
If you find this helpful Select on the post. Please feel free to leave a comment if you have any questions.
I get this everytime. What should i do now?
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
Run the below cell before running your streamlit app on localrunnel
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
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?
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?
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!
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?
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.