Skip to content

Instantly share code, notes, and snippets.

@bilzard
Created April 30, 2019 17:34
Show Gist options
  • Save bilzard/903d9ec82b0bc7f2da53bfdeab0a194b to your computer and use it in GitHub Desktop.
Save bilzard/903d9ec82b0bc7f2da53bfdeab0a194b to your computer and use it in GitHub Desktop.
jupytext, jupyter notebook, jupyter lab

jupytext で Jupyter notebook / Jupyter Lab のソースコードを Git 管理する

Git 管理ポリシー

  • notebook は git 管理せず、ペアリングした *.py のみを git 管理する
    • *.ipynb.gitignore に追加する
  • notebook はデフォルトで python のコードにペアリングする

notebook と ソースコードの二重管理になるのが嫌だったのでこのようにした。

Jupytext のインストールとペアリングの設定

jupytext を使う。最初にjupytextのインストールと設定を済ませておく。

~/.jupyter/jupyter_notebook_config.py

c.ContentsManager.default_jupytext_formats = "ipynb,py"
c.ContentsManager.preferred_jupytext_formats_save = "py:percent"

notebook の共有

リポジトリに notebook が含まれていないので、はじめてリポジトリをクローンしてきた場合は以下のコマンドを実行して notebook ファイルを復元する。

% jupytext --sync **.py
@bilzard
Copy link
Author

bilzard commented Apr 30, 2019

Github は *.ipynb のファイルを表示することができるので、 notebook を git 管理にするポリシーでも悪くないと思う。
実際そうしているプロジェクトも多い(注1)。
どのみち production のコードに乗せるときはリファクタリングが必要なので、 notebook の差分を厳密に管理したい、というケースは稀かもしれない。

注1)ipynb ファイルを git 管理に含めている例
https://github.com/amueller/introduction_to_ml_with_python/blob/master/01-introduction.ipynb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment