HomeData EngineeringData DIYConverting Jupyter Notebooks into PDF

Converting Jupyter Notebooks into PDF

Converting Jupyter Notebooks to PDFs in a few lines (debugging ‘500: Internal Server Error’)

If you’re one of many data scientists looking for a job, you might find yourself working on a data science take-home assignment. Instead of sharing your Jupyter Notebooks, it would be neater if you could convert the notebooks and submit the pdf version. In this blog, I want to share how you can turn Jupyter Notebooks into pdf format in a few lines!

A previous take-home assignment got me to learn how to do this
Converting Jupyter Notebooks into PDF 1

Install nbconvert and LaTeX

nbconvert allows users to convert Notebooks to other formats. You would think after installing nbconvert, and you are good to go…right? If it’s that simple, why would I be writing this post?

After I installed nbconvert, I received an error saying “500: Internal Server Error.” The reason why you see this error is that you haven’t installed LaTeX or Pandoc yet. I decided to download LaTeX. The good thing about downloading LaTeX is that it makes your analysis looks like a research paper, which is very legit.

Run nbconvert from the Command Line

After you installed these two packages, you can try to convert your notebook into the format you want using this command line:

$ jupyter nbconvert --to FORMAT notebook.ipynb

This command line will convert the Jupyter notebook file into the output format given by the FORMAT string. For the purpose of this article, we will convert it into pdf, but you can also convert it into HTML, Markdown, etc. If you want to convert the notebook file into HTML, simply replace pdf as html , and vice versa.

$ jupyter nbconvert --to pdf notebook.ipynb

Depending on the packages you’ve previously installed, you may need to downgrade or upgrade some of them.

For example, if you see this error message…

spyder 3.3.6 has requirement pyqt5<5.13; python_version >= "3", but you'll have pyqt5 5.13.0 which is incompatible

You can resolve this error by uninstalling pyqt5 and install pyqt5==5.12.0 instead.

Install a nbconvert Template

After I successfully converted the notebook into a pdf file, I realized that the margin of the file was off! Luckily I double-checked the file before I submitted it!

Here’s the comparison. By default, we can see that the margin is very off. The default pdf format cut off the side of the page.

Converting Jupyter Notebooks into PDF 1

To resolve this problem, I found a template that was created by Tyler Makaro. I first installed his nb_pdf_template by running a command line: pip install nb_pdf_template. Then I cd into the LaTeX folder and added the two templates that Tyler created: classic.tplx and classicm.tplx. Last but not least, don’t forget to delete the default article.tplx from the LaTeX folder.

Once this is done, you will be able to get the nicely formatted pdf by running this command line:

$ jupyter nbconvert --to pdf notebook.ipynb --template classic

Conclusion

Here are all the packages you’ll need:

  1. Install nbconvert
  2. Install LaTeX or Pandoc
  3. Install a nbconvert template

There you have it! If you find this helpful, please share and follow my blog so you won’t miss out!

Until next time, happy learning!

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Most Popular