Notebook preamble injection
See original GitHub issueIs your feature request related to a problem? Please describe.
To keep my notebook pages mutually consistent, I have a fairly lengthy preamble cell that handles things like common imports, setting matplotlib styling and figure output format, defining helper functions, setting random seeds, and so on.
Some of this code is necessary for ensuring the correctness and reproducibility of the notebook contents (eg it won’t work if you don’t import numpy as np
), and some of it is purely cosmetic. (More on this below).
At this point, my preamble getting to be pretty long (>50 lines), and is definitely not the sort of thing that should be copy-pasted into each notebook page.
My solution for now is to factor all of the common preamble code into a dedicated setup.ipynb
, and use
%run /path/to/setup.ipynb
magic to inject its contents to each page.
This generally works great for developing notebooks. However, this will obviously cause a huge problem if a reader tries to download the .ipynb
file for a page (or launch on binderhub, colab, etc), where the preamble file may not exist.
Describe the solution you’d like
Would it be possible for jb to modify notebooks at compile-time? I’m specifically thinking of the following modifications:
- Add a cell tag which would be stripped out by jb. A
%run ../setup.ipynb
cell could then be used for development, but stripped out at compile time and not appear on the rendered site. - Allow the author to specify a preamble notebook in
_config.yml
which is injected ahead of the first cell in the compiled notebook.
I imagine fancier schemes could also work (eg detecting and stripping specific magics, or monkeypatching them somehow), but the scheme I’m proposing seems a bit simpler and less likely to go horribly wrong. As far as I can tell, it would be sufficient to work around the download/execute problem I described above.
Describe alternatives you’ve considered
I guess I could go back to copy-pasting.
Additional context
As mentioned above, some of the preamble contents are necessary (and informative to a user), and some are purely cosmetic and could be hidden (eg collapsed) by default. It’d be great if it’s possible to do this kind of compile-time injection in a way that lets the author specify this, eg by cell tags for hidden/collapsed.
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (8 by maintainers)
Top GitHub Comments
Right. Even then, that would only solve things confined to python. Another use-case (though one that my
%run
hack does not seem to allow for, but maybe there’s a clever way to do it) is to have a shared preamble cell that sets up common tex macros for mathjax. I get that you’re supposed to do this in_config.yml
, but that’s not too helpful for development because nothing renders inside jupyter as you’re writing.Just a note that @matthew-brett had some links to other implementation of this feature in R, as well as some feedback, here: https://github.com/executablebooks/meta/discussions/172
Given how common this is in other tools like R, as well as the fact that many people have asked for it, I wonder if we could do something like:
notebook_preamble
key that could:kernel
:code
pairs that would choose the code to add to each notebook based on its kernel.