How to Run Streamlit Apps From Colab
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
|
|
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
|
|
To check if the file was written to your current colab sandbox, you can use the list command eg
|
|
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.
|
|
In some cases if nohub fails to work you can use the basic linux command for running processes in the background.
|
|
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) .
|
|
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.
|
|
or
|
|
Then kill the process
|
|
After that you can shutdown ngrok from python
|
|
Reference
1、 https://medium.com/@jcharistech/how-to-run-streamlit-apps-from-colab-29b969a1bdfc
打赏
微信 | 支付宝 |
---|---|
万分感谢 |
- 原文作者:冷眼
- 原文链接:https://cold-eye.github.io/post/python-streamlit-colab/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。