Skip to content

Instantly share code, notes, and snippets.

@sebastibe
Created June 9, 2015 03:32
Show Gist options
  • Save sebastibe/db73c13d0cc9e62819d3 to your computer and use it in GitHub Desktop.
Save sebastibe/db73c13d0cc9e62819d3 to your computer and use it in GitHub Desktop.
Indications on how to save .py files in parallel to the ipynb file

In order to view/download the latest notebook via github or nbviewer or to see how the the notebook code has changed we activate saving the .py files automatically and track both .ipynb files and the .py files.

In order to automate it, add the following code in the ipython_notebook_config.py file:

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(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)

c.FileContentsManager.post_save_hook = post_save

If you aren't sure in which directory to find your ipython_notebook_config.py file, you can type ipython locate profile default, and if you don't find the file there, you can create it by typing ipython profile create.

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