Skip to content

Instantly share code, notes, and snippets.

@hwartig
Forked from pjbull/jupyter_notebook_config.py
Last active August 9, 2016 06:26
Show Gist options
  • Select an option

  • Save hwartig/c363f6d8bb836acc4f19884ed0e2c29a to your computer and use it in GitHub Desktop.

Select an option

Save hwartig/c363f6d8bb836acc4f19884ed0e2c29a to your computer and use it in GitHub Desktop.
Create .py on save of Jupyter notebook
# Copy this file to ~/.jupyter/jupyter_notebook_config.py
import os
import shutil
from nbconvert.nbconvertapp import NbConvertApp
SAVE_PROGRESS_INDICATOR_FILE_PY = '.ipynb_save_py'
def post_save(model, os_path, contents_manager):
""" post-save hook for converting notebooks to .py scripts """
# only do this for notebooks
if model['type'] != 'notebook':
return
# only do this if we've added the special indicator file to the working directory
cwd = os.path.dirname(os_path)
save_progress_indicator = os.path.join(cwd, SAVE_PROGRESS_INDICATOR_FILE_PY)
should_convert_py = os.path.exists(save_progress_indicator)
if should_convert_py:
d, fname = os.path.split(os_path)
converter = NbConvertApp()
converter.export_format = 'script'
converter.initialize()
converter.notebooks = [os_path]
converter.convert_notebooks()
c.FileContentsManager.post_save_hook = post_save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment