Automatically commit and push IPython notebook

I’m currently teaching Python at a Software Carpentry workshop at North West University in Potchefstroom. As always there are concerns about pace and about how people can catch up if they fall behind. In a recent discussion on this topic on the Software Carpentry mailing list, David Dotson mentioned that he commits his IPython notebooks by pressing a custom keyboard shortcut which triggers an automatic git add/commit/push. No code was available, but I poked around a bit and found this StackOverflow question and answer which showed how to add a post-save hook to an IPython notebook (with details on doing the same for the newer Project Jupyter notebooks).

So here’s my code:

import os
from subprocess import check_call
from shlex import split

def post_save(model, os_path, contents_manager):
    """post-save hook for doing a git commit / push"""
    if model['type'] != 'notebook':
        return # only do this for notebooks
    workdir, filename = os.path.split(os_path)
    if filename.startswith('Scratch') or filename.startswith('Untitled'):
        return # skip scratch and untitled notebooks
    # now do git add / git commit / git push
    check_call(split('git add {}'.format(filename)), cwd=workdir)
    check_call(split('git commit -m "notebook save" {}'.format(filename)), cwd=workdir)
    check_call(split('git push'), cwd=workdir)

c.FileContentsManager.post_save_hook = post_save

This code obviously assumes that your working directory is a git repository and it has been configured with a remote to push to. For this workshop my notebooks are in this git repo on GitHub.

I created a new IPython profile (ipython profile create swcteaching) for use while teaching and added that code to the ipython_notebook_config.py file. You can find this file’s location with ipython profile locate swcteaching.

The one little niggle is that the commit message is always the same. I don’t know IPython’s front-end code well enough, but perhaps there is a way to pop up a window and request a commit message (going towards something more like David Dotson’s solution and less like mine).