|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Suppress output and prompt numbers in git version control. |
| 5 | + |
| 6 | +This script will tell git to ignore prompt numbers and cell output |
| 7 | +when looking at ipynb files UNLESS their metadata contains: |
| 8 | + |
| 9 | + "git" : { "keep_output" : true } |
| 10 | + |
| 11 | +The notebooks themselves are not changed. |
| 12 | + |
| 13 | +See also this blogpost: http://pascalbugnion.net/blog/ipython-notebooks-and-git.html. |
| 14 | + |
| 15 | +Usage instructions |
| 16 | +================== |
| 17 | + |
| 18 | +1. Put this script in a directory that is on the system's path. |
| 19 | + For future reference, I will assume you saved it in |
| 20 | + `~/scripts/ipynb_drop_output`. |
| 21 | +2. Make sure it is executable by typing the command |
| 22 | + `chmod +x ~/scripts/ipynb_drop_output`. |
| 23 | +3. Register a filter for ipython notebooks by |
| 24 | + putting the following line in `~/.config/git/attributes`: |
| 25 | + `*.ipynb filter=clean_ipynb` |
| 26 | +4. Connect this script to the filter by running the following |
| 27 | + git commands: |
| 28 | + |
| 29 | + git config --global filter.clean_ipynb.clean ipynb_drop_output |
| 30 | + git config --global filter.clean_ipynb.smudge cat |
| 31 | + |
| 32 | +To tell git NOT to ignore the output and prompts for a notebook, |
| 33 | +open the notebook's metadata (Edit > Edit Notebook Metadata). A |
| 34 | +panel should open containing the lines: |
| 35 | + |
| 36 | + { |
| 37 | + "name" : "", |
| 38 | + "signature" : "some very long hash" |
| 39 | + } |
| 40 | + |
| 41 | +Add an extra line so that the metadata now looks like: |
| 42 | + |
| 43 | + { |
| 44 | + "name" : "", |
| 45 | + "signature" : "don't change the hash, but add a comma at the end of the line", |
| 46 | + "git" : { "keep_outputs" : true } |
| 47 | + } |
| 48 | + |
| 49 | +You may need to "touch" the notebooks for git to actually register a change, if |
| 50 | +your notebooks are already under version control. |
| 51 | + |
| 52 | +Notes |
| 53 | +===== |
| 54 | + |
| 55 | +Changed by David Bau to make stripping output the default. |
| 56 | + |
| 57 | +This script is inspired by http://stackoverflow.com/a/20844506/827862, but |
| 58 | +lets the user specify whether the ouptut of a notebook should be kept |
| 59 | +in the notebook's metadata, and works for IPython v3.0. |
| 60 | +""" |
| 61 | + |
| 62 | +import sys |
| 63 | +import json |
| 64 | + |
| 65 | +nb = sys.stdin.read() |
| 66 | + |
| 67 | +json_in = json.loads(nb) |
| 68 | +nb_metadata = json_in["metadata"] |
| 69 | +keep_output = False |
| 70 | +if "git" in nb_metadata: |
| 71 | + if "keep_outputs" in nb_metadata["git"] and nb_metadata["git"]["keep_outputs"]: |
| 72 | + keep_output = True |
| 73 | +if keep_output: |
| 74 | + sys.stdout.write(nb) |
| 75 | + exit() |
| 76 | + |
| 77 | + |
| 78 | +ipy_version = int(json_in["nbformat"])-1 # nbformat is 1 more than actual version. |
| 79 | + |
| 80 | +def strip_output_from_cell(cell): |
| 81 | + if "outputs" in cell: |
| 82 | + cell["outputs"] = [] |
| 83 | + if "prompt_number" in cell: |
| 84 | + del cell["prompt_number"] |
| 85 | + if "execution_count" in cell: |
| 86 | + cell["execution_count"] = None |
| 87 | + |
| 88 | + |
| 89 | +if ipy_version == 2: |
| 90 | + for sheet in json_in["worksheets"]: |
| 91 | + for cell in sheet["cells"]: |
| 92 | + strip_output_from_cell(cell) |
| 93 | +else: |
| 94 | + for cell in json_in["cells"]: |
| 95 | + strip_output_from_cell(cell) |
| 96 | + |
| 97 | +json.dump(json_in, sys.stdout, sort_keys=True, indent=1, separators=(",",": ")) |
0 commit comments