Table of Contents
1 初始化設定
1.1 下載 script ( 到 ${PATH} 可以搜尋到的位置 )
假設 ~/bin 在 PATH 變數中
#這一個版本的 script 針對 ipynb format v4 #不會截掉 prompt number 只截掉 output curl -o ipynb_drop_output https://gist.githubusercontent.com/pbugnion/ea2797393033b54674af/raw/86d242c77d400bce1b85201fce94106eb7f04fe4/ipynb_drop_output.py chmod +x ipynb_drop_output mv ipynb_drop_output ~/bin
針對 ipynb v4 版本 將檔案的 80, 81 行改成
if "execution_count" in cell: cell["execution_count"] = None
1.2 設定 git filter
touch ~/.config/git/attributes
將下面這一行
*.ipynb filter=clean_ipynb
加入 ~/.config/git/attributes
設定 ~/.gitconfig
git config --global filter.clean_ipynb.clean ipynb_drop_output git config --global filter.clean_ipynb.smudge cat
2 設定 ipynb 檔案
針對不想要 output 的納入版本控制的 ipynb 檔案
編輯 notebook Metadata
會看到
{ "name" : "", "signature" : "some very long hash" }
新增一行 將其改為
{ "name" : "", "signature" : "don't change the hash, but add a comma at the end of the line", "git" : { "suppress_outputs" : true } }
3 Ref
3.1 ipynb script version v4
https://gist.github.com/pbugnion/ea2797393033b54674af
Script for the ipynb format, version 4
#!/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: "git" : { "suppress_output" : true } The notebooks themselves are not changed. See also this blogpost: http://pascalbugnion.net/blog/ipython-notebooks-and-git.html. Usage instructions ================== 1. Put this script in a directory that is on the system's path. 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 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 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, 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", "git" : { "suppress_outputs" : true } } 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 import json nb = sys.stdin.read() json_in = json.loads(nb) nb_metadata = json_in["metadata"] suppress_output = False 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) 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 "execution_count" in cell: cell["execution_count"] = None 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=(",",": "))
4 其他方式 (ipymd, …)
4.1 ipymd
4.1.1 安裝與初始化設定 (jupyternotebookconfig.py)
pip install ipymd
設定 jupyternotebookconfig.py
c.NotebookApp.contents_manager_class = 'ipymd.IPymdContentsManager'
將上面這一行加入 jupyternotebookconfig.py
如果不曉得 jupyternotebookconfig.py 在哪兒 請輸入
jupyter --config-dir # find out the path to the config file
如果沒有 jupyternotebookconfig.py
jupyter notebook --generate-config # generate a default config file
4.1.2 注意事項
Renaming doesn't work yet (issue #4) New notebook doesn't work yet (issue #5) Only nbformat v4 is supported currently (IPython 3.0)