Skip to content

Instantly share code, notes, and snippets.

@jbwhit
Last active September 21, 2023 04:50
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jbwhit/881bdeeaae3e4128947c to your computer and use it in GitHub Desktop.
Save jbwhit/881bdeeaae3e4128947c 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(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d)
check_call(['jupyter', 'nbconvert', '--to', 'html', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
@jbwhit
Copy link
Author

jbwhit commented Jan 20, 2020

So it looks like this stackoverflow answer has the right way to go about this: https://stackoverflow.com/questions/29329667/ipython-notebook-script-deprecated-how-to-replace-with-post-save-hook

jupyter notebook --generate-config

and it will output the file name:

Writing default config to: /Users/user/.jupyter/jupyter_notebook_config.py

And that's the location that you'd like to edit. I've also tweaked the call to use jupyter instead of ipython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment