In [7]:
!git config --global core.attributesfile ~/.gitattributes
!git config --global filter.dropoutput_ipynb.clean ~/bin/ipynb_output_filter.py
!git config --global filter.dropoutput_ipynb.smudge cat

In [8]:
!echo '*.ipynb    filter=dropoutput_ipynb' > /root/.gitattributes

In [9]:
mkdir /root/bin

/root/bin/ipynb_output_filter.py

introduction

This code makes outputs of xxx.ipynb empty. Test.json is created when you command 'git add .'.

code

#! /usr/bin/python

import io
import sys
from IPython.nbformat.current import read, write

json_in = read(sys.stdin, 'json')

for sheet in json_in.worksheets:
    for cell in sheet.cells:
        if 'outputs' in cell:
            cell.outputs = []
        if 'prompt_number' in cell:
            cell.prompt_number = ''

with io.open('/tmp/notebook/test.json', 'w', encoding='utf-8') as f:
    write(json_in, f, 'json')

In [10]:
ipynb_output_filter_py = "#! /usr/bin/python\n\nimport io\nimport sys\nfrom IPython.nbformat.current import read, write\n\njson_in = read(sys.stdin, 'json')\n\nfor sheet in json_in.worksheets:\n    for cell in sheet.cells:\n        if 'outputs' in cell:\n            cell.outputs = []\n        if 'prompt_number' in cell:\n            cell.prompt_number = ''\n\nwith io.open('/tmp/notebook/test.json', 'w', encoding='utf-8') as f:\n    write(json_in, f, 'json')"

In [11]:
with open('/root/bin/ipynb_output_filter.py', 'w') as f:
    f.write(ipynb_output_filter_py)

In [13]:
!chmod +x /root/bin/ipynb_output_filter.py

In [14]:
pwd


Out[14]:
u'/tmp/notebook'

In [16]:
!git init


Initialized empty Git repository in /tmp/notebook/.git/

In [31]:
!git status


On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.ipynb_checkpoints/
	ipynb_output_filter_sample.ipynb

nothing added to commit but untracked files present (use "git add" to track)

In [32]:
ls -l


total 16
-rw-r--r-- 1 root root 15887 Dec  9 02:43 ipynb_output_filter_sample.ipynb

In [33]:
!git add .

In [34]:
ls -l


total 24
-rw-r--r-- 1 root root 14807 Dec  9 02:45 ipynb_output_filter_sample.ipynb
-rw-r--r-- 1 root root  4693 Dec  9 02:46 test.json

In [35]:
cat test.json


{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!git config --global core.attributesfile ~/.gitattributes\n",
      "!git config --global filter.dropoutput_ipynb.clean ~/bin/ipynb_output_filter.py\n",
      "!git config --global filter.dropoutput_ipynb.smudge cat"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!echo '*.ipynb    filter=dropoutput_ipynb' > /root/.gitattributes"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mkdir /root/bin"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# /root/bin/ipynb_output_filter.py\n",
      "\n",
      "## introduction\n",
      "This code makes outputs of xxx.ipynb empty.\n",
      "Test.json is created when you command 'git add .'.\n",
      "\n",
      "## code\n",
      "```python\n",
      "#! /usr/bin/python\n",
      "\n",
      "import io\n",
      "import sys\n",
      "from IPython.nbformat.current import read, write\n",
      "\n",
      "json_in = read(sys.stdin, 'json')\n",
      "\n",
      "for sheet in json_in.worksheets:\n",
      "    for cell in sheet.cells:\n",
      "        if 'outputs' in cell:\n",
      "            cell.outputs = []\n",
      "        if 'prompt_number' in cell:\n",
      "            cell.prompt_number = ''\n",
      "\n",
      "with io.open('/tmp/notebook/test.json', 'w', encoding='utf-8') as f:\n",
      "    write(json_in, f, 'json')\n",
      "```"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ipynb_output_filter_py = \"#! /usr/bin/python\\n\\nimport io\\nimport sys\\nfrom IPython.nbformat.current import read, write\\n\\njson_in = read(sys.stdin, 'json')\\n\\nfor sheet in json_in.worksheets:\\n    for cell in sheet.cells:\\n        if 'outputs' in cell:\\n            cell.outputs = []\\n        if 'prompt_number' in cell:\\n            cell.prompt_number = ''\\n\\nwith io.open('/tmp/notebook/test.json', 'w', encoding='utf-8') as f:\\n    write(json_in, f, 'json')\""
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "with open('/root/bin/ipynb_output_filter.py', 'w') as f:\n",
      "    f.write(ipynb_output_filter_py)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!chmod +x /root/bin/ipynb_output_filter.py"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pwd"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!git init"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!git status"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ls -l"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!git diff"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ls -l"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cat test.json"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": ""
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}

In [ ]: