{"slug": "in-browser-code-playgrounds", "title": "In-Browser Code Playgrounds", "summary": "Codapi creator Anton Zhiyanov added embeddable in-browser code playgrounds for JavaScript, Python, PHP, Ruby, Lua and SQLite, running code entirely client-side through WebAssembly System Interface (WASI) binaries instead of a server. The playgrounds pair WASI-compiled language runtimes — Python 3.12 at 26.3 MB, PHP 8.2 at 13.2 MB, Ruby 3.2 at 24.5 MB and Lua 5.4 at 330 KB — with Ben Taylor's 27 KB Runno WASI runtime, and the JavaScript widget was modified to support pluggable engines. Zhiyanov said container2wasm, which converts Docker images into WASI binaries, was rejected for now because it produces 100+ MB binaries even for the smallest Alpine-based images.", "body_md": "# In-browser code playgrounds\n\nI'm a big fan of interactive code snippets in all kinds of technical writing, from product docs to online courses to blog posts. Like this one:\n\n``` python\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\ngreet(\"World\")\n```\n\nIn fact, I even built an open source tool called Codapi<sup>[1](#fn:1)</sup> for embedding such snippets.\n\nTypically, a code playground consists of a client-side widget and a server-side part that executes the code and returns the result:\n\n``` python\n  browser\n┌───────────────────────────────┐\n│ def greet(name):              │\n│   print(f\"Hello, {name}!\")    │\n│                               │\n│ greet(\"World\")                │\n└───────────────────────────────┘\n  Run ►\n    ↓\n  server\n┌───────────────────────────────┐\n│ docker run codapi/python      │\n│ python main.py                │\n└───────────────────────────────┘\n    ↓\n  browser\n┌───────────────────────────────┐\n│ Hello, World!                 │\n└───────────────────────────────┘\n```\n\nPersonally, I'm quite happy with this setup. But often people prefer not to depend on a server and run the code entirely in the browser. So I decided to look into it and implemented embeddable in-browser code playgrounds for JavaScript, Python, PHP, Ruby, Lua, and SQLite.\n\n## \n\nThe modern way to run arbitrary programs in the browser seems to be WebAssembly System Interface (WASI<sup>[2](#fn:2)</sup>) — an executable binary format based on WebAssembly. With WASI, you compile a program (originally written in C, Rust, Go, or some other language) into a WASI binary and then run it with a WASI runtime (there are a number of these runtimes from different vendors).\n\nJust as we can compile an arbitrary program into WASI binary, we can take a language interpreter like Lua or CPython, compile it into WASI, and run it with the WASI runtime to execute Lua or Python code. In practice, however, it's not that easy, because WASI compilers do not (yet) implement all the features of traditional compilers like GCC.\n\nFortunately, VMWare Labs has already done the hard part and compiled PHP<sup>[3](#fn:3)</sup>, Python<sup>[4](#fn:4)</sup> and Ruby<sup>[5](#fn:5)</sup> into WASI. So all I had to do was publish the WASI binaries as NPM packages to make them available on the CDN. I've also compiled Lua<sup>[6](#fn:6)</sup> and SQLite<sup>[7](#fn:7)</sup> to WASI.\n\nThere is also Kohei Tokunaga's container2wasm initiative<sup>[8](#fn:8)</sup>, which converts arbitrary Docker images into WASI binaries. It looks promising, but it generates 100+ MB binaries for even the smallest Alpine-based images. And since downloading hundreds of megabytes just to read an interactive article is probably not the best idea, this approach is not very practical (yet).\n\nLanguage runtimes compiled into WASI are one part of the equation. The other one is the WASI runtime (the thing that runs the binaries) capable of working in the browser. I chose the Runno<sup>[9](#fn:9)</sup> runtime by Ben Taylor because it's simple and lightweight (27 KB).\n\nThe last step was to modify the JavaScript widget<sup>[10](#fn:10)</sup> to support pluggable engines (WASI is one of them).\n\nAnd that was it!\n\n## \n\nHere are some interactive code snippets implemented as described above. Note that the language runtime is downloaded when you click the Run button, so the first run may take some time. Subsequent runs are almost instantaneous.\n\n### Python\n\nExecutes the code using the Python 3.12 WASI runtime (26.3 MB).\n\n``` python\ndef greet(name):\n    print(f\"Hello, {name}!\")\n\ngreet(\"World\")\n```\n\n### PHP\n\nExecutes the code using the PHP 8.2 WASI runtime (13.2 MB).\n\n```\nfunction greet($name) {\n    echo \"Hello, $name!\";\n}\n\ngreet(\"World\");\n```\n\n### Ruby\n\nExecutes the code using the Ruby 3.2 WASI runtime (24.5 MB).\n\n``` python\ndef greet(name)\n  puts \"Hello, #{name}!\"\nend\n\ngreet(\"World\")\n```\n\n### Lua\n\nExecutes the code using the Lua 5.4 WASI runtime (330 KB).\n\n```\nfunction greet(name)\n  print(\"Hello, \" .. name .. \"!\")\nend\n\ngreet(\"World\")\n```\n\n### JavaScript\n\nExecutes the code using the AsyncFunction<sup>[11](#fn:11)</sup>.\n\n``` js\nconst greet = (name) => {\n    console.log(`Hello, ${name}!`);\n};\n\ngreet(\"World\");\n```\n\n### Fetch\n\nExecutes the code using the Fetch API<sup>[12](#fn:12)</sup>.\n\n```\nPOST https://httpbingo.org/dump/request\ncontent-type: application/json\n\n{ \"message\": \"hello\" }\n```\n\n### SQLite\n\nExecutes the code using the SQLite 3.44 WASI runtime (2.1 MB).\n\n```\nselect id, name, department\nfrom employees\norder by id limit 3;\n```\n\n## \n\nBecause the WASI runtime plugs into the existing architecture, WASI-powered code snippets support advanced Codapi features such as templates or code cells.\n\nTemplates<sup>[13](#fn:13)</sup> allow you to hide some code behind the scenes and show only the relevant part. For example, in the SQLite example above, the `employees` table is created as part of the template, so the snippet can take it for granted:\n\n```\nselect id, name, department\nfrom employees\norder by id limit 3;\n```\n\nCode cells<sup>[14](#fn:14)</sup> allow you to make code snippets depend on each other. For example, the first snippet defines the `wrap` function, while the second snippet uses it:\n\n``` python\nimport textwrap\n\ndef wrap(text, width=20):\n    \"\"\"Wraps the text so every line is at most width characters long.\"\"\"\n    return textwrap.fill(text, width)\ntext = (\n    \"Python is a programming language that lets you work quickly \"\n    \"and integrate systems more effectively.\"\n)\nprint(wrap(text))\n```\n\n## \n\nTo use native browser playgrounds (e.g. JavaScript or Fetch), include the `snippet.js` script and add the `codapi-snippet` element next to the static code example. Use the `browser` engine:\n\n```\n<pre>console.log(\"hello\")</pre>\n\n<codapi-snippet engine=\"browser\" sandbox=\"javascript\" editor=\"basic\">\n</codapi-snippet>\n\n<script src=\"https://unpkg.com/@antonz/codapi@0.12.0/dist/snippet.js\"></script>\n```\n\nTo use WASI-powered playgrounds (e.g. Python or SQLite), include two additional scripts and use the `wasi` engine:\n\n```\n<pre>print(\"hello\")</pre>\n\n<codapi-snippet engine=\"wasi\" sandbox=\"python\" editor=\"basic\"></codapi-snippet>\n\n<script src=\"https://unpkg.com/@antonz/runno@0.6.1/dist/runno.js\"></script>\n<script src=\"https://unpkg.com/@antonz/codapi@0.12.0/dist/engine/wasi.js\"></script>\n<script src=\"https://unpkg.com/@antonz/codapi@0.12.0/dist/snippet.js\"></script>\n```\n\nTo switch from in-browser to server-side playgrounds (which can run virtually any software), remove the `engine` attribute:\n\n```\n<pre>\nfn main() {\n    println!(\"Hello, World!\");\n}\n</pre>\n\n<codapi-snippet sandbox=\"rust\" editor=\"basic\"></codapi-snippet>\n```\n\nSee the documentation<sup>[15](#fn:15)</sup> for details.\n\n## \n\nWASI-powered sandboxes allow code snippets to run completely in-browser, with no server involved. They may take some time and traffic to initialize the runtime, but after that they run almost instantly.\n\nAs implemented in Codapi, they fit nicely into the overall architecture, providing access to features like templates and code cells. You can also easily switch from a browser-side to a server-side execution model.\n\nGive them a try!\n\n[Playgrounds](https://github.com/nalgeon/codapi-js/blob/main/docs/browser-only.md) •\n[Snippet widget](https://github.com/nalgeon/codapi-js) •\n[About Codapi](https://codapi.org/)\n\n[★ Subscribe](https://antonz.org/subscribe/) to keep up with new posts.", "url": "https://wpnews.pro/news/in-browser-code-playgrounds", "canonical_source": "https://antonz.org/in-browser-code-playgrounds/", "published_at": "2026-12-02 09:06:44+00:00", "updated_at": "2026-12-02 09:18:31.246769+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["Codapi", "Anton Zhiyanov", "WebAssembly System Interface", "Runno", "Ben Taylor", "VMware Labs", "container2wasm", "Kohei Tokunaga"], "also_reported_by": [], "takeaway": "Compile language interpreters to WASI and run them with a lightweight in-browser runtime like the 27 KB Runno to embed code playgrounds without a server; runtimes range from 330 KB (Lua 5.4) to 26.3 MB (Python 3.12) and download on first run.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/in-browser-code-playgrounds", "markdown": "https://wpnews.pro/news/in-browser-code-playgrounds.md", "text": "https://wpnews.pro/news/in-browser-code-playgrounds.txt", "jsonld": "https://wpnews.pro/news/in-browser-code-playgrounds.jsonld"}}