{"slug": "pyr-js-python-and-r-in-html-pages-with-one-script-tag", "title": "Pyr.js: Python and R in HTML pages, with one script tag", "summary": "Thierry Moudiki released pyr.js, a roughly 460-line single-file JavaScript library that runs Python and R code embedded in HTML pages entirely in the browser, built with Anthropic's Claude writing the code and tests from his design decisions. The library relies on Pyodide (Python compiled to WebAssembly) and webR for R, and connects code to the DOM through attributes such as py-click, py-out, r-click and r-input, with py-then and r-then passing results as data to JavaScript chart libraries including Observable Plot, Highcharts and D3. Moudiki reports that the first visit downloads several megabytes of engines and Python startup takes about 3 seconds, while hosting pyr.js on the same site lets Python run in a shared worker that other tabs and page reloads reuse.", "body_md": "# pyr.js: Python and R in HTML pages, with one script tag\n\n**, and kindly contributed to**\n\n[T. Moudiki's Webpage - R](https://thierrymoudiki.github.io//blog/2026/11/15/python/r/javascript/pyr_js)\n[R-bloggers](https://www.r-bloggers.com/)]. (You can report issue about the content on this page\n\n[here](https://www.r-bloggers.com/contact-us/))\n\nWant to share your content on R-bloggers?\n\n[click here](https://www.r-bloggers.com/add-your-blog/)if you have a blog, or\n\n[here](http://r-posts.com/)if you don't.\n\nWhat if you could put **Python** or **R** code in an HTML page, double-click the file, and have it run? No server, no build step, no install, and nothing sent anywhere: everything runs in the browser.\n\nThat’s what [`pyr.js`](https://github.com/Techtonique/pyr.js) does. I built it this week with Claude (Anthropic’s AI assistant), which wrote the code and tests from my design decisions and verified everything in a headless browser. The heavy lifting is done by [Pyodide](https://pyodide.org/) (Python compiled to WebAssembly) and [webR](https://docs.r-wasm.org/webr/latest/) (same thing for R). `pyr.js` is the glue: a single file, about 460 lines you can actually read, that finds the Python and R code in your page and connects it to your HTML.\n\n## A first example\n\nSave this as an `.html` file and open it in your browser:\n\n```\n<script src=\"https://cdn.jsdelivr.net/gh/Techtonique/[email protected]/pyr.js\"></script>\n\n<p id=\"count\">count = 0</p>\n<button py-click=\"add_one()\" py-out=\"count\">Add</button>\n\n<script type=\"text/python\">\ncount = 0\n\ndef add_one():\n    global count\n    count += 1\n    return f\"count = {count}\"\n</script>\n```\n\n`py-click` runs Python when the button is clicked, and `py-out` says where the result goes. Any DOM event works the same way (`py-input`, `py-change`, …), and for R, it’s `r-click`, `r-input`, and so on. R’s printed output and plots show up directly in the page:\n\n```\n<label>Points: <input type=\"range\" min=\"10\" max=\"500\" value=\"100\"\n       r-input=\"hist(rnorm(as.numeric(.value)))\" r-out=\"plot\"></label>\n<div id=\"plot\"></div>\n```\n\nHere, `.value` (`_value` in Python) holds the slider’s current value, and the histogram is redrawn as you move it.\n\n## Python and R compute, JavaScript draws\n\nMy favorite part: results can be passed as **data** to any JavaScript chart library. In a [previous post](https://thierrymoudiki.github.io/blog/2026/09/27/python/r/javascript/analytics-pipeline), I used [Observable Plot](https://observablehq.github.io/plot/) and [Highcharts](https://www.highcharts.com/) for dashboards. With `pyr.js`, `py-then` / `r-then` hand the result to a function of your choice. An R data frame arrives as an array of rows, ready to plot:\n\n```\n<script src=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/d3.min.js\"></script>\n<script src=\"https://cdn.jsdelivr.net/npm/@observablehq/[email protected]/dist/plot.umd.min.js\"></script>\n<script src=\"https://cdn.jsdelivr.net/gh/Techtonique/[email protected]/pyr.js\"></script>\n<script>\n  function draw(rows, el) {\n    el.replaceChildren(Plot.dot(rows, { x: \"wt\", y: \"mpg\", stroke: \"cyl\" }).plot());\n  }\n</script>\n\n<script type=\"text/r\">\ncars <- function() {\n  d <- mtcars\n  d$cyl <- factor(paste(d$cyl, \"cylinders\"))   # factors arrive as text\n  d[, c(\"wt\", \"mpg\", \"cyl\")]\n}\n</script>\n\n<div r-ready=\"cars()\" r-then=\"draw\" r-out=\"chart\"></div>\n<div id=\"chart\"></div>\n```\n\nThe same works with Python: lists of dicts, numpy arrays and pandas DataFrames are converted for you. Here’s the Observable Plot example from the repository, with a Python random walk (redrawn by a slider) and the R data frame above:\n\n## How fast is it?\n\nThe first visit downloads the engines (several MB for Python, cached by the browser afterwards), and starting Python takes about 3 seconds. Nothing magical so far.\n\nThe interesting part: when `pyr.js` is hosted on the same site as your pages, Python runs in a *shared worker*. Other tabs of the site, and even page reloads, reuse the interpreter that’s already running: in my tests (Chromium), the page was ready in **under 0.1 second** instead of 3. Each tab still has its own variables, and a reload still starts the page fresh. For R, webR doesn’t allow this (yet), so it starts with each page.\n\nThere’s also a *live editing* mode for development: put your Python code in a `.py` file, add `data-watch` to the script tag, and your changes apply to the running page as soon as you save, without reloading.\n\n## Privacy\n\npyr.js sends nothing anywhere. No analytics, no telemetry: your code and your data stay in the visitor’s browser. The only network requests are for the engines themselves and the packages your code imports.\n\n## Try it\n\nThe repository is on [GitHub](https://github.com/Techtonique/pyr.js) (MIT license), with **four examples**: the basics, [Observable Plot](https://observablehq.com/@observablehq/plot-gallery), [Highcharts](https://www.highcharts.com/) and live editing. The README explains the rest (attributes, data conversion, self-hosting the engines, pinning a version with an `integrity` hash). Feedback, issues and stars are welcome!\n\n**leave a comment** for the author, please follow the link and comment on their blog:\n\n**.**\n\n[T. Moudiki's Webpage - R](https://thierrymoudiki.github.io//blog/2026/11/15/python/r/javascript/pyr_js)\n[R-bloggers.com](https://www.r-bloggers.com/)offers\n\n**about**\n\n[daily e-mail updates](https://feedburner.google.com/fb/a/mailverify?uri=RBloggers)\n[R](https://www.r-project.org/)news and tutorials about\n\n[learning R](https://www.r-bloggers.com/how-to-learn-r-2/)and many other topics.\n\n[Click here if you're looking to post or find an R/data-science job](https://www.r-users.com/).\n\nWant to share your content on R-bloggers?\n\n[click here](https://www.r-bloggers.com/add-your-blog/)if you have a blog, or\n\n[here](http://r-posts.com/)if you don't.", "url": "https://wpnews.pro/news/pyr-js-python-and-r-in-html-pages-with-one-script-tag", "canonical_source": "https://www.r-bloggers.com/2026/11/pyr-js-python-and-r-in-html-pages-with-one-script-tag/", "published_at": "2026-11-16 04:09:10+00:00", "updated_at": "2026-11-16 04:22:37.423761+00:00", "lang": "en", "topics": ["artificial-intelligence", "developer-tools", "ai-tools"], "entities": ["pyr.js", "Thierry Moudiki", "Techtonique", "Claude", "Anthropic", "Pyodide", "webR", "Observable Plot"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/pyr-js-python-and-r-in-html-pages-with-one-script-tag", "markdown": "https://wpnews.pro/news/pyr-js-python-and-r-in-html-pages-with-one-script-tag.md", "text": "https://wpnews.pro/news/pyr-js-python-and-r-in-html-pages-with-one-script-tag.txt", "jsonld": "https://wpnews.pro/news/pyr-js-python-and-r-in-html-pages-with-one-script-tag.jsonld"}}