├── .gitattributes ├── .gitignore ├── README.md ├── scripts └── nbstripout └── simple_nb.ipynb /.gitattributes: -------------------------------------------------------------------------------- 1 | *.ipynb filter=stripoutput 2 | 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Using nbstripout 2 | 3 | nbstripout is a filter that removes numbering and output from jupyter notebooks. 4 | 5 | The script `nbstripout` was found 6 | [here](https://github.com/cfriedline/ipynb_template/blob/master/nbstripout) with inspiration 7 | from [here](https://gist.github.com/minrk/6176788) (see the discussion there). 8 | 9 | It is available on PyPI, so it is rather straightforward to use. 10 | 11 | ## Short 12 | 13 | Install nbstripout: 14 | 15 | python3 -m pip install --user nbstripout 16 | 17 | Add the following in the file `.git/config`: 18 | 19 | [filter "stripoutput"] 20 | clean = "nbstripout" 21 | smudge = cat 22 | required 23 | 24 | and also write a `.gitattributes` file: 25 | 26 | *.ipynb filter=stripoutput 27 | 28 | ## Long 29 | 30 | Whenever a [Jupyter](http://jupyter.org/) notebook (detected by the extension `.ipynb`) is 31 | submitted to `git diff` or `git commit`, it is converted by the filter. The filter removes 32 | all numbering (that might change upon execution) and the output (that might contain binary 33 | data for figures, etc). 34 | -------------------------------------------------------------------------------- /scripts/nbstripout: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """strip outputs from an IPython Notebook 3 | Opens a notebook, strips its output, and writes the outputless version to the original file. 4 | Useful mainly as a git pre-commit hook for users who don't want to track output in VCS. 5 | This does mostly the same thing as the `Clear All Output` command in the notebook UI. 6 | Adapted from rom https://gist.github.com/minrk/6176788 to work with 7 | git filter driver 8 | """ 9 | import sys 10 | import codecs 11 | 12 | import nbformat 13 | 14 | def strip_output(nb): 15 | """strip the outputs from a notebook object""" 16 | if 'worksheets' in nb: 17 | for ws in nb.worksheets: 18 | for cell in ws.cells: 19 | if 'outputs' in cell: 20 | cell['outputs'] = [] 21 | if 'prompt_number' in cell: 22 | del cell['prompt_number'] 23 | else: 24 | for cell in nb.cells: 25 | if 'outputs' in cell: 26 | cell['outputs'] = [] 27 | if 'execution_count' in cell: 28 | cell['execution_count'] = None 29 | return nb 30 | 31 | if __name__ == '__main__': 32 | nb = nbformat.reads(sys.stdin.read(), nbformat.NO_CONVERT) 33 | nb = strip_output(nb) 34 | sys.stdout = codecs.getwriter('utf-8')(sys.stdout); 35 | nbformat.write(nb, sys.stdout, nbformat.NO_CONVERT) 36 | 37 | -------------------------------------------------------------------------------- /simple_nb.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "%matplotlib inline\n", 12 | "import matplotlib.pyplot as plt\n", 13 | "import numpy as np" 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": null, 19 | "metadata": { 20 | "collapsed": false 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "plt.plot(np.random.random(7));" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "## A Markdown cell\n", 32 | "\n", 33 | "Text goes in Markdown Cells.\n", 34 | "\n", 35 | "The script `nbstripout` was found [here](https://github.com/cfriedline/ipynb_template/blob/master/nbstripout) with inspiration from [here](https://gist.github.com/minrk/6176788)." 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": null, 41 | "metadata": { 42 | "collapsed": true 43 | }, 44 | "outputs": [], 45 | "source": [] 46 | } 47 | ], 48 | "metadata": { 49 | "kernelspec": { 50 | "display_name": "Python 2", 51 | "language": "python", 52 | "name": "python2" 53 | }, 54 | "language_info": { 55 | "codemirror_mode": { 56 | "name": "ipython", 57 | "version": 2 58 | }, 59 | "file_extension": ".py", 60 | "mimetype": "text/x-python", 61 | "name": "python", 62 | "nbconvert_exporter": "python", 63 | "pygments_lexer": "ipython2", 64 | "version": "2.7.9" 65 | } 66 | }, 67 | "nbformat": 4, 68 | "nbformat_minor": 0 69 | } 70 | --------------------------------------------------------------------------------