Using Jupyter Server’s GatewayKernelManager#

Another way to expose other Jupyter applications like nbclient or papermill to remote kernels is to use the GatewayKernelManager (and, implicitly, GatewayKernelClient) classes that are embedded in Jupyter Server.

These classes essentially emulate the lower level KernelManager and KernelClient classes but forward their requests to/from a configured gateway server. Their necessary configuration for interacting with the gateway server is set on the GatewayClient configurable.

This allows for the hosting application to remain outside the resource-managed cluster since the kernel is actually being managed by the target gateway server.

So, using the previous example, one my have…

import nbformat
from nbclient import NotebookClient
from jupyter_server.gateway.gateway_client import GatewayClient
from jupyter_server.gateway.managers import GatewayKernelManager

with open("my_notebook.ipynb") as fp:
    test_notebook = nbformat.read(fp, as_version=4)

# Set any other gateway-specific parameters on the GatewayClient (singleton) instance
gw_client = GatewayClient.instance()
gw_client.url = "http://my-gateway-server.com:8888"

client = NotebookClient(nb=test_notebook, kernel_manager_class=GatewayKernelManager)
client.execute(kernel_name='my_remote_kernel')

In this case, my_remote_kernel’s kernel specification file actually resides on the Gateway server. NotebookClient will think its talking to local KernelManager and KernelClient instances, when, in actuality, they are forwarding requests to (and getting response from) the Gateway server at ‘http://my-gateway-server.com:8888’.