Skip to content

Commit

Permalink
REFACTOR: Extract Jupyter kernel creation into independent function.
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters committed Dec 6, 2024
1 parent f2727c1 commit 0c0c1d7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
34 changes: 12 additions & 22 deletions packages/frontend/src/stdlib/analyses/decapodes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IReplyErrorContent } from "@jupyterlab/services/lib/kernel/messages";
import { For, Match, Show, Switch, createMemo, createResource, onCleanup } from "solid-js";
import { For, Match, Show, Switch, createMemo, createResource } from "solid-js";
import { isMatching } from "ts-pattern";

import type { DiagramAnalysisProps } from "../../analysis";
Expand All @@ -22,6 +22,7 @@ import type { ModelJudgment, MorphismDecl } from "../../model";
import type { DiagramAnalysisMeta } from "../../theory";
import { uniqueIndexArray } from "../../util/indexing";
import { PDEPlot2D, type PDEPlotData2D } from "../../visualization";
import { createKernel } from "./jupyter";

import Loader from "lucide-solid/icons/loader";
import RotateCcw from "lucide-solid/icons/rotate-ccw";
Expand All @@ -31,19 +32,14 @@ import "./decapodes.css";
import "./simulation.css";

/** Configuration for a Decapodes analysis of a diagram. */
export type DecapodesContent = JupyterSettings & {
export type DecapodesContent = {
domain: string | null;
mesh: string | null;
initialConditions: Record<string, string>;
plotVariables: Record<string, boolean>;
scalars: Record<string, number>;
};

type JupyterSettings = {
baseUrl?: string;
token?: string;
};

export function configureDecapodes(options: {
id?: string;
name?: string;
Expand Down Expand Up @@ -73,21 +69,15 @@ export function configureDecapodes(options: {
*/
export function Decapodes(props: DiagramAnalysisProps<DecapodesContent>) {
// Step 1: Start the Julia kernel.
const [kernel, { refetch: restartKernel }] = createResource(async () => {
const jupyter = await import("@jupyterlab/services");

const serverSettings = jupyter.ServerConnection.makeSettings({
baseUrl: props.content.baseUrl ?? "http://127.0.0.1:8888",
token: props.content.token ?? "",
});

const kernelManager = new jupyter.KernelManager({ serverSettings });
const kernel = await kernelManager.startNew({ name: "julia-1.11" });

return kernel;
});

onCleanup(() => kernel()?.shutdown());
const [kernel, restartKernel] = createKernel(
{
baseUrl: "http://127.0.0.1:8888",
token: "",
},
{
name: "julia-1.11",
},
);

// Step 2: Run initialization code in the kernel.
const startedKernel = () => (kernel.error ? undefined : kernel());
Expand Down
32 changes: 32 additions & 0 deletions packages/frontend/src/stdlib/analyses/jupyter.ts
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];
}

0 comments on commit 0c0c1d7

Please sign in to comment.