Skip to content

Instantly share code, notes, and snippets.

@ceshine
Created August 23, 2017 12:31
Show Gist options
  • 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
@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