Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Flatten notebooks for git diff

Copy to somewhere on $PATH. Then, in the root of a git repository, run these commands:

echo "*.ipynb diff=ipynb" >> .gitattributes 
git config diff.ipynb.textconv

When you change a notebook and run git diff, you'll see the diff of flattened, simplified notebooks, rather than the full JSON. This does lose some information (metadata, non-text output), but it makes it easier to see simple changes in the notebook.

This doesn't help with merging conflicting changes in notebooks. For that, see

import sys
from IPython.nbformat.current import read
from IPython.utils.text import strip_ansi
fname = sys.argv[1]
with open(fname, encoding='utf-8') as f:
nb = read(f, 'ipynb')
banners = {
'heading': 'Heading %d ------------------',
'markdown': 'Markdown cell ---------------',
'code': 'Code cell -------------------',
'raw': 'Raw cell --------------------',
'output': 'Output ----------------------',
for cell in nb.worksheets[0].cells:
if cell.cell_type == 'heading':
print(banners['heading'] % cell.level)
if cell.cell_type == 'code':
source = cell.input
source = cell.source
if not source.endswith('\n'):
if cell.cell_type == 'code':
if cell.outputs:
for output in cell.outputs:
if 'text' in output:
elif 'traceback' in output:
print("(Non-plaintext output)")

I get an error.
Here's the end of git config -l to show I have the right line in there:


Here's the content of .gitattributes:

maye@lunatic|~/Dropbox/src/diviner on develop!
± cat .gitattributes
*.ipynb diff=ipynb
maye@lunatic|~/Dropbox/src/diviner on develop!

Here's my try to use it:

± git diff notebooks/analyses/Physics.ipynb
error: cannot run No such file or directory
fatal: unable to read files to diff
-> [128]
maye@lunatic|~/Dropbox/src/diviner on develop!

Here's the content of my $HOME/bin which is on the PATH:

± ll ~/bin
total 40
-rwxr--r--  1 maye  staff   1.2K Sep  8 15:24
-rwx------  1 maye  staff   116B Oct  5  2012 printpath
-rwxr-xr-x  1 maye  staff   1.2K Sep 28  2012 ssh-copy-id
lrwxr-xr-x  1 maye  staff    62B Apr 21 18:19 subl -> /Applications/Sublime
lrwxr-xr-x  1 maye  staff    37B Nov 20  2012 vcprompt -> /Users/maye/src/vcprompt/bin/vcprompt

Very mysterious: Adding the full path results in git lying to me:

maye@lunatic|~/Dropbox/src/diviner on develop!
± git diff notebooks/analyses/Physics.ipynb
error: cannot run /Users/maye/bin/ No such file or directory
fatal: unable to read files to diff
-> [128]
maye@lunatic|~/Dropbox/src/diviner on develop!
± ll /Users/maye/bin/
-rwxr--r--  1 maye  staff   1.2K Sep  8 15:24 /Users/maye/bin/
maye@lunatic|~/Dropbox/src/diviner on develop!

Make it executable? Oh, sorry, it is. Do you have a /usr/bin/python3? I believe you'll see "No such file or directory" if the kernel can't find the executable named in the shebang line. Somethimes this happens with DOS-style files when the carriage return is taken as part of the filename.


This is awesome. I backported it for Python 2.7 (that sounds much grander than changing 2 lines) and it's already saving me a number of headaches.

Getting the following error using IPython 2.2.0 on Ubuntu 14.04:

ethan@oryx:~/ProgBio/repo (gh-pages *)$ git diff ipynbs/functions-writing.ipynb
Traceback (most recent call last):
  File "/usr/local/bin/", line 4, in <module>
    from IPython.utils.text import strip_ansi
ImportError: cannot import name 'strip_ansi'
fatal: unable to read files to diff

Looks like in the current release (2.2.0) line 4 should still be from IPython.nbconvert.filters.ansi import strip_ansi. Change is in ipython/ipython@d2acc30

Oh, and now that this works, it is awesome!


takluyver commented Oct 30, 2014

Oh, I wasn't getting pinged by comments on here for some reason. I'm glad it's helping people - if anyone is still having trouble with it, let me know.

jfeist commented Mar 19, 2015

Just in case it might be useful to someone:

I have been a big fan of since I discovered it, and have been using it extensively as a diff filter for git. However, I find it to be a bit slow, especially for repositories with many (large) notebooks. So I spent a bit of time writing a filter for jq which does the same thing, but is orders of magnitude faster.

The relevant section of my .gitconfig now looks like this:

[diff "ipynb"]
    textconv = "jq -r 'def banner: \"\\(.) \"+(28-(.|length))*\"-\"; (\"Non-cell info\"|banner),del(.cells),\"\", (.cells[] | (\"\\(.cell_type) cell\"|banner), \"\\(.source|add)\\n\")'"

I am typically not interested in the outputs for diffing notebooks, so the textconv filter here does not show them. However, I did find it to be more convenient for me to show the metadata of the notebook as well in the output, and everything not in "cells" is shown first under the header "Non-cell info". This disappears by removing the part (\"Non-cell info\"|banner),del(.cells),\"\",.

I have also written a more "complete" script which shows the outputs in pretty much the same way as That can be found at If you download it and put it somewhere in your path, you can use textconv = nbflatten.jq instead.

jfeist commented Mar 19, 2015

PS: jq is also very useful (and fast!) for making a filter to remove the output of notebooks when adding them to git. The relevant part of .gitconfig is

[filter "clean_nb"]
    clean = "jq '(.cells[] | select(.outputs) ) |= [] | (.cells[] | select(.execution_count)) |= null'"

And in .gitattributes, you then need *.ipynb filter=clean_nb diff=ipynb.

@jfeist, using jq is fantastic. This really saved me a lot of time!

Modification to @jfeist's snippet for .gitconfig. More details here ( stedolan/jq#921 ). Also, double quotes must be escaped in single quotes with .gitconfig. ( )

[filter "clean_nb"]
        clean = jq '(.cells[] | select(has(\"outputs\")) | .outputs) = [] | (.cells[] | select(has(\"execution_count\")) | .execution_count) = null'

edgimar commented Sep 21, 2015

In case anyone cares, newer versions of ipython have a "nbconvert" function built into them, so you can do something like ipython nbconvert myfile.ipynb --to markdown --stdout and get a similar effect to this script. Otherwise you will need to mess around with the nbflatten script in order to get it to work with recent versions of ipython.

Has someone here use jq as a nbflatten replacement (with [diff "ipynb"], not [filter ...]!) on windows? I tried and jq crashes even on jq "." whatever.ipnb

Re: jq, @janschulz filed stedolan/jq#1072, an it's a fun one.

JFYI: with a recent build of jq, the jq version of nbflatten and the filter now works on windows.

vmuriart commented Apr 6, 2016

For anyone looking to download the version @janschulz was referring to its on AppVeyor

Thanks @janschulz for the heads up

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