Skip to content

Instantly share code, notes, and snippets.

@pbugnion
Last active October 22, 2023 12:25
Show Gist options
  • Save pbugnion/ea2797393033b54674af to your computer and use it in GitHub Desktop.
Save pbugnion/ea2797393033b54674af to your computer and use it in GitHub Desktop.

Revisions

  1. Pascal Bugnion renamed this gist Dec 26, 2014. 1 changed file with 0 additions and 0 deletions.
  2. Pascal Bugnion revised this gist Dec 26, 2014. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion ipynb_drop_output.py
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    The notebooks themselves are not changed.
    See also {{ blogpost }}.
    See also this blogpost: http://pascalbugnion.net/blog/ipython-notebooks-and-git.html.
    Usage instructions
    ==================
    2 changes: 1 addition & 1 deletion ipython_notebook_in_git.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,6 @@ This gist lets you keep IPython notebooks in git repositories. It tells git to i

    To use the script, follow the instructions given in the script's docstring.

    For further details, read {{ blogpost }}.
    For further details, read [this blogpost](http://pascalbugnion.net/blog/ipython-notebooks-and-git.html).

    The procedure outlined here is inspired by [this answer](http://stackoverflow.com/a/20844506/827862) on Stack Overflow.
  3. Pascal Bugnion revised this gist Dec 25, 2014. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ipynb_drop_output.py
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@
    4. Connect this script to the filter by running the following
    git commands:
    git config --global filter.clean_ipynb.clean ~/scripts/ipynb_drop_output
    git config --global filter.clean_ipynb.clean ipynb_drop_output
    git config --global filter.clean_ipynb.smudge cat
    To tell git to ignore the output and prompts for a notebook,
  4. Pascal Bugnion renamed this gist Dec 24, 2014. 1 changed file with 0 additions and 0 deletions.
  5. Pascal Bugnion revised this gist Dec 24, 2014. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
  6. Pascal Bugnion renamed this gist Dec 24, 2014. 1 changed file with 0 additions and 0 deletions.
  7. Pascal Bugnion renamed this gist Dec 24, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. Pascal Bugnion renamed this gist Dec 24, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. Pascal Bugnion revised this gist Dec 24, 2014. 2 changed files with 10 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.txt → README.md
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,6 @@ This gist lets you keep IPython notebooks in git repositories. It tells git to i

    To use the script, follow the instructions given in the script's docstring.

    For further details, read {{ blogpost }}.
    For further details, read {{ blogpost }}.

    The procedure outlined here is inspired by [this answer](http://stackoverflow.com/a/20844506/827862) on Stack Overflow.
    7 changes: 7 additions & 0 deletions ipynb_output_filter.py
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,13 @@
    You may need to "touch" the notebooks for git to actually register a change, if
    your notebooks are already under version control.
    Notes
    =====
    This script is inspired by http://stackoverflow.com/a/20844506/827862, but
    lets the user specify whether the ouptut of a notebook should be suppressed
    in the notebook's metadata, and works for IPython v3.0.
    """

    import sys
  10. Pascal Bugnion revised this gist Dec 24, 2014. 2 changed files with 24 additions and 16 deletions.
    6 changes: 6 additions & 0 deletions README.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@

    This gist lets you keep IPython notebooks in git repositories. It tells git to ignore prompt numbers and program outputs when checking that a file has changed.

    To use the script, follow the instructions given in the script's docstring.

    For further details, read {{ blogpost }}.
    34 changes: 18 additions & 16 deletions ipynb_output_filter.py
    Original file line number Diff line number Diff line change
    @@ -6,29 +6,31 @@
    This script will tell git to ignore prompt numbers and cell output
    when looking at ipynb files if their metadata contains:
    "vc" : { "suppress_output" : true }
    "git" : { "suppress_output" : true }
    The notebooks themselves are not changed.
    See also {{ blogpost }}.
    Usage instructions
    ==================
    1. Put this script in a directory that is on the system's path.
    2. Make sure it is executable by typing
    `cd /location/of/script && chmod +x ipynb_output_filter.py`
    in a terminal.
    For future reference, I will assume you saved it in
    `~/scripts/ipynb_drop_output`.
    2. Make sure it is executable by typing the command
    `chmod +x ~/scripts/ipynb_drop_output`.
    3. Register a filter for ipython notebooks by
    creating a `.gitattributes` file in your home directory
    containing the line:
    `*.ipynb filter=dropoutput_ipynb`
    putting the following line in `~/.config/git/attributes`:
    `*.ipynb filter=clean_ipynb`
    4. Connect this script to the filter by running the following
    git commands:
    git config --global core.attributesfile ~/.gitattributes
    git config --global filter.dropoutput_ipynb.clean /path/to/script/ipynb_output_filter.py
    git config --global filter.dropoutput_ipynb.smudge cat
    git config --global filter.clean_ipynb.clean ~/scripts/ipynb_drop_output
    git config --global filter.clean_ipynb.smudge cat
    To enable this, open the notebook's metadata (Edit > Edit Notebook Metadata). A
    To tell git to ignore the output and prompts for a notebook,
    open the notebook's metadata (Edit > Edit Notebook Metadata). A
    panel should open containing the lines:
    {
    @@ -41,11 +43,11 @@
    {
    "name" : "",
    "signature" : "don't change the hash, but add a comma at the end of the line",
    "vc" : { "suppress_outputs" : true }
    "git" : { "suppress_outputs" : true }
    }
    You may need to "touch" the notebooks for git to actually register a change.
    You may need to "touch" the notebooks for git to actually register a change, if
    your notebooks are already under version control.
    """

    import sys
    @@ -56,8 +58,8 @@
    json_in = json.loads(nb)
    nb_metadata = json_in["metadata"]
    suppress_output = False
    if "vc" in nb_metadata:
    if "suppress_outputs" in nb_metadata["vc"] and nb_metadata["vc"]["suppress_outputs"]:
    if "git" in nb_metadata:
    if "suppress_outputs" in nb_metadata["git"] and nb_metadata["git"]["suppress_outputs"]:
    suppress_output = True
    if not suppress_output:
    sys.stdout.write(nb)
  11. Pascal Bugnion created this gist Dec 23, 2014.
    84 changes: 84 additions & 0 deletions ipynb_output_filter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    #!/usr/bin/env python

    """
    Suppress output and prompt numbers in git version control.
    This script will tell git to ignore prompt numbers and cell output
    when looking at ipynb files if their metadata contains:
    "vc" : { "suppress_output" : true }
    The notebooks themselves are not changed.
    Usage instructions
    ==================
    1. Put this script in a directory that is on the system's path.
    2. Make sure it is executable by typing
    `cd /location/of/script && chmod +x ipynb_output_filter.py`
    in a terminal.
    3. Register a filter for ipython notebooks by
    creating a `.gitattributes` file in your home directory
    containing the line:
    `*.ipynb filter=dropoutput_ipynb`
    4. Connect this script to the filter by running the following
    git commands:
    git config --global core.attributesfile ~/.gitattributes
    git config --global filter.dropoutput_ipynb.clean /path/to/script/ipynb_output_filter.py
    git config --global filter.dropoutput_ipynb.smudge cat
    To enable this, open the notebook's metadata (Edit > Edit Notebook Metadata). A
    panel should open containing the lines:
    {
    "name" : "",
    "signature" : "some very long hash"
    }
    Add an extra line so that the metadata now looks like:
    {
    "name" : "",
    "signature" : "don't change the hash, but add a comma at the end of the line",
    "vc" : { "suppress_outputs" : true }
    }
    You may need to "touch" the notebooks for git to actually register a change.
    """

    import sys
    import json

    nb = sys.stdin.read()

    json_in = json.loads(nb)
    nb_metadata = json_in["metadata"]
    suppress_output = False
    if "vc" in nb_metadata:
    if "suppress_outputs" in nb_metadata["vc"] and nb_metadata["vc"]["suppress_outputs"]:
    suppress_output = True
    if not suppress_output:
    sys.stdout.write(nb)
    exit()


    ipy_version = int(json_in["nbformat"])-1 # nbformat is 1 more than actual version.

    def strip_output_from_cell(cell):
    if "outputs" in cell:
    cell["outputs"] = []
    if "prompt_number" in cell:
    del cell["prompt_number"]


    if ipy_version == 2:
    for sheet in json_in["worksheets"]:
    for cell in sheet["cells"]:
    strip_output_from_cell(cell)
    else:
    for cell in json_in["cells"]:
    strip_output_from_cell(cell)

    json.dump(json_in, sys.stdout, sort_keys=True, indent=1, separators=(",",": "))