Skip to content

Instantly share code, notes, and snippets.

@ceshine
Created August 23, 2017 12:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ceshine/2a68a96e7a9f72551d00c5578249340f to your computer and use it in GitHub Desktop.
Save ceshine/2a68a96e7a9f72551d00c5578249340f to your computer and use it in GitHub Desktop.
Jupyter Notebook Post-save Hook: Auto-convert a Python script and a HTML from the notebook
# Reference: https://svds.com/jupyter-notebook-best-practices-for-data-science/
import os
from subprocess import check_call
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(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d)
check_call(['jupyter', 'nbconvert', '--to', 'html', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
@DanyshMushtaq
Copy link

my content manager is S3ContentsManager. I basically source notebooks from S3 Folder. I tried to use this code in this form as well as replacing FileContentsManager in last line by S3ContentsManager., but it did not work for me. How will this code change for me?

@ceshine
Copy link
Author

ceshine commented Jul 14, 2019

Sorry. I'm not familiar with S3ContentsManager. Maybe you can check if it implements post_save_hook first. If it does, the next step is to find a way to sync local files generated by jupyter nbconvert.

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