<t-codecell>

Code the reader edits. It runs in a sandbox, again whenever the code or a page variable it mentions changes, and its result goes into a variable.

A stone dropped from 20 m falls for .

true true 2000 400 Math.sqrt(2 * height / 9.81)

Attributes

AttributeDefaultDescription
nameThe variable that holds the result.
outputoffShows the result under the editor. Without a name it always shows, since it has nowhere else to go. Logged lines and errors show either way.
copyoffAdds a Copy button that puts the code, as edited, on the clipboard.
langjsThe language. JavaScript is built in; registerEngine adds others.
timeout2000Milliseconds a run may take before it's stopped.
debounce400Milliseconds to wait after the last keystroke before running.
errorA variable that gets the error message, or null when a run succeeds.
labelthe nameThe editor's accessible name.

A simulation

The result can be anything that fits in a variable, such as a whole trajectory to plot.

A baseball leaves the bat at 35° and 45 m/s. With air drag it lands m away; in a vacuum it would go m.

The cell mentions angle and speed, so dragging either one runs it again. Change the drag coefficient cd to 0 and the solid line lands on the dashed one, the path in a vacuum.

Writing the code

Short code can go straight inside the element, as in the first example. Longer code is better in a <script type="text/plain">, as in the second: the browser doesn't run it, and Markdown and HTML leave its < and & alone. Either way, its indentation is removed.

The code is either:

It runs as an async function, so await works at the top level.

In the editor, the names that come from the page are tinted, like the values the reader changes in the prose. A name the code declares itself isn't tinted.

Page variables the code mentions are passed in as copies, so assigning to one doesn't change the page. The code can declare its own variable with the same name. The result goes back out the same way. Numbers, strings, arrays, plain objects, Map, Set, Date and typed arrays can cross; functions and DOM elements can't.

Output

Under the editor are what the code logged with console.log, and the error and the line it happened on, if there is one. The result shows there too with output, or when the cell has no name; usually other elements on the page show it instead. While a run is in progress the old output stays, dimmed, and so does every element that shows the cell's variable. A failed run keeps the last good value.

InputEffect
TypingRuns the code after a pause.
Ctrl/⌘ + EnterRuns now, even if nothing changed.
ResetPuts the starting code back. It only shows up after an edit.
CopyPuts the code on the clipboard. Only with copy.

The sandbox

JavaScript cells run in a Web Worker, one per cell:

The editor

The editor is CodeMirror, and it loads only on pages that have a <t-codecell>. Until it loads, the code shows as plain text. If it can't load, for example offline, a plain text area takes its place. Highlighting uses the same colors as <t-call>, so it follows the theme. To load CodeMirror from somewhere else, see configure.

Other languages

An engine runs a language's code. registerEngine(lang, factory) adds one: factory() is called once per cell and returns { run(code, inputs, { timeout }), dispose() }. run returns a promise of { value, logs }, where logs is a list of { level, text }. To report a failure, it rejects with an Error, which can carry a line.

An engine for Python (with Pyodide) or SQL (with DuckDB) would start its runtime in a worker in the same way.