Expose streamlit app with Colab

How to use Ngrok to expose public IP as Steamlit web server

Nicola Landro
2 min readMar 21, 2022

Ngrok is a service that allow you to expose TCP, HTTP and HTTPS port outside your network without the necessity to have a public IP. In particular we will use pyngrok to expose a simply streamlit app.

How to work on Google Colab with Streamlit

So start a colab notebook and follow the guide.

Create Streamlit APP

We can start by installing streamlit and writing an app.py:

!pip install streamlitwith open('app.py', 'w') as f:
f.write('import streamlit as st\nst.title("Title")')

We can edit this file like in the figure by clicking it on colab file system.

Now we are ready to run it, and put the process in background:

!streamlit run app.py &>/dev/null&

Now the app is exposed at localhost:8501 inside your colab.

Public expose with Ngrok

Now to use that port we need pyngrok to expose outside the colab notebook.

Start registering on Ngrok ang copy the Key.
Now into the notebook you can install pyngrok and past your Key.

!pip install pyngrok==4.1.1
from pyngrok import ngrok
!ngrok authtoken <PASTE YOUR API KEY>
public_url = ngrok.connect(port='8501')
print(public_url)

Now it printed a public url where you can find your streamlit app.

(Remember that if you edit the app.py it automatically reload the app without need to restart)

Conclusions

Streamlit is a very good tool to create simplyest python web interfaces for our demo, and it is very most used in AI demos like on Spaces in Huggingface, but it can be used for any demo you want. It also have a simply authentication lib to protect the access. With that tecqnique we can expose to the world something that are on our computer or into a colab notebook, for example to use GPU in our demo.
For these exciting feature we must regrate the opensource community, so contribute you too and enjoy!

--

--

Nicola Landro

Linux user and Open Source fun. Deep learning PhD. , Full stack web developer, Mobile developer, cloud engineer and Musitian.