Pluto.jl · Snapshot.jl · WasmTarget.jl · Native Therapy Components

Interactive Notebooks as Components

Many Julia workflows begin in a Pluto.jl notebook. With Snapshot.jl, you can compile those reactive notebooks to WasmGC and embed them directly inside any Therapy.jl route alongside @island controls and WasmMakie plots. No iframes, no running Julia server.

Live Julia Random.Xoshiro Monte Carlo sampler and Statistics mean/std converging onto the Central Limit Theorem Gaussian curve.

Monte Carlo & Central Limit Lab

Take $n$ independent draws from almost any distribution with mean $\mu$ and variance $\sigma^2$. As $n$ grows, the distribution of their sample mean $\bar{X}_n$ converges to a normal curve $\mathcal{N}(\mu, \sigma^2 / n)$, even when the underlying distribution is skewed or bimodal.

Every simulation below runs real Julia Random.Xoshiro and Statistics code compiled to WasmGC by WasmTarget.jl. Drag the sliders to change the source distribution, sample size $n$, trial count $M$, or RNG seed.

using PlutoUI
import Random
import Statistics

Source distribution (1 = Uniform, 2 = Exponential, 3 = Bimodal) = 2

Sample size per draw $n$ = 8

Monte Carlo trials $M$ = 1200

RNG seed = 7

draw_one (generic function with 1 method)
function draw_one(rng::Random.Xoshiro, kind::Int)::Float64
    u = Random.rand(rng)
    if kind == 1
        return 2.0 * u
    elseif kind == 2
        return -log(max(1e-12, 1.0 - u))
    else
        u2 = Random.rand(rng)
        base = u < 0.5 ? 0.35 : 1.65
        return base + 0.18 * (u2 - 0.5)
    end
end
dist_sigma (generic function with 1 method)
function dist_sigma(kind::Int)::Float64
    if kind == 1
        return sqrt(1.0 / 3.0)
    elseif kind == 2
        return 1.0
    else
        return sqrt(0.4225 + 0.0027)
    end
end
dist_label (generic function with 1 method)
function dist_label(kind::Int)::String
    if kind == 1
        return "Uniform(0, 2)"
    elseif kind == 2
        return "Exponential(1)"
    else
        return "Bimodal mixture"
    end
end
fmt3 (generic function with 1 method)
function fmt3(x::Float64)::String
    neg = x < 0.0
    scaled = round(Int, abs(x) * 1000.0)
    whole = div(scaled, 1000)
    frac = mod(scaled, 1000)
    fstr = frac < 10 ? string("00", frac) : (frac < 100 ? string("0", frac) : string(frac))
    return neg ? string("-", whole, ".", fstr) : string(whole, ".", fstr)
end
fmt1 (generic function with 1 method)
function fmt1(x::Float64)::String
    neg = x < 0.0
    scaled = round(Int, abs(x) * 10.0)
    whole = div(scaled, 10)
    frac = mod(scaled, 10)
    return neg ? string("-", whole, ".", frac) : string(whole, ".", frac)
end
run_clt (generic function with 1 method)
function run_clt(kind::Int, n::Int, trials::Int, seed::Int)::Vector{Float64}
    rng = Random.Xoshiro(UInt64(seed * 9973 + 17))
    means = Vector{Float64}(undef, trials)
    inv_n = 1.0 / Float64(n)
    for t in 1:trials
        s = 0.0
        for _ in 1:n
            s += draw_one(rng, kind)
        end
        means[t] = s * inv_n
    end
    return means
