DEV Community

Alessandro Marrella
Alessandro Marrella

Posted on • Originally published at alessandromarrella.com on

How to run and serve a webserver in Google Colab without ngrok

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

Enter fullscreen mode Exit fullscreen mode

Step 2: Scaffold a New Dagster Project#

!dagster project scaffold --name test

Enter fullscreen mode Exit fullscreen mode

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 &"
    ]
)

Enter fullscreen mode Exit fullscreen mode

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='/')

Enter fullscreen mode Exit fullscreen mode

you can also run it in an iframe with

output.serve_kernel_port_as_iframe(3000)

Enter fullscreen mode Exit fullscreen mode

Putting it all together#

You can check out a full example in this Colab Notebook

Top comments (0)