I have been using Jupyter notebook for a while and I found that opening ipynb files for quick reference is not straightforward. Jupyter notebook has a functionality to download into the familiar and easy to share formats such as html or pdf. However, it is cumbersome to download each and every file manually, especially when you have multiple files.
In this post, I demonstrate step by step automated Jupyter notebook file conversion into html or pdf from command line. Using command line code, we can automatically convert all notebook files with “. ipynb" extension that were saved in a given directory or subdirectories. So that the converted html or pdf files can easily be opened and/or shared to others who do not use Jupyter notebook.
First, install the nbconvert library. The library can be used to convert “.ipynb” files into other formats such as html, LaTeX, pdf, Markdown and others. The full documentation for nbconvert is found here.
nbconvert can be installed as:
- pip install nbconvert
or
- conda install nbconvert
The nbconvert uses Pandoc and TeX to convert markdown to formats other than HTML and to convert to PDF, respectively.
Once the packages are installed, we can automatically convert the ".ipynb" files found in a directory or subdirectory using the following command line syntax. The first line is to convert the notebooks to html and the second is to convert to pdf:
1) To convert a single ipynb file:
jupyter nbconvert --to html ipynb_file
jupyter nbconvert --to pdf ipynb_file
Make sure that the file is in the current working directory otherwise we need to provide the file path.
2) To convert all ipynb files in a directory:
jupyter nbconvert --to html directory_path/*.ipynb
jupyter nbconvert --to pdf directory_path/*.ipynb
The “directory_path” here refers to the path of the directory where the "ipynb" files are located. The wildcard character (*) before the ".ipynb" refers to all files with ".ipynb" extension found in the directory.
3) To convert all ipynb files in subdirectories:
jupyter nbconvert --to html directory_path/*/*.ipynb
jupyter nbconvert --to pdf directory_path/*/*.ipynb
The “directory_path” refers to the path of the directory containing subdirectories and the wildcard character (*) after the “directory_path” represents the subdirectories where the "ipynb" files are located. The second wildcard (*) refers to all files with ".ipynb" extension.
I hope the step by step automated Jupyter notebook file conversion helps you to convert notebooks into html or pdf files. Html and/or pdf files have the advantage that can be easily accessed from different platforms. The html format takes more memory than the pdf format. But it only needs nbconvert and pandoc to be installed (relatively easy to convert), but for pdf you also need TeX. Depending on your need and computer memory, you can decide to which format to convert.