[Showcase] Stan embedded in Jupyter

cmdstanjupyter, is a combination of two ideas.
The first is an iPython extension which adds the ability to use the magic command %%stan and create/compile a CmdStanPy object from text directly in the notebook. This is done by creating a hidden folder called .stan and placing files in there, so you still ultimately have the models saved on disk.

The second is a JupyterLab extension that provides a CodeMirror syntax highlighter for stan and enables it in both .stan files and %%stan magic cells. This basically turns a %%stan cell into a tiny stan IDE - indentation with C-[ and C-], comment toggling with C-/, and bracket matching are all available.

The end result of using both is shown in this quick demo which runs an example from McElreath’s Statistical Rethinking:

How to install and use:
Assuming you already have JupyterLab (version 4) and cmdstanpy installed, you only need to run one command:

pip install cmdstanjupyter

You can then open any notebook and run %load_ext cmdstanjupyter and make your %%stan cells!

R Users:
Anyone using R in Jupyter through IRKernel can use the syntax highlighting plugin for .stan files, but unfortunately magic commands are not (and will never be) supported by IRKernel, so the %%stan cells are python-only.

16 Likes

I should note some of the prior art in this area:

  • Previous attempts:
    • The %%stan magic was first implemented for pystan by Jan Freyberge here. I adapted it to cmdstanpy - pystan users can probably still use his version with jupyterlab-stan-highlight, though I have not tested this
    • His efforts were preceded by Aravind, who implemented a basic version for the now-outdated Jupyter Notebook interface (see this forum post)
    • My syntax highlighter was based on a plugin for the stata language, discussed in this github issue for highlighting stan
1 Like

This looks great, thanks @WardBrian!

Just wanted to also mention that R users who prefer R markdown can use the CmdStan engine for R markdown provided by CmdStanR:

2 Likes

Great news!

What if you would use .cmdstan folder? We already use that for cmdstanpy/cmdstanr related stuff?

The .stan folder is currently created in the same folder as the notebook, not a home folder. That can change, of course, but I like it as more local

This is great! It’s nice to have syntax highlighting in a new standalone package. I also like the addition of the stanfile magic.

1 Like

This is brilliant! Thanks for this work – the continual improvement in Stan’s development tools is promising.

@WardBrian any intentions of putting this up on PyPi instead of having to hit your Github directly?

I should get it on pypi - I initially was waiting to see if I could bundle them together into one package to simplify the install, but after trying I think thats either not possible or very non-obvious. I’ll try to get it on there soon

Edit: Should be good to go cmdstanjupyter · PyPI

After a long time where this project had fallen out of date with changes to the Jupyter ecosystem, I recently completed an upgrade that allows it to work with the latest versions:
https://pypi.org/project/cmdstanjupyter/2.0.0/

Those of you who saw this thread years ago may remember that it was (at the time) two separate extensions that needed to be installed separately. Besides just updating to newer versions, this release also combines them into one place (deprecating the old jupyterlab-stan-highlight)

As a reminder, this allows you to combine Stan code and python into one notebook, with the Stan files compiled by cmdstanpy automatically when the cell is run:

4 Likes