end
render_clt_svg (generic function with 1 method)
function render_clt_svg(kind::Int, n::Int, trials::Int, seed::Int)
    means = run_clt(kind, n, trials, seed)
    emp_mu = Statistics.mean(means)
    emp_sd = Statistics.std(means)
    mu0 = 1.0
    sigma0 = dist_sigma(kind)
    label = dist_label(kind)
    theo_sd = sigma0 / sqrt(Float64(n))

    lo = 0.0
    hi = 2.4
    nbins = 28
    bw = (hi - lo) / Float64(nbins)
    counts = zeros(Int, nbins)
    for m in means
        idx = floor(Int, (m - lo) / bw) + 1
        if 1 <= idx <= nbins
            counts[idx] += 1
        end
    end

    densities = Vector{Float64}(undef, nbins)
    max_d = 0.25
    for i in 1:nbins
        d = Float64(counts[i]) / (Float64(trials) * bw)
        densities[i] = d
        if d > max_d
            max_d = d
        end
    end
    sqrt2pi = 2.5066282746310002
    peak_gauss = 1.0 / (theo_sd * sqrt2pi)
    if peak_gauss > max_d
        max_d = peak_gauss
    end
    max_d *= 1.12

    w = 640.0
    h = 260.0
    pl = 46.0
    pr = 20.0
    pt = 36.0
    pb = 34.0
    pw = w - pl - pr
    ph = h - pt - pb

    parts = String[]
    push!(parts, "<div style=\"width:100%;max-width:660px;margin:0 auto;\">")
    push!(parts, "<svg viewBox=\"0 0 640 260\" width=\"100%\" style=\"display:block;border-radius:12px;border:1px solid color-mix(in oklab,var(--color-base-content) 14%,transparent);background:var(--color-base-100);\">")
    push!(parts, string("<text x=\"46\" y=\"22\" font-size=\"12\" font-weight=\"600\" fill=\"var(--color-base-content)\" opacity=\"0.8\">", label, " sample means (n=", string(n), ", M=", string(trials), ")</text>"))
    push!(parts, "<line x1=\"465\" y1=\"18\" x2=\"485\" y2=\"18\" stroke=\"var(--color-secondary)\" stroke-width=\"2.5\"/>")
    push!(parts, "<text x=\"491\" y=\"22\" font-size=\"11\" fill=\"var(--color-base-content)\" opacity=\"0.7\">CLT Normal curve</text>")

    for k in 0:3
        gy = pt + ph * (Float64(k) / 3.0)
        push!(parts, string("<line x1=\"", fmt1(pl), "\" y1=\"", fmt1(gy), "\" x2=\"", fmt1(w - pr), "\" y2=\"", fmt1(gy), "\" stroke=\"var(--color-base-content)\" stroke-opacity=\"0.1\" stroke-width=\"1\"/>"))
    end

    bar_w = pw / Float64(nbins)
    for i in 1:nbins
        bx = pl + Float64(i - 1) * bar_w + 1.0
        bh = (densities[i] / max_d) * ph
        by = pt + ph - bh
        push!(parts, string("<rect x=\"", fmt1(bx), "\" y=\"", fmt1(by), "\" width=\"", fmt1(max(1.0, bar_w - 2.0)), "\" height=\"", fmt1(max(0.0, bh)), "\" rx=\"2\" fill=\"color-mix(in oklab,var(--color-primary) 62%,transparent)\" stroke=\"var(--color-primary)\" stroke-width=\"0.8\"/>"))
    end

    push!(parts, "<polyline fill=\"none\" stroke=\"var(--color-secondary)\" stroke-width=\"2.6\" stroke-linecap=\"round\" points=\"")
    npts = 80
    for i in 0:npts
        xv = lo + (hi - lo) * (Float64(i) / Float64(npts))
        z = (xv - mu0) / theo_sd
        gv = exp(-0.5 * z * z) / (theo_sd * sqrt2pi)
        if i > 0
            push!(parts, " ")
        end
        px = pl + ((xv - lo) / (hi - lo)) * pw
        py = pt + ph - (gv / max_d) * ph
        push!(parts, string(fmt1(px), ",", fmt1(py)))
    end
    push!(parts, "\"/>")

    mx = pl + ((mu0 - lo) / (hi - lo)) * pw
    push!(parts, string("<line x1=\"", fmt1(mx), "\" y1=\"", fmt1(pt), "\" x2=\"", fmt1(mx), "\" y2=\"", fmt1(pt + ph), "\" stroke=\"var(--color-accent)\" stroke-dasharray=\"4 4\" stroke-width=\"1.5\"/>"))

    for ti in 0:4
        tick = Float64(ti) * 0.5
        tx = pl + ((tick - lo) / (hi - lo)) * pw
        push!(parts, string("<text x=\"", fmt1(tx), "\" y=\"", fmt1(h - 12.0), "\" text-anchor=\"middle\" font-size=\"11\" font-family=\"monospace\" fill=\"var(--color-base-content)\" opacity=\"0.65\">", fmt1(tick), "</text>"))
    end
    push!(parts, "</svg>")

    push!(parts, "<div style=\"display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:10px;margin-top:12px;\">")
    push!(parts, string("<div style=\"padding:10px 12px;border-radius:10px;border:1px solid color-mix(in oklab,var(--color-base-content) 12%,transparent);background:var(--color-base-100);\"><div style=\"font-size:11px;opacity:0.65;\">Empirical mean</div><div style=\"font-family:monospace;font-size:16px;font-weight:700;color:var(--color-primary);\">", fmt3(emp_mu), "</div></div>"))
    push!(parts, string("<div style=\"padding:10px 12px;border-radius:10px;border:1px solid color-mix(in oklab,var(--color-base-content) 12%,transparent);background:var(--color-base-100);\"><div style=\"font-size:11px;opacity:0.65;\">True mean</div><div style=\"font-family:monospace;font-size:16px;font-weight:700;\">", fmt3(mu0), "</div></div>"))
    push!(parts, string("<div style=\"padding:10px 12px;border-radius:10px;border:1px solid color-mix(in oklab,var(--color-base-content) 12%,transparent);background:var(--color-base-100);\"><div style=\"font-size:11px;opacity:0.65;\">Empirical std</div><div style=\"font-family:monospace;font-size:16px;font-weight:700;color:var(--color-secondary);\">", fmt3(emp_sd), "</div></div>"))
    push!(parts, string("<div style=\"padding:10px 12px;border-radius:10px;border:1px solid color-mix(in oklab,var(--color-base-content) 12%,transparent);background:var(--color-base-100);\"><div style=\"font-size:11px;opacity:0.65;\">CLT std (sigma/sqrt(n))</div><div style=\"font-family:monospace;font-size:16px;font-weight:700;\">", fmt3(theo_sd), "</div></div>"))
    push!(parts, "</div></div>")
    return HTML(join(parts))
