This post describes how to integrate executable WebPPL code boxes into HTML documents, generated with Quarto.
What’s WebPPL?
WebPPL is a light-weight probabilistic programming language, which is built on top of Javascript. It is great for fast-prototyping or just trying out ideas about stochastic processes. You can just run it in a browser and it even provides intelligent visualizations of many common data formats.
For example, the WebPPL code calculates the Bayesian solution for the 3-card puzzle:
// three cards; with blue or red on either side var cards = [["blue", "blue"], ["blue", "red"], ["red", "red"]] var model = function() { var card = uniformDraw(cards) var color = uniformDraw(card) condition(color == "blue") return card.join("-") } viz(Infer({method: "enumerate", model: model }))
WebPPL, or tools like it, are extremely useful also for teaching: no installation requirements, no fuzz with visualization, direct hands-on exploration of stochastic processes and Bayesian inference. Consequently, several instructional web-books have used WebPPL for interactive illustrations and exercises, e.g., on probabilistic models of cognition, probabilistic language understanding, or statistics (see here or here).
What’s Quarto?
Quarto is a relatively new “open-source scientific and technical publishing system” [from the Quarto website] and may become a more general alternative to replace Jupyter notebooks or Rmarkdown. I’ve used Jupyter-notebook based webbooks and Rmarkdown-based webbooks for educational material in my classes, and it worked fine. But I also recently tried Quarto and found the ease, portability and overall configurability improved (totally subjective, I guess).
Since I will likely use Quarto in the future, and since others likely will, too; and since I would love to see more people use tools like WebPPL for education in stats and probabilistic modeling, this post describes how to integrate executable WebPPL code boxes into HTML documents, generated with Quarto.
Integrating WebPPL in Quarto documents
Here’s what you need to do to get interactive WebPPL code boxes and outputs into your HTML-output from QMD files.
- In the YML preamble of your QMD file, include the following:
- Wrap all WebPPL code in `<pre class="webppl"> … </pre>`.
- Include the following JS-script at the end of your QMD file:
Full example
Here is a full example of how your QMD file might look like.