<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
Attributes
| Attribute | Default | Description |
|---|---|---|
name | The variable that holds the result. | |
output | off | Shows 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. |
copy | off | Adds a Copy button that puts the code, as edited, on the clipboard. |
lang | js | The language. JavaScript is built in; registerEngine adds others. |
timeout | 2000 | Milliseconds a run may take before it's stopped. |
debounce | 400 | Milliseconds to wait after the last keystroke before running. |
error | A variable that gets the error message, or null when a run succeeds. | |
label | the name | The 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
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:
- One expression, like the
exprof a<t-let>:speed * 3.6. - Statements with a
return, as in the example. Without areturn, the result isundefined.
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.
| Input | Effect |
|---|---|
| Typing | Runs the code after a pause. |
| Ctrl/⌘ + Enter | Runs now, even if nothing changed. |
| Reset | Puts the starting code back. It only shows up after an edit. |
| Copy | Puts the code on the clipboard. Only with copy. |
The sandbox
JavaScript cells run in a Web Worker, one per cell:
- The code can't reach the page: no
document, nowindow, no other cells and no page variables except the copies it was given. - A run that takes longer than
timeout, like an endlesswhile (true) {}, is stopped and reported. The page keeps responding. A new edit also stops a run that's still going. - A worker can still use
fetchwith the page's address. That's fine for a reader editing code on their own screen, but it doesn't make code from strangers safe to run.
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.