Setup nbconvert

From HPCWIKI
Jump to navigation Jump to search

nbconvert

The nbconvert tool[1] (jupyter nbconvert) converts notebooks to various other formats via Jinja templates. The nbconvert tool allows you to convert an .ipynb into

  • HTML
  • LaTeX
  • PDF
  • Reveal JS
  • Markdown (md)
  • ReStructured Text (rst)
  • executable script
  • etc

Setup nvconvert on Ubuntu

#Install pandoc 
sudo apt-get install pandoc

# install nbconvert with webpdf
sudo -H pip install --upgrade nbconvert[webpdf] pyppeteer

#Running the tests after a dev install above:
pip install nbconvert[test]
py.test --pyargs nbconvert

Usage

General command line usage is, $ jupyter nbconvert --to <output format> <input notebook>

# convert notebook to HTML, then load HTML on browser to print as PDF
jupyter nbconvert --to html HPCMATE.ipynb

# convert notebook to webpdf
jupyter nbconvert --to webpdf HPCMATE.ipynb

# convert notebook to webpdf, log_level parameter would point out if you have problem
jupyter nbconvert --to pdf --debug HPCMATE.ipynb


Further details is available at https://nbconvert.readthedocs.io/en/latest/usage.html

Common Error

Package version dependency

# common errors 1, 

/usr/local/lib/python3.8/dist-packages/nbconvert/utils/pandoc.py:51: RuntimeWarning: You are using an unsupported version of pandoc (2.5).
Your version must be at least (2.14.2) but less than (4.0.0).

# check pandock release information at https://github.com/jgm/pandoc/releases
# then download proper version of deb file and install with dpkg -i 
wget https://github.com/jgm/pandoc/releases/download/3.1.11/pandoc-3.1.11-1-amd64.deb
$ sudo dpkg -i pandoc-3.1.11-1-amd64.deb

PDF convert error with invalid characters

even all system level package and configuration has been successful, actual conversion into PDF may not successful because of "invalid character" in side of notebook. and seems not clear anwer yet from community[2]

Customize nbconvert[3]

By default, nbconvert finds templates from a few locations and you can check where they are

# find template location
from jupyter_core.paths import jupyter_path
print(jupyter_path('nbconvert','templates'))
['/opt/hpcmate/home/.local/share/jupyter/nbconvert/templates', '/usr/local/share/jupyter/nbconvert/templates', '/usr/share/jupyter/nbconvert/templates']


# display template structure 
from IPython.display import HTML, display
with open('template_structure.html') as f:
    display(HTML(f.read()))

References