LoginSignup
125
111

More than 5 years have passed since last update.

jupyter (ipython notebook) のipynbファイルをバージョン管理しやすくする

Last updated at Posted at 2016-02-28

動機:ipynbファイルは差分が絶望的に見にくい

コードだけの差分が見たいのに、
以下で無数に差分がでるのが辛い。。。

  1. 出力結果 out[1]: hogehoge
  2. 実行順番 (In[17]などの 17の部分)
  3. セルの区切り具合

方法1(人力:非推奨): 毎回出力結果をクリアしてからコミットする

 kernel再起動の時にclear output を選択する。
 
何もしないよりは見やすくなるが以下の欠点がる。

  • 上記の3に対しては無意味
  • めんどい
  • 忘れてそのままコミットした時に死ぬ

方法2(こっちが本題): 自動でpyファイルを生成し、それをバージョン管理する

  • ipynbをセーブした時に自動でpyファイルも更新される!
  • 基本的に不要な、出力結果や実行順番を消すこともできる!

やり方はここを参考にした。

http://jupyter-notebook.readthedocs.org/en/latest/extending/savehooks.html

各自の環境にある以下の設定ファイルをいじる。

.jupyter\jupyter_notebook_config.py

上記ファイルは最初は無いかもしれない。
その時は生成するコマンドがあるので、ググッてください。


方法2のための変更箇所1:保存前に、無駄な出力などを削除する

このコメントアウトされた部分を。。。

jupyter_notebook_config.py
#c.FileContentsManager.post_save_hook = None

↓ こうじゃ

jupyter_notebook_config.py

def scrub_output_pre_save(model, **kwargs):
    """scrub output before saving notebooks"""
    # only run on notebooks
    if model['type'] != 'notebook':
        return
    # only run on nbformat v4
    if model['content']['nbformat'] != 4:
        return

    for cell in model['content']['cells']:
        if cell['cell_type'] != 'code':
            continue
        cell['outputs'] = []
        cell['execution_count'] = None

c.FileContentsManager.pre_save_hook = scrub_output_pre_save


方法2のための変更箇所2:同名のpyファイルをipynbと同じタイミングで自動保存する

こいつを

jupyter_notebook_config.py
#c.FileContentsManager.post_save_hook = None

↓こうじゃ

jupyter_notebook_config.py
import io
import os
from notebook.utils import to_api_path

_script_exporter = None

def script_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert

    replaces `ipython notebook --script`
    """
    from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        return

    global _script_exporter
    if _script_exporter is None:
        _script_exporter = ScriptExporter(parent=contents_manager)
    log = contents_manager.log

    base, ext = os.path.splitext(os_path)
    py_fname = base + '.py'
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
    with io.open(script_fname, 'w', encoding='utf-8') as f:
        f.write(script)
c.FileContentsManager.post_save_hook = script_post_save

 


以上でまともなバージョン管理ができるかと。
jupyter最高!

125
111
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
125
111