Customize the printed version of a jupyter notebook

Viewed 74

Working with jupyter (formerly ipython) notebooks is extremely useful for me. I use them for developing code or running preliminary analysis of small data samples. I keep handwritten notes of my findings in a paper laboratory notebook (I'm a scientist working in a lab) and from time to time I would like to print a notebook with results in order to add it to my notes and keep track of my work. I mostly use numpy, scipy and create figures using matplotlib and bokeh.

However, whenever I try to print a copy of the jupyter notebook it looks badly formatted. In particular, figures that extend beyond the page's width tend to be cropped, meaning that I loose the most valuable information I'm trying to keep by printing the notebook. This is, by far, the more aggravating thing; bad font sizes and page breaks, although aesthetically displeasing, do not imply a loss of information.

Is there a way to make the printed version to automagically scale down those figures to fit width? I wouldn't want to do it manually as a single notebook usually contains between 10 to 20 figures.

So far I've tried:

  • printing from the browser while in the jupyter notebook
  • exporting the notebook to html format and then printing from that
  • converting to pdf via jupyter nbconvert

but I cannot seem to achieve what I'm looking for.

I think there might be a way to do it by (programmatically) tinkering with the html version by changing the css, but although I'm versed in programming I have no experience with css.

Thanks in advance for all your help!

1 Answers

You can try to use the --EmbedImagesPreprocessor.resize option in nbconvert.

  • This only works if your images are png, jpg or another raster format image.
  • Install the PIL python package first, if it is not already installed.
  • Valid sizes are small, mid and large

Example:

$ jupyter nbconvert --to html --EmbedImagesPreprocessor.embed_images=True
    --EmbedImagesPreprocessor.resize=large my_notebook.ipynb

Reference: https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/exporting.html

Related