Today I learned how to run a webserver in Google Colab, without needing external services like ngrok. I'm using Dagster here as an example but any webserver should work.

If you just want to create a public url for a webserver running in colab you can jump to step 4

Step 1: Install the Required Packages

!pip install dagster dagster-webserver

Step 2: Scaffold a New Dagster Project

!dagster project scaffold --name test

Step 3: Run Dagster in the Background

To run Dagster's webserver, you'll need to start it in the background. This can be done using Python's subprocess module. The following code navigates to the project directory and starts the webserver on port 3000:

import subprocess

subprocess.Popen(
    [
        "bash",
        "-c",
        "cd /content/test && dagster-webserver -h 0.0.0.0 -p 3000 &"
    ]
)

Step 4: Create a Public URL for the Webserver

Google Colab provides a way to create a public URL for your webserver. Use the output.serve_kernel_port_as_window function to expose the webserver running on port 3000.

This does the real magic. Note that the URL is authenticated, and only the user running the notebook has access to the webserver.

from google.colab import output
output.serve_kernel_port_as_window(3000, path='/')

you can also run it in an iframe with

output.serve_kernel_port_as_iframe(3000)

Putting it all together

You can check out a full example in this Colab Notebook