Skip to content

Commit

Permalink
support running local kernels alongside remote kernels
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmarkarl committed Dec 7, 2023
1 parent 023e524 commit 831e1cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
29 changes: 23 additions & 6 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def __init__(
authorizer=None,
identity_provider=None,
kernel_websocket_connection_class=None,
local_kernel_websocket_connection_class=None,
):
"""Initialize a server web application."""
if identity_provider is None:
Expand Down Expand Up @@ -277,6 +278,7 @@ def __init__(
authorizer=authorizer,
identity_provider=identity_provider,
kernel_websocket_connection_class=kernel_websocket_connection_class,
local_kernel_websocket_connection_class=local_kernel_websocket_connection_class,
)
handlers = self.init_handlers(default_services, settings)

Expand All @@ -301,6 +303,7 @@ def init_settings(
authorizer=None,
identity_provider=None,
kernel_websocket_connection_class=None,
local_kernel_websocket_connection_class=None,
):
"""Initialize settings for the web application."""
_template_path = settings_overrides.get(
Expand Down Expand Up @@ -383,6 +386,7 @@ def init_settings(
"identity_provider": identity_provider,
"event_logger": event_logger,
"kernel_websocket_connection_class": kernel_websocket_connection_class,
"local_kernel_websocket_connection_class": local_kernel_websocket_connection_class,
# handlers
"extra_services": extra_services,
# Jupyter stuff
Expand Down Expand Up @@ -1507,6 +1511,12 @@ def _default_session_manager_class(self) -> t.Union[str, type[SessionManager]]:
help=_i18n("The kernel websocket connection class to use."),
)

local_kernel_websocket_connection_class = Type(
klass=BaseKernelWebsocketConnection,
config=True,
help=_i18n("The local kernel websocket connection class to use."),
)

@default("kernel_websocket_connection_class")
def _default_kernel_websocket_connection_class(
self,
Expand All @@ -1515,6 +1525,12 @@ def _default_kernel_websocket_connection_class(
return "jupyter_server.gateway.connections.GatewayWebSocketConnection"
return ZMQChannelsWebsocketConnection

@default("local_kernel_websocket_connection_class")
def _default_local_kernel_websocket_connection_class(
self,
) -> t.Union[str, type[ZMQChannelsWebsocketConnection]]:
return ZMQChannelsWebsocketConnection

config_manager_class = Type(
default_value=ConfigManager,
config=True,
Expand Down Expand Up @@ -1885,8 +1901,8 @@ def init_configurables(self) -> None:
self.gateway_config = GatewayClient.instance(parent=self)

if not issubclass(
self.kernel_manager_class,
AsyncMappingKernelManager,
self.kernel_manager_class,
AsyncMappingKernelManager,
):
warnings.warn(
"The synchronous MappingKernelManager class is deprecated and will not be supported in Jupyter Server 3.0",
Expand All @@ -1895,8 +1911,8 @@ def init_configurables(self) -> None:
)

if not issubclass(
self.contents_manager_class,
AsyncContentsManager,
self.contents_manager_class,
AsyncContentsManager,
):
warnings.warn(
"The synchronous ContentsManager classes are deprecated and will not be supported in Jupyter Server 3.0",
Expand Down Expand Up @@ -1947,8 +1963,8 @@ def init_configurables(self) -> None:
identity_provider_kwargs = {"parent": self, "log": self.log}

if (
self.login_handler_class is not LoginHandler
and self.identity_provider_class is PasswordIdentityProvider
self.login_handler_class is not LoginHandler
and self.identity_provider_class is PasswordIdentityProvider
):
# default identity provider, non-default LoginHandler
# this indicates legacy custom LoginHandler config.
Expand Down Expand Up @@ -2101,6 +2117,7 @@ def init_webapp(self) -> None:
authorizer=self.authorizer,
identity_provider=self.identity_provider,
kernel_websocket_connection_class=self.kernel_websocket_connection_class,
local_kernel_websocket_connection_class=self.local_kernel_websocket_connection_class,
)
if self.certfile:
self.ssl_options["certfile"] = self.certfile
Expand Down
16 changes: 13 additions & 3 deletions jupyter_server/services/kernels/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ def kernel_websocket_connection_class(self):
"""The kernel websocket connection class."""
return self.settings.get("kernel_websocket_connection_class")

@property
def local_kernel_websocket_connection_class(self):
"""The kernel websocket connection class."""
return self.settings.get("local_kernel_websocket_connection_class")

def set_default_headers(self):
"""Undo the set_default_headers in JupyterHandler
Expand All @@ -44,9 +49,14 @@ async def pre_get(self):
raise web.HTTPError(403)

kernel = self.kernel_manager.get_kernel(self.kernel_id)
self.connection = self.kernel_websocket_connection_class(
parent=kernel, websocket_handler=self, config=self.config
)
if kernel.kernel_name == "python3":
self.connection = self.local_kernel_websocket_connection_class(
parent=kernel, websocket_handler=self, config=self.config
)
else:
self.connection = self.kernel_websocket_connection_class(
parent=kernel, websocket_handler=self, config=self.config
)

if self.get_argument("session_id", None):
self.connection.session.session = self.get_argument("session_id")
Expand Down

0 comments on commit 831e1cf

Please sign in to comment.