Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Jupyter / IPython Notebook nbconvert to output PDFs that don't overflow off the page?

When I convert a Jupyter / IPython Notebook to a PDF via ipython nbconvert my-notebook.ipynb --to PDF, it mostly looks good, except that some of the long lines and all of the output just goes off the edge of the page, without wrapping. How can I get it to stop overflowing and wrap long lines?

like image 495
Jonathan Avatar asked Dec 27 '15 23:12

Jonathan


People also ask

How do I export a Jupyter Notebook to PDF?

The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML - not visible in the screenshot).

What is %% capture in Jupyter?

What does %% capture do in Jupyter? Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


1 Answers

I would suggest to use a slightly different approach, Instead of using terminal directly, Use this little Python script with GUI to get really high quality pdf from Jupyter Notebooks. It supports syntax highlighting and does not mess up Latex or graphs.

# Script adapted from CloudCray
# Original Source: https://gist.github.com/CloudCray/994dd361dece0463f64a
# 2016--06-29
# Run this script in the same folder as the notebook(s) you wish to convert
# This will create both an HTML and a PDF file
#
# You'll need wkhtmltopdf (this will keep syntax highlighting, etc)
#   http://wkhtmltopdf.org/downloads.html

import subprocess
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename

WKHTMLTOPDF_PATH = "C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf"  # or wherever you keep it

def export_to_html(filename):
    cmd = 'ipython nbconvert --to html "{0}"'
    subprocess.call(cmd.format(filename), shell=True)
    return filename.replace(".ipynb", ".html")


def convert_to_pdf(filename):
    cmd = '"{0}" "{1}" "{2}"'.format(WKHTMLTOPDF_PATH, filename, filename.replace(".html", ".pdf"))
    subprocess.call(cmd, shell=True)
    return filename.replace(".html", ".pdf")


def export_to_pdf(filename):
    fn = export_to_html(filename)
    return convert_to_pdf(fn)

def main():
    print("Export IPython notebook to PDF")
    print("    Please select a notebook:")

    Tk().withdraw() # Starts in folder from which it is started, keep the root window from appearing 
    x = askopenfilename() # show an "Open" dialog box and return the path to the selected file
    x = str(x.split("/")[-1])

    print(x)

    if not x:
        print("No notebook selected.")
        return 0
    else:
        fn = export_to_pdf(x)
        print("File exported as:\n\t{0}".format(fn))
        return 1

main()
like image 62
Philipp Schwarz Avatar answered Oct 07 '22 00:10

Philipp Schwarz