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!
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
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
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: