Skip to main content

Browser execution and network access

Trusted input

Notebook cells run as JavaScript in the page that hosts the widget. Treat imported Notebook Kit HTML, ObservableHQ notebooks, and their remote modules as trusted code because they can use the page's browser privileges.

JavaScript evaluates in the browser page that hosts the view. Notebook Kit supplies the cell languages and standard library. Python constructs the definition and sends values to its browser runtime.

Notebook Kit's node, python, and r cells read precomputed interpreter results from .observable/cache paths served by the host page. Calculate with the host Python kernel through Python variables.

Runtime profiles

The notebook source selects the runtime profile before a view starts. The profile is fixed for the notebook session and inherited by each view.

SourceProfile
Python-authored cellsNotebook Kit
Notebook Kit HTMLNotebook Kit
Observable model with stdlib: "1" or classic nodes with no library declarationObservable
Observable model with stdlib: "2" or native cells with no library declarationNotebook Kit
HTML serialized by to_notebook_html()Preserved source profile

The Notebook Kit profile uses the builtins exported by Notebook Kit. The Observable profile uses the classic Observable standard library, which adds classic names such as require and DOM. Both profiles receive view-scoped FileAttachment, document, width, and dark values from pyobservablejs.

Notebook Kit SQL cells retain native database results, including columnar Apache Arrow tables. Classic Observable imports expose row arrays, so dependent cells can call methods such as .map(). Their queries follow the cell lifecycle and can stream results.

Builtins

The selected profile resolves builtins when cells reference them.

BuiltinContract
InputsCreates controls, tables, and other input elements. Pass an input to view(...) to expose its changing value.
PlotCreates charts from data and mark specifications. Plot.plot() and a mark's .plot() return browser elements.
sqlBuilds composable SQL query fragments. A query's .query(db) executes it against a database client.
html, mdCreates reactive HTML and Markdown DOM values from tagged templates.
GeneratorsProvides input, observe, and queue in both profiles. Other methods come from the selected standard library.
MutableCreates a reactive source with a .value getter and setter. Consumers rerun after the value changes.
widthYields the notebook root content width through Notebook Kit’s Generators.width(root). The first value arrives after browser layout, then updates after resize.
darkYields whether the notebook root uses a dark color scheme. Theme changes rerun dependent cells.

The Notebook Kit profile also exposes sample datasets such as aapl, cars, and penguins. Runtime-owned builtin names cannot be Python variables. See the naming rules in Variables and serialization.

Module imports and the network

Standard JavaScript cells can import an npm package or browser module:

obs.js(
"""
import {format} from "npm:d3-format@3";

const compact = format(".2s");
display(compact(42000));
"""
)

External libraries, sample datasets, module imports, and source notebooks can require browser network access. Notebook Kit resolves npm: specifiers from jsDelivr, a package delivery network, at render time. The page's content security policy, which limits browser scripts and network requests, must permit every package, data, and module origin used by its cells. To carry local source and data with a widget, embed modules with rewrite_imports=True and data with embed_file_attachments=True. Library and dataset builtins can still fetch remote resources. See Add files and local modules.

Network access happens at two distinct times:

WhenWhatWhere it runs
Notebook constructionfrom_observablehq fetches original notebook source. Local files are read and embedded.Python
View rendernpm imports, URL-backed attachments, remote datasets, imported notebooks.Browser

Promises, generators, and invalidation

Notebook Kit tracks asynchronous values as part of the graph.

ValueEvaluationInvalidation
PromiseDependent cells wait for the resolved value.A stale resolution is ignored. The operation continues unless the cell cancels it.
Generator or async generatorThe first yield defines the value. Later yields rerun dependent cells.The runtime calls the generator's return() method.
invalidationProvides a promise for the current cell evaluation.Resolves when the evaluation is invalidated or its runtime is disposed.

Use invalidation to release resources owned by a cell. See Display views for the corresponding Python lifecycle.