end

Tip: toggle light and dark mode in the top bar. Because the notebook mounts natively into Therapy's DOM instead of an iframe, its CSS tokens and SVG diagrams reskin with the page, and switching notebook tabs above runs through Therapy's View Transitions router.

Built for Scientific Julia Workflows

Explore data and models in Pluto, then publish them directly inside a real web application or documentation portal without rewriting your analysis in JavaScript.

One Component Tree

Compose SnapshotNotebook right next to Therapy navigation, authentication, @island forms, and Markdown prose. Shared CSS tokens keep typography and dark mode aligned automatically.

Zero-Server Interactivity

Pluto @bind sliders and inputs drive compiled Julia WasmGC modules directly in the reader's browser. Host on GitHub Pages, Cloudflare, or any static CDN with instant cold starts.

SPA View Transitions

Therapy's client router disposes the previous notebook's WasmGC runtime on therapy:router:before-swap and boots the next notebook cleanly during soft page transitions.

Embed in 3 Steps

Export your Pluto notebook with fragment = true, mount the output directory via staticfiles, and render SnapshotNotebook in any route.

using Snapshot
using Therapy

# 1. Export a reactive Pluto notebook as an embeddable HTML fragment + WasmGC islands
Snapshot.export_notebook(
    "notebooks/svd_geometry.jl";
    output_dir = "notebooks-static",
    theme_picker = false,
    fragment = true,
    fragment_dependencies = :host,
    assets_base = "__PI_ASSETS_BASE__",
)

# 2. Mount the exported assets in app.jl
app = App(
    routes_dir = "src/routes",
    components_dir = "src/components",
    title = "Scientific Dashboard",
)
staticfiles(app, joinpath(@__DIR__, "notebooks-static"), "notebooks-static")

# 3. Render the notebook inside any route (src/routes/notebooks/index.jl)
() -> Div(
    H1("Matrix Geometry Lab"),
    SnapshotNotebook(
        joinpath(@__DIR__, "..", "..", "notebooks-static", "svd_geometry.fragment.html");
        assets_base = "/notebooks-static",
    ),
)

Ready for Evolving WasmTarget & Julia WebAssembly

Both Therapy.jl and Snapshot.jl isolate compilation behind a thin Wasm backend adapter (WasmPlatformSpec, compile_island_wasm, and content-addressed on-disk caching). As WasmTarget.jl expands Julia language coverage and as Julia's native WebAssembly BinaryPlatforms support (Julia PR #64675) brings wasm32 JLL binaries and bridge modules, existing Therapy routes and embedded Snapshot notebooks gain broader Julia package support without rewriting your app.