Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Jupyter Notebook Post-save Hook: Auto-convert a Python script and a HTML from the notebook
# Reference:
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

This comment has been minimized.

Copy link

@DanyshMushtaq DanyshMushtaq commented Jul 12, 2019

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?


This comment has been minimized.

Copy link
Owner Author

@ceshine 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