-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR: Extract Jupyter kernel creation into independent function.
- Loading branch information
Showing
2 changed files
with
44 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { ServerConnection } from "@jupyterlab/services"; | ||
import type { IKernelConnection, IKernelOptions } from "@jupyterlab/services/lib/kernel/kernel"; | ||
import { type Resource, type ResourceReturn, createResource, onCleanup } from "solid-js"; | ||
|
||
type ResourceRefetch<T> = ResourceReturn<T>[1]["refetch"]; | ||
|
||
type ServerSettings = Parameters<typeof ServerConnection.makeSettings>[0]; | ||
|
||
/** Create a Jupyter kernel as a Solid resource. | ||
Returns a handle to the resource and a callback to restart the kernel. The | ||
kernel is automatically shut down when the component is unmounted. | ||
*/ | ||
export function createKernel( | ||
serverOptions: ServerSettings, | ||
kernelOptions: IKernelOptions, | ||
): [Resource<IKernelConnection>, ResourceRefetch<IKernelConnection>] { | ||
const [kernel, { refetch: restartKernel }] = createResource(async () => { | ||
const jupyter = await import("@jupyterlab/services"); | ||
|
||
const serverSettings = jupyter.ServerConnection.makeSettings(serverOptions); | ||
|
||
const kernelManager = new jupyter.KernelManager({ serverSettings }); | ||
const kernel = await kernelManager.startNew(kernelOptions); | ||
|
||
return kernel; | ||
}); | ||
|
||
onCleanup(() => kernel()?.shutdown()); | ||
|
||
return [kernel, restartKernel]; | ||
} |