Jupyter Notebook nbconvert - BKJackson/BKJackson_Wiki GitHub Wiki

Running notebooks from the command line

$ jupyter nbconvert --to notebook --inplace --execute mynotebook.ipynb  

General format (options: LaTeX, PDF, executable script, notebook, and others)

$ jupyter nbconvert --to FORMAT notebook.ipynb  

$ jupyter nbconvert --to script mynotebook.ipynb   

Default output format is HTML

$ jupyter nbconvert notebook.ipynb  

Clear output of notebook from the command line

Clear current notebook:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace Notebook.ipynb  

Clear other output notebook called NotebookNoOut.ipynb:

jupyter nbconvert --ClearOutputPreprocessor.enabled=True \
  --to notebook --output=NotebookNoOut Notebook.ipynb

Create default nbconvert config file

jupyter nbconvert --generate-config

Post save hook for saving .py and .html versions of Jupyter notebooks Source

post-save-hook.py:

import os
from subprocess import check_call

def post_save(model, os_path, contents_manager):
    """post-save hook for converting notebooks to .py and .html files."""
    if model['type'] != 'notebook':
        return # only do this for notebooks
    d, fname = os.path.split(os_path)
    check_call(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)
    check_call(['ipython', 'nbconvert', '--to', 'html', fname], cwd=d)

c.FileContentsManager.post_save_hook = post_save

More great tips and tricks by Jonathan Whitmore at his github and his gists.