[Showcase] Stan embedded in Jupyter

I’ve been working on improving the workflow of using stan from within the Python ecosystem. My latest effort involves two extensions for iPython and JupyterLab which allow you to easily edit and run stan programs as part of a notebook file.

The first, cmdstanjupyter, 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, jupyterlab-stan-highlight provides a CodeMirror syntax highlight 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 and cmdstanpy installed, you only need to run two commands:

pip install cmdstanjupyter
jupyter labextension install jupyterlab-stan-highlight (this command may require you to have nodejs installed)

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

Note: you may need to select a different cell and then click back on the %%stan cell to recieve highlighting when you first create it. This is still a work in progress!

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