#!/usr/bin/env quilt A polyglot notebook

A polyglot notebook

This line will be rewritten by a cell further down the page.

HTML is the ground language here, so the page is the program and the cells are its quotes. Each language's cells share one machine that persists for the whole run — a Python kernel, a live sqlite3, a long-lived shell, a Node context — and the page itself is the HTML machine: what a cell prints goes back into it.

1. Machines persist across cells

A definition in one cell is there for the next. Left, Python; right, SQL; the arrangement is just a CSS grid.

py↖ menu = {"espresso": 2.5, "latte": 3.75, "cortado": 3.25, "flat white": 3.5} def pricey(threshold): return sorted(k for k, v in menu.items() if v > threshold) ↗ py↖pricey(3.0)↗
sql↖ CREATE TEMP TABLE menu(item TEXT, price REAL); INSERT INTO menu VALUES ('espresso', 2.5), ('latte', 3.75), ('cortado', 3.25), ('flat white', 3.5); ↗ sql↖SELECT item FROM menu WHERE price > 3.0 ORDER BY item;↗

2. Cells edit the page

Output that carries an id the page already has redefines that element wherever it stands. The chart below is drawn by the cell after it — the page held an empty figure with that id, and the cell's output replaced it. The headline at the top of the page is rewritten the same way.

(the chart cell has not run yet)
py↖ bars = html↖↗ for i, (name, price) in enumerate(sorted(menu.items(), key=lambda kv: -kv[1])): y, w = 8 + i * 26, int(price * 60) bars = html↖ ↙bars↘ ↙↑(name)↘ ↙↑(f"{price:.2f}")↘ ↗ print(html↖
↙bars↘
Prices, drawn by the cell below this figure.
↗.coparse()) print(html↖

Rewritten by a cell: the dearest drink is ↙↑(max(menu, key=menu.get))↘.

↗.coparse()) ↗

3. The page is the bus

Machines share nothing with each other; what they share is the page. An unquote that reaches the page reads it: \↙#total\↘ is the text of the element with that id, spliced into the cell as a string literal of the cell's language. SQL answers into #total, and one row of four languages reads it back.

sql↖SELECT '' || printf('%.2f', SUM(price)) || '' FROM menu;↗
py↖round(float(↙#total↘) / len(menu), 3)↗ ts↖const total: number = Number(↙#total↘); total * 100↗ bash↖total=↙#total↘; echo "cents: ${total/./}"↗ sql↖SELECT ↙#total↘ - (SELECT MIN(price) FROM menu);↗

4. Cells create cells

Output is read as Quilt-in-HTML, so a quote in it is a new cell, run right away and rendered in place (dashed edge). Python builds each new cell as a Python term — \↑ lifts the name into it as a Python literal — and splices the term into the page markup two quote levels up, \↙\↙cell\↘\↘. A shell loop writes cells too (${i} braced, because bash reads the byte after $i as part of the name).

py↖ for name in pricey(3.0): cell = py↖print(↙↑(name)↘, "costs", menu[↙↑(name)↘])↗ print(html↖
py↖↙↙cell↘↘↗
↗.coparse()) ↗
bash↖for i in 1 2 3; do echo html↖
bash↖echo "cell ↙↙${i}↘↘ of 3, made by a shell loop"↗
↗; done↗

5. Layout is yours

Cells are elements, so anything HTML can do with an element it can do with a cell. A cell in a collapsible section:

A TypeScript cell, folded away ts↖ const rows = ["espresso", "latte"].map((n, i) => `${i + 1}. ${n}`); console.log(rows.join("\n")); ↗

And a cell whose language has no machine fails on its own, with the notebook going on:

wgsl↖fn f() -> f32 { return 1.0; }↗

The same session, a line at a time: quilt repl html.