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
This code makes outputs of xxx.ipynb empty. Test.json is created when you command 'git add .'.
#! /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]:
In [16]:
!git init
In [31]:
!git status
In [32]:
ls -l
In [33]:
!git add .
In [34]:
ls -l
In [35]:
cat test.json
In [ ]: