Jupyter Notebook extension - %%stan magic

I have created a very basic %%stan magic extension to maintain the stan worklow within jupyter notebook . Once a PR for codemirror stan mode gets through (syntax highlighting/autocomplete) could be enabled.

Install: pip install git+https://github.com/Arvinds-ds/stanmagic.git
Extension page: https://github.com/Arvinds-ds/stanmagic
Sample notebook: https://github.com/Arvinds-ds/stanmagic/blob/master/StanMagic-Help.ipynb

Its very preliminary and more of a hacked POC but some may find it useful as I have during the past week.

9 Likes

Have also created a basic notebook extension to do automatic syntax highlighting for %%stan code cells

https://github.com/Arvinds-ds/stan_code_helper.git

https://github.com/Arvinds-ds/stan_code_helper/raw/master/stan_code_helper.png

3 Likes

Hi, I could install and enable it. But failed to load it.

jupyter nbextension list

shows

  Known nbextensions:
  config dir: /Users/ahartikainen/.jupyter/nbconfig
    notebook section
      stan_code_helper/main  enabled 
      - Validating: OK
  config dir: /Users/ahartikainen/miniconda2/envs/py36/etc/jupyter/nbconfig
    notebook section
      jupyter-js-widgets/extension  enabled 
      - Validating: OK
      jupyter-vega/index  enabled 
      - Validating: OK

So I guess I have something wrong with my jupyter / ipython? Any ideas.

What is the error you are getting?. Your extension log looks ok to me. You need to install the Stanmagic extension first. stanmagic installs the extension for saving/compile checking stan code in Jupyter notebooks. stan_helper_code is some javascript code that enables syntax highlighting. It is an optional extension that requires stanmagic. I have got a couple of queries on this. I have updated the README to highlight this requirement.

ModuleNotFoundError

I just edited my reply above…

Great, it works. I forgot to install stanmagic.

Thanks for all the feedback. Didn’t realize there were many pystan users!. Thanks to @ahartikainen for suggesting improvements. I have updated the stanmagic package (https://github.com/Arvinds-ds/stanmagic.git) with few of your requests including syntax colored HTML formatting of stan code output using pygments.

2 Likes

The more people get involved writing cool new features, the more PyStan users we’ll have!

So, thanks!

I note that this hasn’t kept up with the most recent pystan. Are there any such plans (or similar for jupyterstan)?

since then we’ve got CmdStanPy and a bunch of notebooks as well - example-models/jupyter/covid-inf-rate at master · stan-dev/example-models · GitHub

Yep – I use stan within Jupyter all the time – I mostly just miss syntax highlighting!

1 Like

now I understand - GitHub - Arvinds-ds/stanmagic: Stan Jupyter Magic needs to be updated - did you file an issue there? @aravind, happy to see how this could be modified to work with CmdStanPy.

I’ve forked jupyterstan to support cmdstanpy instead of pystan, but I was unable to get the syntax highlighting to work. I think this may be related to the JupyerLab 3 release.

As at least some form of a workaround for this, I added %stanf to load an existing stan file, and this should highlight using pygments. Very happy to continue working on this, but I’m having a hard time finding ‘prior art’ on the syntax highlighting part of this

1 Like

worked on my machine - this is awesome!

I have also now just put together a quick syntax highlighter for .stan files here:

Now, all that needs to be done is compose the two (syntax highlighting in %%stan cells) and the Jupyter integration will be pretty slick.
Example: