Skip to content

Commit

Permalink
Align external kernel connections with jupyter-server
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Sep 25, 2023
1 parent f631264 commit 94f3e9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
16 changes: 15 additions & 1 deletion jupyverse_api/jupyverse_api/kernels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -239,5 +240,18 @@ 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
11 changes: 8 additions & 3 deletions plugins/kernels/fps_kernels/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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()

0 comments on commit 94f3e9f

Please sign in to comment.