The simplicity that streamlit offers for building apps is so cool and useful. But sometimes you may be constrained by lack of compute or incompatible working environment on your local computer.

In most cases Colab solves that problem. But what if you want to share your streamlit app with others from Colab.

In this tutorial we will be learning how to run or deploy our streamlit app from Google’s Colaboratory.

Let us start. This is a simple overview of what we will be doing.

Create An Account on Ngrok

Ngrok is a powerful solution for providing secure tunnels from your local system to the public. So the first task is to signup to ngrok.com and create a free account.This will give you access to several features.

Get Your Authentication Tokens

To use ngrok,you will need to an authentication token which can be found on your dashboard of your ngrok account ( https://dashboard.ngrok.com/get-started/setup ).

This is what you will use to authenticate when working with ngrok. You can find your authtokens below the ‘Connect Your Account’ like this

./ngrok authtokens xxxxxxxxxxxxxxxxxxxx

This is what you will use to connect your account.

Working Inside Colab

In your colab instance you can install the necessary packages you need for your app. But the two most important for running streamlit is ofcourse streamlit and then pyngrok.

Since you are working inside Colab a.k.a. Jupyter Notebooks in the Cloud with Fast Compute, you will need to use the ! sign in front of any linux/unix command for installing and running unix based command. Hence in our case we can install streamlit and pyngrok as below

1
2
!pip install streamlit
!pip install pyngrok

Write Your App to Your Colab Sandbox

There are several ways to run your script from colab but the simplest is to write your code to a file and save it to your current instance using the %%writefile . The %%writefile will write the current cell below to the filename specified.

In our case

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
%%writefile app.py
import streamlit as st
PAGE_CONFIG = {"page_title":"StColab.io","page_icon":":smiley:","layout":"centered"}
st.beta_set_page_config(**PAGE_CONFIG)
def main():
	st.title("Awesome Streamlit for ML")
	st.subheader("How to run streamlit from colab")
	menu = ["Home","About"]
	choice = st.sidebar.selectbox('Menu',menu)
	if choice == 'Home':
		st.subheader("Streamlit From Colab")	
if __name__ == '__main__':
	main()

To check if the file was written to your current colab sandbox, you can use the list command eg

1
!ls

Start Your App

Now that we have written our app, we can now start our app like we would have done if we were running it locally. But the caveat is to run it in the background so that if the cell finish running, our app will continue to run as a background process behind.

We can do so using nohub or disown.

1
!nohub streamlit run app.py &

In some cases if nohub fails to work you can use the basic linux command for running processes in the background.

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

This will start streamlit on the normal default port of 8501 which we will use for the next section in pyngrok.

Create A Secure Tunnel Using Pyngrok

As we learnt earlier ngrok allows us to create a secure tunnel for accessing our local apps and webhooks. We will be using pyngrok which acts as a python wrapper around ngrok. Installing pyngrok both offers the python package as well as the CLI hence you can use any as you wish.

To create our tunnel we will be using pyngrok and passing in the port from streamlit (ie 8501) .

1
2
3
4
from pyngrok import ngrok
# Setup a tunnel to the streamlit port 8501
public_url = ngrok.connect(port='8501')
public_url

This will generate a public URL (something like https://b546sh556d.ngrok.io ) that your app will be running on.

Viola our app works. We have successfully deployed our streamlit app from Google’s Colab using Ngrok.

Shutting Down Your App

In case you want to shut down your app, you will have to first check the process streamlit is running on and then kill that process.

1
!pgrep streamlit

or

1
!ps -ef | grep streamlit

Then kill the process

1
!kill 445 # process id

After that you can shutdown ngrok from python

1
ngrok.kill()

Reference

1、 https://medium.com/@jcharistech/how-to-run-streamlit-apps-from-colab-29b969a1bdfc

打赏

微信 微信 支付宝 支付宝
万分感谢