Now the full program — the seed script's sibling, and the payoff of every chapter in this book:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
--- Live trading dashboard: SQLite -> frames -> views
--- Seed first: crow run dashboard_seed.co Then: crow run dashboard.co
use web, ui, db, frame, math
conn = db.connect['sqlite://trades.db']
--- Reactive UI state
min_qty = 0
--- Event handlers
fn reset_filters[]
min_qty = 0
view stat_card[label, value]
return div[
h3[label],
p['`value`'],
class = 'card'
]
derive trades
return query[conn, 'SELECT symbol, qty, price, pnl FROM trades ORDER BY id']
view render[]
total_pnl = sum[trades.pnl]
shown = trades
if min_qty gt 0
shown = trades[trades.qty gte min_qty]
shown = sort[shown, 'pnl', desc = true]
by_symbol = group[trades, 'symbol', ['pnl', 'sum'], ['pnl', 'count']]
return div[
h1['Trading Dashboard'],
div[
stat_card['P&L', fmt[total_pnl, decimals = 2, commas = true]],
stat_card['Trades', len[trades]],
class = 'stats'
],
div[
field['Min qty', min_qty],
button['Reset', click = reset_filters],
class = 'controls'
],
h2['Trades'],
table[shown],
h2['P&L by Symbol'],
table[by_symbol]
]
serve[3000]
page['/', render]Walk it top to bottom and count the chapters:
db.connect — written with its module's prefix, which always
reaches a module's name (chapter 8); bare connect would do as well,
since db alone provides it. The connection is infrastructure, not UI
state — Crow keeps it as a process-wide handle, never serialized.min_qty / reset_filters — chapter 12's reactive loop: edit the
field, the state changes, the page re-renders; click Reset, same.stat_card — a parameterized component, used at two types
(string and int values). A function or a view called with two kinds is
compiled once for each, which quietly makes both work.trades — a derivation, chapter 12's other half: the query runs
off the render, and runs again when a handler writes the table or a
refresh tick passes, so every page shows the database as it is now
without a query per render. A view reads it like a fact; a view that
queried itself would be refused, since a view is a function of the
state it is given. Frames flow from the derivation through masks,
sort and group into table[...] untouched.Seed, run, open localhost:3000 — the page below is real output, served
by this program during this book's own verification:
Trading Dashboard
P&L: 1,116.10 Trades: 4
[Min qty ____] [Reset]
Trades: symbol qty price pnl ...
P&L by Symbol: AAPL 346.4 · MSFT -120.5 · NVDA 890.2