|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8" /> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | +<title>Pyodide PoC — real pick_and_retry loop in the browser</title> |
| 7 | +<style> |
| 8 | + body { font-family: system-ui, sans-serif; max-width: 760px; margin: 2rem auto; padding: 0 1rem; line-height: 1.5; color: #1b1b1b; } |
| 9 | + h1 { font-size: 1.4rem; } |
| 10 | + p.lede { color: #444; } |
| 11 | + button { font-size: 1rem; padding: 0.5rem 1rem; border: 1px solid #888; border-radius: 6px; background: #f6f6f6; cursor: pointer; } |
| 12 | + button:disabled { opacity: 0.5; cursor: default; } |
| 13 | + #log { white-space: pre-wrap; background: #0f1419; color: #d6deeb; padding: 1rem; border-radius: 8px; margin-top: 1rem; font-family: ui-monospace, monospace; font-size: 0.85rem; min-height: 6rem; } |
| 14 | + .ok { color: #9ece6a; } |
| 15 | + .err { color: #f7768e; } |
| 16 | + .dim { color: #7a88a0; } |
| 17 | + code { background: #eee; padding: 0 0.25rem; border-radius: 3px; } |
| 18 | +</style> |
| 19 | +</head> |
| 20 | +<body> |
| 21 | +<h1>Proof of concept: the real Python loop, in your browser</h1> |
| 22 | +<p class="lede"> |
| 23 | + This page loads <a href="https://pyodide.org">Pyodide</a> (CPython + NumPy in |
| 24 | + WebAssembly), unpacks the actual <code>pir</code> package, and runs the |
| 25 | + <strong>unmodified</strong> |
| 26 | + <code>examples/manipulation/01_pick_and_retry.py</code> loop headless — no |
| 27 | + install, no server, no matplotlib. It prints the resulting |
| 28 | + <code>Trace.summary()</code>. This is Phase 0 from |
| 29 | + <code>docs/pyodide_playground_strategy.md</code>: confirm the real loop runs in |
| 30 | + the browser and measure load time before wiring it into the playground UI. |
| 31 | +</p> |
| 32 | + |
| 33 | +<button id="run">Run the real loop</button> |
| 34 | +<label style="margin-left:1rem;">seed |
| 35 | + <input id="seed" type="number" value="3" style="width:4rem;" /> |
| 36 | +</label> |
| 37 | + |
| 38 | +<div id="log" class="dim">Click “Run the real loop”. First run downloads Pyodide (~a few MB); later runs are cached.</div> |
| 39 | + |
| 40 | +<script> |
| 41 | +const logEl = document.getElementById("log"); |
| 42 | +const runBtn = document.getElementById("run"); |
| 43 | +const seedEl = document.getElementById("seed"); |
| 44 | +let pyodideReadyPromise = null; |
| 45 | + |
| 46 | +function log(msg, cls) { |
| 47 | + const span = document.createElement("span"); |
| 48 | + if (cls) span.className = cls; |
| 49 | + span.textContent = msg + "\n"; |
| 50 | + logEl.appendChild(span); |
| 51 | +} |
| 52 | +function clearLog() { logEl.textContent = ""; logEl.className = ""; } |
| 53 | +const now = () => performance.now(); |
| 54 | +const ms = (t) => `${(now() - t).toFixed(0)} ms`; |
| 55 | + |
| 56 | +async function ensurePyodide() { |
| 57 | + if (pyodideReadyPromise) return pyodideReadyPromise; |
| 58 | + pyodideReadyPromise = (async () => { |
| 59 | + const t = now(); |
| 60 | + log("loading Pyodide runtime…", "dim"); |
| 61 | + // eslint-disable-next-line no-undef |
| 62 | + const pyodide = await loadPyodide({ indexURL: "https://cdn.jsdelivr.net/pyodide/v0.27.2/full/" }); |
| 63 | + log(` Pyodide ready (${ms(t)})`, "ok"); |
| 64 | + |
| 65 | + const tn = now(); |
| 66 | + log("loading numpy…", "dim"); |
| 67 | + await pyodide.loadPackage("numpy"); |
| 68 | + log(` numpy ready (${ms(tn)})`, "ok"); |
| 69 | + |
| 70 | + const tz = now(); |
| 71 | + log("fetching + unpacking pir_bundle.zip…", "dim"); |
| 72 | + const buf = await (await fetch("./pir_bundle.zip")).arrayBuffer(); |
| 73 | + await pyodide.unpackArchive(buf, "zip"); |
| 74 | + log(` bundle unpacked (${ms(tz)})`, "ok"); |
| 75 | + |
| 76 | + return pyodide; |
| 77 | + })(); |
| 78 | + return pyodideReadyPromise; |
| 79 | +} |
| 80 | + |
| 81 | +const DRIVER = ` |
| 82 | +import json, os, sys, time, importlib.util |
| 83 | +
|
| 84 | +# unpackArchive extracts relative to the current working directory. |
| 85 | +cwd = os.getcwd() |
| 86 | +if cwd not in sys.path: |
| 87 | + sys.path.insert(0, cwd) |
| 88 | +
|
| 89 | +# Prove the browser path is matplotlib-free: block it so an accidental import |
| 90 | +# fails loudly instead of silently dragging in a multi-MB package. |
| 91 | +class _NoMatplotlib: |
| 92 | + def find_spec(self, name, path=None, target=None): |
| 93 | + if name == "matplotlib" or name.startswith("matplotlib."): |
| 94 | + raise ImportError("matplotlib is intentionally unavailable on the headless browser path") |
| 95 | + return None |
| 96 | +sys.meta_path.insert(0, _NoMatplotlib()) |
| 97 | +
|
| 98 | +path = os.path.join(cwd, "examples", "manipulation", "01_pick_and_retry.py") |
| 99 | +spec = importlib.util.spec_from_file_location("pick_and_retry", path) |
| 100 | +mod = importlib.util.module_from_spec(spec) |
| 101 | +spec.loader.exec_module(mod) |
| 102 | +
|
| 103 | +t0 = time.perf_counter() |
| 104 | +trace = mod.run(seed=SEED, render=False) |
| 105 | +elapsed_ms = (time.perf_counter() - t0) * 1000.0 |
| 106 | +
|
| 107 | +s = trace.summary() |
| 108 | +result = { |
| 109 | + "steps": s.steps, |
| 110 | + "success": bool(s.success), |
| 111 | + "failure_counts": dict(s.failure_counts), |
| 112 | + "total_reward": round(sum(trace.rewards), 3), |
| 113 | + "elapsed_ms": round(elapsed_ms, 1), |
| 114 | +} |
| 115 | +json.dumps(result) |
| 116 | +`; |
| 117 | + |
| 118 | +async function run() { |
| 119 | + runBtn.disabled = true; |
| 120 | + clearLog(); |
| 121 | + try { |
| 122 | + const pyodide = await ensurePyodide(); |
| 123 | + const seed = parseInt(seedEl.value, 10) || 0; |
| 124 | + const tr = now(); |
| 125 | + log(`running pick_and_retry.run(seed=${seed}, render=False)…`, "dim"); |
| 126 | + const code = DRIVER.replace("SEED", String(seed)); |
| 127 | + const out = JSON.parse(await pyodide.runPythonAsync(code)); |
| 128 | + log(` loop finished (${ms(tr)})`, "ok"); |
| 129 | + log(""); |
| 130 | + log("Trace.summary():", "ok"); |
| 131 | + log(` steps = ${out.steps}`); |
| 132 | + log(` success = ${out.success}`); |
| 133 | + log(` failure_counts = ${JSON.stringify(out.failure_counts)}`); |
| 134 | + log(` total_reward = ${out.total_reward}`); |
| 135 | + log(` (pure loop compute: ${out.elapsed_ms} ms)`, "dim"); |
| 136 | + log(""); |
| 137 | + log("This is the same numbers `python3 examples/manipulation/01_pick_and_retry.py --no-render` prints locally.", "dim"); |
| 138 | + } catch (e) { |
| 139 | + log("ERROR: " + e, "err"); |
| 140 | + console.error(e); |
| 141 | + } finally { |
| 142 | + runBtn.disabled = false; |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +runBtn.addEventListener("click", run); |
| 147 | +</script> |
| 148 | +<script src="https://cdn.jsdelivr.net/pyodide/v0.27.2/full/pyodide.js"></script> |
| 149 | +</body> |
| 150 | +</html> |
0 commit comments