Skip to content

Instantly share code, notes, and snippets.

@aaronmyatt
Forked from jbwhit/post-save-hook.py
Created October 6, 2016 14:24
Show Gist options
  • Save aaronmyatt/3504feb38b30b71e3054c552ca0a7ada to your computer and use it in GitHub Desktop.
Save aaronmyatt/3504feb38b30b71e3054c552ca0a7ada to your computer and use it in GitHub Desktop.
Saves Jupyter Notebooks as .py and .html files automatically. Add to the ipython_notebook_config.py file of your associated profile.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment