Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a presentable PDF from a Jupyter Notebook?

I know that I can click on File -> Print Preview and let my browser save the result as a PDF. But that PDF will contain quite a bit of code.

Can I hide parts from the print preview or make a PDF with another tool?

Ideally, there would be magic functions:

  • %hide: Hide the cell and its output
  • %hide-code: Hide only the code of the cell, but show the output of the code
like image 616
Martin Thoma Avatar asked Sep 10 '25 17:09

Martin Thoma


1 Answers

Print to PDF in the browser

In JupyterLab, you can hide the cell's input and output by clicking the side bar as demonstrated in the doc. After hiding, you can use the browser's printing menu to generate a PDF. I've tested it on FireFox and expect similar result on Chrome or Edge.

enter image description here

Export to PDF with nbconvert

If you aim at a native conversion from ipynb format to pdf, the nice old nbconvert tool is your friend. It usually comes together with Jupyter and can be invoked conveniently in the menu of JupyterLab File->Export Notebook As...->PDF or, in the classic Jupyter Notebook interface, File->Download as->PDF (via LaTeX). Hiding input/output in cells can be realized by setting the cell's metadata hide_input=true and installing a nbextension as discussed in issue #155.

Print Preview menu in the classical Jupyter

Finally to your question

Can I hide parts from the print preview or make a PDF with another tool?

The "print preview" button in the classical Jupyter interface is a shortcut for calling nbconvert to generate a HTML file and redirect your browser to it. Therefore, similar configuration for nbconvert to hide input as discussed above (in issue #155) can be used. This feature is implemented by jupyter_contrib_nbextensions bundle and explained in the doc.

like image 147
gdlmx Avatar answered Sep 13 '25 09:09

gdlmx