Use ngrok with secret key on Google Colab

0xkoji - Nov 9 '23 - - Dev Community

What is ngrok?

ngrok combines your reverse proxy, firewall, API gateway, and global load balancing to deliver apps and APIs.

https://ngrok.com/

Simply put, ngrok makes it easy to publish a web application running locally with a single command.
This is very useful to build prototyping.

For Google Colab, using ngrok would make things easy if you want to run an open source LLM and use it via an API server (like FastAPI, Flask etc).

How to use ngrok on Google Colab

There are 3 steps to use ngrok with Google Colab secret key.

  1. Download ngrok
  2. Set secret key
  3. Load secret key

Step1. Download ngrok

This step is pretty straightforward.
Download zip file for linux and unzip it.

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip -o ngrok-stable-linux-amd64.zip
Enter fullscreen mode Exit fullscreen mode

Step2. Set secret key

Click key icon in the sidebar and click Add a new secret.
You will need to fill two text box. One is the key name and you can name whatever you want and the other is the key value or token. Then you need to click a toggle button to enable the key.

Image description

Step3. Load secret key

The following code is very simple, load the secret key and pass it to ngrok via ngrok's python package for authentication.

import os
from google.colab import userdata
from pyngrok import conf
os.environ["NGROK"] = userdata.get("NGROK")
conf.get_default().auth_token = os.environ["NGROK"]
Enter fullscreen mode Exit fullscreen mode

These three steps will work for things that you need to pass a secret key or token on Google Colab.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .