Café

A website built from inside a notebook. Open it with quilt notebook --serve examples/notebook/cafe.html.quilt, run the cells top to bottom, and /site is what is left when the cells are taken away.

1. The database — a SQL cell

The SQL machine is a live sqlite3 on the session's own file, and the Python kernel is told where that file is. One database, two machines.

sql↖ CREATE TABLE IF NOT EXISTS menu(item TEXT, price INTEGER); DELETE FROM menu; INSERT INTO menu VALUES ('flat white', 350), ('cortado', 320), ('cake', 450); ↗ sql↖SELECT count(*) FROM menu;↗

2. The backend — Python cells

route registers a handler in the kernel; the server proxies /app/… to it as a feed. Returning a list answers JSON.

py↖ @route("/menu") def menu(): rows = db().execute("SELECT item, price FROM menu ORDER BY price DESC").fetchall() return [{"item": row["item"], "price": row["price"]} for row in rows] ↗ py↖ @route("/order", methods=("POST",)) def order(req): if not req["body"]: return 400, {"error": "what would you like?"} with db() as con: con.execute("CREATE TABLE IF NOT EXISTS orders(item TEXT)") con.execute("INSERT INTO orders VALUES (?)", (req["body"],)) placed = con.execute("SELECT count(*) FROM orders").fetchone()[0] return {"ordered": req["body"], "placed": placed} ↗

3. The view — TypeScript cells

These run in this page's realm: their document is this document, their handlers outlive the cell, and they reach the Python backend with fetch. They are evaluated as JavaScript, so no type annotations here.

the view this cell builds

The first cell prints its view rather than assigning it. What a cell prints is fed to the page, and markup carrying an id the page already has redefines that element — so the page the server holds has the menu in it, and /site does too. (Assigning to innerHTML would change what this frame shows and nothing else: the page is the server's.)

ts↖ const pounds = (pence) => "£" + (pence / 100).toFixed(2); const view = (rows) => `
`; fetch("/app/menu") .then((r) => r.json()) .then((rows) => { console.log(view(rows)); return `${rows.length} on the menu`; }) ↗

The second adds behaviour. The handler goes on document, not on the list: the page is a live document that cells keep redefining, and a listener bound to an element it replaced would go with it.

ts↖ document.addEventListener("click", (ev) => { const item = ev.target.dataset && ev.target.dataset.item; if (!item) return; fetch("/app/order", { method: "POST", body: item }) .then((r) => r.json()) .then((o) => { document.getElementById("orders").textContent = `${o.placed} order(s), last: ${o.ordered}`; }); }); "click an item to order it" ↗

no orders yet

4. The page is the bus

Machines share nothing with each other; what they share is the page. A cell's output that carries an id the page already has redefines that element — so this Python cell writes the headline, wherever the headline happens to be.

py↖ count = db().execute("SELECT count(*) FROM menu").fetchone()[0] print(f'

Café — {count} things

') ↗

…and an unquote that reaches the page reads it back, in the reading cell's own language: \↙#title\↘ is a Python string here.

py↖↙#title↘.split("—")[-1].strip()↗