To use pyngrok in a Google Colab notebook, first install it as a dependency using !pip install pyngrok.
SSH Example
To expose an SSH server running in Colab, use ngrok.connect("22", "tcp"). You will need to provide your ngrok authtoken via conf.get_default().auth_token.
HTTP Example
To expose a web server (like Flask) from Colab, use ngrok.connect(port).public_url. It is recommended to update your application's base URL or webhook configurations with this public_url so that external services can reach your local server.
import getpass
from pyngrok import ngrok, conf
# Set authtoken
print("Enter your authtoken...")
conf.get_default().auth_token = getpass.getpass()
# Open a TCP ngrok tunnel to the SSH server
connection_string = ngrok.connect("22", "tcp").public_url
ssh_url, port = connection_string.strip("tcp://").split(":")
print(f" * ngrok tunnel available, access with `ssh root@{ssh_url} -p{port}`")