Skip to content

Allow instantiation of multiple notebooks inside a single scope #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/runtime/define.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {Variable, VariableDefinition} from "@observablehq/runtime";
import type {Module, Variable, VariableDefinition} from "@observablehq/runtime";
import type {DisplayState} from "./display.js";
import {clear, display, observe} from "./display.js";
import {main} from "./index.js";
import {input} from "./stdlib/generators/index.js";
import {Mutator} from "./stdlib/mutable.js";

Expand Down Expand Up @@ -31,7 +30,7 @@ export type Definition = {
assets?: Map<string, string>;
};

export function define(state: DefineState, definition: Definition, observer = observe): void {
export function define(main: Module, state: DefineState, definition: Definition, observer = observe): void {
const {id, body, inputs = [], outputs = [], output, autodisplay, autoview, automutable} = definition;
const variables = state.variables;
const v = main.variable(observer(state, definition), {shadow: {}});
Expand Down
30 changes: 25 additions & 5 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
import type {Module} from "@observablehq/runtime";
import {Runtime} from "@observablehq/runtime";
import type {DefineState, Definition} from "./define.js";
import {define as _define} from "./define.js";
import type {observe} from "./display.js";
import {fileAttachments} from "./stdlib/fileAttachment.js";
import {library} from "./stdlib/index.js";

export * from "./define.js";
export type {DefineState, Definition} from "./define.js";
export * from "./display.js";
export * from "./inspect.js";
export * from "./stdlib/index.js";

export type * from "./stdlib/databaseClient.js";
export type * from "./stdlib/fileAttachment.js";
export {DatabaseClient} from "./stdlib/databaseClient.js";
export type * from "./stdlib/fileAttachment.js";
export {FileAttachment, registerFile} from "./stdlib/fileAttachment.js";

export const runtime = Object.assign(new Runtime({...library, __ojs_runtime: () => runtime}), {fileAttachments});
export const main = (runtime as typeof runtime & {main: Module}).main = runtime.module();
export class NotebookRuntime {
readonly runtime: Runtime & {fileAttachments: typeof fileAttachments};
readonly main: Module;

constructor(builtins = library) {
const runtime = new Runtime({...builtins, __ojs_runtime: () => runtime});
this.runtime = Object.assign(runtime, {fileAttachments});
this.main = runtime.module();
}

define(state: DefineState, definition: Definition, observer?: typeof observe): void {
_define(this.main, state, definition, observer);
}
}

const defaultNotebook = new NotebookRuntime();

export const runtime = defaultNotebook.runtime;
export const main = defaultNotebook.main;
export const define = defaultNotebook.define.bind(defaultNotebook);

main.constructor.prototype.defines = function (this: Module, name: string): boolean {
return (
Expand Down