2

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
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

2 Answers2

4

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.

gdlmx
  • 6,479
  • 1
  • 21
  • 39
2

As the above-mentioned answers do not work for me; I found another solution:

I use an additional package notebook-as-pdf.

Read the tutorial here. It uses Chromium and is the only approach that worked for me so far reliably. The "print preview" does still not work. But it produces a clean PDF with no code input.

I do:

  1. pip install -U notebook-as-pdf
  2. pyppeteer-install
  3. jupyter nbconvert --to PDFviaHTML --TemplateExporter.exclude_input=True PATH_TO_YOUR_FILE.ipynb

What did NOT work for me:

  1. The solution above.
  2. All the scripts mentioned in those answers
Createdd
  • 865
  • 11
  • 15