Skip to content

Instantly share code, notes, and snippets.

@masnick
Created July 17, 2015 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masnick/d89c03ba3f0ad48416a5 to your computer and use it in GitHub Desktop.
Save masnick/d89c03ba3f0ad48416a5 to your computer and use it in GitHub Desktop.
IPython post hook for saving `.py` and `.html` files from notebooks
# http://stackoverflow.com/questions/29329667/ipython-notebook-script-deprecated-how-to-replace-with-post-save-hook
import os
from subprocess import check_call
c = get_config()
def post_save(model, os_path, contents_manager):
"""post-save hook for converting notebooks to .py scripts"""
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
@Navjotbians
Copy link

Navjotbians commented Feb 20, 2021

Hi, I am trying this method to automatically save the .py version of the notebook but getting the below-outlined error

Traceback (most recent call last):
File "jupyter_notebook_config.py", line 11, in
c = get_config()
NameError: name 'get_config' is not defined

My Jupyter version is 4.4 and using a lengthy version of code that can be seen here
https://tech-notes.maxmasnick.com/ipython-notebooks-automatically-export-py-and-html

your help is much appreciated.
image

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