Rich documents with live Jupyter cells, for accessible and reproducible data analysis.
Datalayer Docs is a document editor built on Lexical:
headings, lists, tables, equations, diagrams, images, video β and Jupyter
cells that run code on a kernel and keep their outputs in the text. It was
@datalayer/jupyter-lexical (and jupyter_lexical on PyPI) in the
jupyter-ui repository; it lives
here now, beside Cells and
Decks, with the same two halves:
| Package | Registry | What it is |
|---|---|---|
@datalayer/docs |
npm | The editor, its extensions and plug-ins, the conversions, the agent tools, and the Reactor plugin |
datalayer_docs |
PyPI | The datalayer docs command, the documents API, the Reactor extension, the Jupyter server extension |
npm install @datalayer/docs
pip install datalayer_docs # the command, the API and the Reactor extension
pip install 'datalayer_docs[jupyter]' # ... and the Jupyter server extensionMoving from @datalayer/jupyter-lexical: replace the dependency and the
import specifier; the exports are the same (Editor,
JupyterLexicalExtension, the nodes, the plug-ins, the toolsβ¦), and deep
imports keep their paths (@datalayer/docs/lib/..., @datalayer/docs/style/...).
Installed beside the Datalayer CLI, datalayer_docs adds a docs group to
the datalayer command (the datalayer.cli entry point); on its own the
docs script runs the same group.
datalayer docs about # what Docs is, and its version
datalayer docs new notes.lexical # the starter document, as a file
datalayer docs new research/notes # ... or in the store (~/.datalayer/docs, or $DATALAYER_DOCS_DIR)
datalayer docs ls # the documents of the store
datalayer docs show research/notes # one document, as JSON
datalayer docs text research/notes # its plain text
datalayer docs serve [notes.lexical] # the API and the interface, one origin (:8798)
datalayer docs delete research/notesdocs serve hosts a Reactor platform with the docs plugin: the documents API
under /documents and, when it has been built (make build-app, or a wheel
that carries it), the interface at /.
@datalayer/docs/lib/plugin puts documents in any Reactor shell: a list in
the sidebar slot with a New document button, the open document in the main
slot (the package's Editor, saving each change back), a "Docs" view in the
shell's selector, and commands β docs.list, docs.new, docs.open,
docs.delete and the data commands docs.listDocs, docs.getDoc,
docs.createDoc, docs.updateDoc, docs.deleteDoc β declared to agents as
one AgentTools bundle (src/plugin/agentTools.json).
import { buildReactorFromPlugins, configurePlugin } from '@datalayer/reactor';
import { DocsPlugin } from '@datalayer/docs/lib/plugin';
const reactor = buildReactorFromPlugins([
configurePlugin(DocsPlugin, { backendUrl: 'http://localhost:8798' }),
]);On the Python side, installing datalayer_docs beside a Reactor host
publishes the docs extension (the datalayer.reactor.extensions entry
point): the /documents routes, the same data commands against the store,
the agent tools bundle, and the plugin's Module Federation container when it
has been built into share/ (make build-extension).
pip install 'datalayer_docs[jupyter]'
jupyter server extension enable datalayer_docs.jupyter
make build-jupyter # the page, into datalayer_docs/jupyter/static
jupyter server # then open /datalayer_docsmake install # pip install -e ".[test]"
npm install # or, in the Datalayer workspace, `npm i` at its root
make build-lib # lib/
make test # jest and pytest
make examples # the editor's examples on :3211
make dev-app # the interface on :5192, against `datalayer docs serve` on :8798See RELEASE.md for releases.
The editor is built with Lexical extensions:
every behaviour of this package is an extension in src/extensions, and
JupyterLexicalExtension bundles them all. A host depends on the bundle from
its own root extension, adds what is its own β namespace, theme, initial
state, focus on mount β and renders the content editable where it wants it:
import { AutoFocusExtension } from '@lexical/extension';
import { LexicalExtensionComposer } from '@lexical/react/LexicalExtensionComposer';
import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import { JupyterLexicalExtension, commentTheme } from '@datalayer/docs';
import { defineExtension } from 'lexical';
// Module scope: the composer rebuilds the editor whenever this reference changes.
const extension = defineExtension({
name: 'my-app/Document',
namespace: 'my-document',
theme: commentTheme,
dependencies: [JupyterLexicalExtension, AutoFocusExtension],
});
export const Document = () => (
<LexicalExtensionComposer extension={extension} contentEditable={null}>
<ContentEditable className="editor-input" />
</LexicalExtensionComposer>
);Bundled behaviours are reconfigured from the root with configExtension, for
instance configExtension(HistoryExtension, { disabled: true }) for a
collaborative document, or configExtension(ListMaxIndentLevelExtension, { maxDepth: 3 }).
Some of them expose signals in their output so a live editor can be
reconfigured without being rebuilt β the list ceiling, or the lexicalId and
serviceManager of LexicalStateExtension that register the editor in the
shared store for agent tools.
Two kinds of behaviour need the host: what only the host has (a kernel,
completion providers) and what hangs off the host's DOM (floating menus).
The first are output components β JupyterInputOutputExtension,
ComponentPickerMenuExtension and InlineCompletionExtension β placed with
useExtensionComponent or <ExtensionComponent lexical:extension={β¦} />;
the second stay React plug-ins rendered as children of the composer, as do
the toolbar, the comments panel and the collaboration provider.
Every extension keeps a React plug-in of the same name in src/plugins for an
editor still built with LexicalComposer; the plug-in is a thin wrapper over
the extension's register⦠function, so both paths share one implementation.
Three block types come with the bundle, and with / in the editor:
- Video (
VideoExtension,VideoNode): records the screen or the camera with the browser's ownMediaRecorderβ no account, no key, no upload. The file stays in the page's memory: it is lost when the page reloads, and nobody else editing the document sees it. Download keeps a copy. After a reload the block says its video is gone rather than pretending it never was. - Loom Video (
LoomExtension,LoomNode): an empty block that records a Loom video β the author signs in to their Loom account, where the video is saved β or takes the address of one, and then shows it. Pasting a Loom address offers to embed it. - Deck Slide (
DeckExtension,DeckNode): a deck drawn by@datalayer/decksfrom itsDeckSpec, one title slide to begin with. Double-click it to edit the specification as JSON; Save checks it withvalidateDeckSpecand keeps it only when it has no errors.
Recording with Loom needs a Loom public app id. This package keeps none; the host gives it from its own configuration:
configExtension(LoomExtension, { publicAppId: process.env.LOOM_PUBLIC_APP_ID });(<Editor extensions={[β¦]} /> takes that list, and <LoomPlugin publicAppId={β¦} />
does the same for an editor built with LexicalComposer.) Without one, Loom
blocks still show and embed videos; only Record is off. The examples read
LOOM_PUBLIC_APP_ID from the shell, or from .env.local in this package,
which git ignores (a bare .env it does not β keep the id out of that one):
npm run install:react18 # once, see below
echo 'LOOM_PUBLIC_APP_ID=β¦' >> .env.local # once
npm run start:vite # or start:webpackThe Loom recorder, @loomhq/record-sdk, runs on React 18: it imports React
from the page and mounts with ReactDOM.render, which React 19 removed. Under
JupyterLab, which shares React 18, nothing is needed. A React 19 page must give
the recorder a React 18 of its own β the SDK is imported only when somebody
presses Record, so without one it is only Record that fails, and the block
says so. This package's dev servers show both ways, with the pair
npm run install:react18 puts in vendor/react18:
- webpack (
webpack.config.js): the SDK and all it imports join a module layer,loom18, in whichreactandreact-domresolve to that pair; - Vite (
vite.config.ts): a plugin bundles the SDK apart with esbuild, everyreactimport pointed at that pair, and serves the result wherever the source imports the SDK.
A document goes in and out of other formats through src/convert:
-
Markdown β
$convertToMarkdownString/$convertFromMarkdownStringwith the package's transformers (tables, images, equations, rules, and fenced code as executable Jupyter inputs). -
nbformat β
lexicalToNbformat(nodes)writes a code cell per Jupyter input with its outputs and one Markdown cell per run of anything else;nbformatToLexical(notebook, editor)reads a notebook back (the input/output plug-in must be mounted for the code cells). -
LaTeX β
$convertToLatexString(transformers?, { document, documentClass, classOptions, title, author, date })and$convertFromLatexString(latex, transformers?), built on transformers shaped after@lexical/markdown's (LATEX_TRANSFORMERS): the title block (\title,\author,\date,\maketitle), headings from\partto\subparagraph, paragraphs and text formats, quotes and abstracts, theorem environments and proofs, lists, check lists and description lists, listings (lstlisting,minted,verbatim), algorithms, tables (tabularand friends,\multicolumnas spanning cells), figures, rules, inline and display math (AMS environments included), links, YouTube embeds, footnotes, citations andthebibliography, TeX dashes and quotes.\newcommandmacros from the preamble are expanded. Columns are read into layout nodes:multicols(at\columnbreak, or evenly), beamercolumns, side-by-sideminipages, and thetwocolumnclass option; they are written back asmulticols. Beamer frames become sections and theirblocks collapsible boxes;moderncventries and theletterclass (\opening,\closing,\signature,\ps) read as prose. A host adds a node by adding a transformer.$importLatexalso returns what the preamble said (LatexDocumentInfo). -
LaTeX templates β
LATEX_TEMPLATESholds one document per Overleaf template category (journal article, bibliography, book, calendar, CV, letter, assignment, newsletter, poster, presentation, thesis), each in the idiom of the category's best-known template. -
PDF β four routes, one extension each, all in the browser and all TypeScript (
PdfExportExtensionbundles them; commands and functions insrc/extensions/PdfExportExtension.ts):PdfPrintExtension/PRINT_PDF_COMMANDβ the rendered document, cloned into a hidden iframe with the page's stylesheets and a print stylesheet, printed by the browser (what JupyterLab does). Highest fidelity; the reader saves as PDF from the dialog.PdfDocumentExtension/EXPORT_PDF_DOCUMENT_COMMANDβ a vector PDF laid out from the document model (src/convert/pdf: a small PDF writer over the standard 14 fonts, a layout engine for headings, paragraphs, lists, code, tables, columns, quotes, boxes). Text stays text; pictures β equations through KaTeX, images, drawings, rich outputs β are drawn by the browser and placed as JPEGs. Runs headless withpictures: false.PdfSnapshotExtension/EXPORT_PDF_SNAPSHOT_COMMANDβ the editor drawn to a canvas with html2canvas and cut into page images at the whitest row near each cut. Everything as shown, nothing selectable.PdfTypesetExtension/EXPORT_PDF_TYPESET_COMMANDβ the document as Typst markup (src/convert/typst), compiled by the Typst engine in WebAssembly, fetched from a CDN on first use (loadTypstEngine; both URLs configurable). Real typesetting; LaTeX math through themitexpackage from Typst's registry.
The LexicalFormats example shows all of them on one document: Markdown
and LaTeX sources you can edit and apply, a live notebook, the nbformat
JSON, and the LaTeX rendered by reading it back into a read-only editor.
The LexicalLatex example ("LaTeX Templates") reads each template into an
editable editor beside its source, each written to the other on leaving.
The LexicalPdf example ("PDF Export") runs the four PDF routes on the
sample document side by side, with a preview of each result; both other
examples carry an "Export PDF" menu.