Writing

Code that runs in the post

A fenced block tagged artifact stops being a listing and starts being a program — including a transformer that runs entirely in your browser.

Noel Thomas

Founder

2 min read

  • engineering

Writing about a system and demonstrating it are usually two different jobs, done in two different places: the post explains, and a link sends you somewhere else to watch it work. That gap is where most technical writing loses people.

So a fenced code block can now be marked artifact, and the renderer treats it as a program rather than a listing. Nothing else about authoring changes — it is still a fence in a markdown file.1

#The short version

Tag the fence and it runs:

md
```js artifact title="Whitespace tokenizer" height=240
// ...
```

Here is that block, live. Edit the input and the chips follow it.

Whitespace tokenizer

Press Source on the bar above to read the code, Restart to reset it.

#What the code gets

Each artifact runs as an ES module in its own sandboxed frame, so import and top-level await both work and a bad loop only takes down that one widget. Four things are defined before your code runs:2

rootan element to render into
print(...)append a line of output
status(text)set the progress line above the output
clear()empty both

Anything that throws — including a rejected promise — is reported in the frame rather than swallowed.

#A real model, in the page

The reason this exists. Nothing below runs on a server: the weights are fetched once, cached by the browser, and the inference happens on your machine.3

Sentiment, running locally

That one is gated behind a button on purpose. An artifact that pulls tens of megabytes should never start on page load, so the default is to wait for a click; cheap examples opt in with autorun.

#Options

OptionEffect
title="…"Label on the bar
height=320Frame height in pixels
note="…"Line shown under the run button
autorunStart on load — for examples that cost nothing

Ordinary fences are untouched. Leave the meta off and you get a listing, same as before:

js
export const article = { title: 'Code that runs in the post' }

#Footnotes

  1. The options live in the fence's info string, which every other markdown tool is required to ignore. The file still opens cleanly anywhere else.

  2. A module body cannot be wrapped in try/catch without breaking its imports, so failures are collected off the window instead.

  3. The frame is same-origin, which is what gives it the Cache API — under a strict sandbox the origin is opaque and transformers.js would refetch its weights on every visit.

More articles

Transformers

The architecture that changed the world.

Read more

Pytorch Internals

What exactly Pytorch does behind the scenes to enable performant tensor computations.

Read more

Address

Calgary, AB · Montreal, QC

Calgary
+1 (587) 700-9968
Montreal
+1 (825) 365-9891
Hours
09:00 – 17:00 · MT