3Run [Stan](https://mc-stan.org) sampling in your browser, inside a VS
4Code-style IDE built on [minwebide](https://github.com/magland/minwebide).
5Projects live in your browser's IndexedDB. Models compile on a remote
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 6[stan-wasm-wasi](https://github.com/magland/stan-wasm-wasi) server to pure
7WASI modules; sampling itself runs locally, one web worker per chain, each
8invoking the module CLI-style through a small WASI shim.
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 9
10**Live site:** https://concept-collection.github.io/stan-web-ide/
12## How it works
14A project holds `.stan` programs, `.json` data files, and `.sample` files. A
15`.sample` file is a YAML description of one sampling run:
17```yaml
18stan: linear.stan # the Stan program
19data: data.json # the data
20num_chains: 4 # optional; defaults 4 / 1000 / 1000 / 2.0 / random
21num_warmup: 1000
22num_samples: 1000
23init_radius: 2.0
24seed: 42 # omit for a random seed
25```
27Paths are relative to the `.sample` file (leading `/` = project root).
28Opening a `.sample` file shows a **form view** — file pickers, sampling
29parameters, a Run button, and per-chain progress bars. The form edits the
30underlying YAML (tab menu → *Reopen as Text Editor* for the raw file); the
31tab bar's ▶/⏹ runs and stops the same way. Runs use current editor contents,
32saved or not.
34A run compiles the program (server-side, cached by source hash), streams
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 35Stan's console output to the **Output** panel, and writes into the output
36directory — always `<name>.out` next to the `.sample` file (so
37`fit.sample` → `fit.out`, replaced on each run):
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 38
39- `chain_1.csv` … one CSV per chain, header = parameter names, one row per draw
40- `summary.csv` — mean, MCSE, sd, 5%/50%/95%, ESS, ESS/s, split-Rhat per
41 parameter (via [mcmc-stats](https://github.com/flatironinstitute/mcmc-stats.js))
42- `console.txt` — the sampler's console output
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 43- `run.json` — the exact configuration used (including the resolved seed);
44 written last, so it doubles as the run's completion marker
46When a run finishes, a **results dashboard** opens on `run.json`: tabs for
47the summary table (Rhat highlighted when > 1.01), histograms, trace plots,
48scatter plots, a draws table, and the console — plots via lazily-loaded
49[plotly](https://plotly.com/javascript/). It reads the sibling CSVs, so it
50reopens in any later session: click **View results** on the `.sample` form,
51or `run.json` in the output folder.
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 52
53The `.stan` editor has syntax highlighting plus diagnostics, hover docs,
54completion, and auto-format from
55[stan-language-server](https://github.com/tomatitito/stan-language-server)
56(stanc3 compiled to JS, running in a worker).
58## The compilation server
60Compiling Stan to WebAssembly needs a server; everything else is local. The
61status bar shows the configured server (click it to change; persisted in the
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 62browser). The default is `https://stan-wasm-wasi.fly.dev`, a hosted
63[stan-wasm-wasi](https://github.com/magland/stan-wasm-wasi) instance — it
64allows any origin (CORS), caches compiled models by source hash, and
65auto-stops when idle, so the first compile after an idle period pays a
66~30 s cold start. To run one locally instead:
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 67
68```sh
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 69docker build -t stan-wasm-wasi https://github.com/magland/stan-wasm-wasi.git
70docker run --rm -p 8083:8080 stan-wasm-wasi
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 73then set the server URL to `http://localhost:8083`.
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 74
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 75The compiled models are pure-WASI command modules (`main.wasm`): each run of
76the module executes one MCMC chain (CmdStan seed/chain-id convention), with
77draws on stdout as CSV and Stan's console on stderr. Chains run in parallel
78workers, so no threads, no `SharedArrayBuffer`, and no cross-origin
79isolation are needed.
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 80
81## Development
83minwebide is consumed as a sibling checkout (`file:../minwebide`):
85```sh
86git clone https://github.com/magland/minwebide ../minwebide
87(cd ../minwebide && npm install) # fetches the pinned VS Code source
88npm install
89npm run dev # http://127.0.0.1:3000
90```
92- `npm run build` — static bundle in `dist/`
93- `npm run typecheck` — typechecks app code (vendor diagnostics suppressed)
b1dbd02Switch sampling to pure-WASI web workers; add results dashboardJeremy Magland 94- `npm run smoke` — headless end-to-end test against the built bundle; when
95 the default compile server is reachable it also compiles and samples for
96 real
21efcb6stan web IDE: run Stan sampling in the browserJeremy Magland 97- `node scripts/dev-check.mjs` — quick checks against a running dev server
99CI checks out `magland/minwebide` next to this repo, installs both, builds,
100and publishes `dist/` to GitHub Pages.