16

What is the best way to print an iPython notebook (.ipynb) that contains a lot of figures/plots, photos, and code that would appear with a horizontal scroll bar? I've tried converting them to HTML, slides, PDF, etc, but neither has produced a decent output. For example, the slides have super-large font/zoom such that one page has no more than 5 lines of text in it. I've tried GitPrint, but that's only good for markdown (md) files. I've tried converting ipynb to tex and using pdflatex to convert to PDF, but there are a lot of errors and I keep getting stuck with a question mark prompt (?). When I hit enter through them, the output doesn't contain the photos. So what is the best way here? I don't care about the extension, only that it looks good (like the ipynb) on paper.

Goh-shans
  • 125
  • 1
  • 12
Goosal Tapal
  • 205
  • 1
  • 2
  • 5

3 Answers3

8

You can use nbconvert to convert the ipynb file into HTML and the question will be: How to pretty print a HTML file? And print a HTML file would be much easier compared with printing a ipynb file.

Command is like this:

jupyter nbconvert --to html filename.ipynb
kingbase
  • 1,268
  • 14
  • 23
0

I think I've found a decent solution as I was stuck with the same problem. For an aesthetically pleasing print-out of the Jupyter notebook (.pdf format) for the usage of study and learning (as lecture slides), I recommend take a print-out using your web-browser (Chrome: Print=Ctrl+P).

Outcome: an aestheically pleasing document, containing all codes, pictures as embedded within a Jupyter notebook.

tl:dr Avoid any conversion within the Jupyter notebook option; print straight out of web-browser.

Sumax
  • 631
  • 1
  • 7
  • 13
  • 4
    directly printing the chrome page is not a decent solution. As it is not properly formatted for fitting into proper page layout – Abdul Rehman Oct 24 '19 at 10:40
  • 1
    This does not work well in JupyterLab (1.2) but works in classic notebook. In JupyterLab select Help -> Launch classic notebook, then from your *browser* menu (e.g. 3 dot menu) choose print and print to pdf. This will paginate with proper margins. – Andrew Olney Sep 03 '20 at 17:14
0

The easiest and flexible way is to use CSS. For example, put the following code in a cell and run. I will remove all code blocks and output blocks. In this way you can control what you want.

%%html
 <style>
 div.input {
      display: none;
      padding: 0;
    }

    div.output_prompt {
      display: none;
      padding: 0;
    }

    div.text_cell_render {
      padding: 1pt;
    }

    div#notebook p,
    div#notebook,
    div#notebook li,
    p {
      font-size: 11pt;
      line-height: 135%;
      margin: 0;
    }

    .rendered_html h1,
    .rendered_html h1:first-child {
      font-size: 16pt;
      margin: 7pt 0;
    }

    .rendered_html h2,
    .rendered_html h2:first-child {
      font-size: 14pt;
      margin: 6pt 0;
    }

    .rendered_html h3,
    .rendered_html h3:first-child {
      font-size: 13pt;
      margin: 6pt 0;
    }

    div.output_subarea {
      padding: 0;
    }
 </syle>
shin
  • 31,901
  • 69
  • 184
  • 271