From 08db92250a0ad6379e8d38a9c3db6030a94bfa48 Mon Sep 17 00:00:00 2001 From: David Brochart Date: Fri, 29 Sep 2023 09:38:58 +0200 Subject: [PATCH] Align external kernel connections with jupyter-server (#352) --- jupyverse_api/jupyverse_api/kernels/__init__.py | 17 ++++++++++++++++- plugins/kernels/fps_kernels/main.py | 11 ++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/jupyverse_api/jupyverse_api/kernels/__init__.py b/jupyverse_api/jupyverse_api/kernels/__init__.py index 0aa848c2..ee79f73e 100644 --- a/jupyverse_api/jupyverse_api/kernels/__init__.py +++ b/jupyverse_api/jupyverse_api/kernels/__init__.py @@ -4,6 +4,7 @@ from fastapi import APIRouter, Depends, Request from jupyverse_api import Router, Config +from pydantic import Field from ..app import App from ..auth import Auth, User @@ -239,5 +240,19 @@ async def kernel_channels( class KernelsConfig(Config): default_kernel: str = "python3" - connection_path: Optional[str] = None + allow_external_kernels: bool = Field( + description=( + "Whether or not to allow external kernels, whose connection files are placed in " + "external_connection_dir." + ), + default=False, + ) + external_connection_dir: Optional[str] = Field( + description=( + "The directory to look at for external kernel connection files, if " + "allow_external_kernels is True. Defaults to Jupyter runtime_dir/external_kernels. " + "Make sure that this directory is not filled with left-over connection files." + ), + default=None, + ) require_yjs: bool = False diff --git a/plugins/kernels/fps_kernels/main.py b/plugins/kernels/fps_kernels/main.py index cd856a40..93cce3f7 100644 --- a/plugins/kernels/fps_kernels/main.py +++ b/plugins/kernels/fps_kernels/main.py @@ -12,6 +12,7 @@ from jupyverse_api.yjs import Yjs from jupyverse_api.app import App +from .kernel_driver.paths import jupyter_runtime_dir from .routes import _Kernels @@ -38,13 +39,17 @@ async def start( kernels = _Kernels(app, self.kernels_config, auth, frontend_config, yjs) ctx.add_resource(kernels, types=Kernels) - if self.kernels_config.connection_path is not None: - path = Path(self.kernels_config.connection_path) + if self.kernels_config.allow_external_kernels: + external_connection_dir = self.kernels_config.external_connection_dir + if external_connection_dir is None: + path = Path(jupyter_runtime_dir()) / "external_kernels" + else: + path = Path(external_connection_dir) task = asyncio.create_task(kernels.watch_connection_files(path)) yield - if self.kernels_config.connection_path is not None: + if self.kernels_config.allow_external_kernels: task.cancel() for kernel in kernels.kernels.values(): await kernel["server"].stop()