5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jupyter > Pythonコードをgitでバージョン管理をするには > jupyter nbconvert --to script [notebook]でpyファイルに変換する

Last updated at Posted at 2017-01-07
動作環境
GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4

Jupyter上でコードを変更している中、コードの変更をgit管理したくなった。

関連情報

関連情報を調べて見つけたのが以下。
http://stackoverflow.com/questions/18734739/using-ipython-notebooks-under-version-control

For Ipython 2 notebook servers, this can be accomplished by starting the server using:

ipython notebook --script

実行してみた (--script)

$ jupyter-notebook disp_checkpoint.ipynb --script
[W 07:21:15.940 NotebookApp] 
            `--script` is deprecated and will be removed in notebook 5.0.
    
            You can trigger nbconvert via pre- or post-save hooks:
    
                ContentsManager.pre_save_hook
                FileContentsManager.post_save_hook
    
            A post-save hook has been registered that calls:
    
                jupyter nbconvert --to script [notebook]
    
            which behaves similarly to `--script`.

v5.1.0では--scriptは古いということらしい。

とりあえず、この状態でnotebookを保存するとipynbファイルに加えて、pyファイルも保存されるようになった。

[I 07:21:56.843 NotebookApp] Saving file at /disp_checkpoint.ipynb
[I 07:21:57.255 NotebookApp] Saving script /disp_checkpoint.py

pyファイルは以下のようになっていて、コードのセルの前にIn[]:のコメントが入る。

# coding: utf-8

# In[1]:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

data_1d = np.random.rand(22500)
print(data_1d)
data_2d = np.reshape(data_1d, (150,150))

plt.imshow(data_2d, extent=(0,150,0,150),cmap=cm.gist_rainbow)ko-o-d
plt.show()


# In[17]:

'''
disp_checkpoint.ipynb
'''
...

post-save hook

$ jupyter-notebook disp_checkpoint.ipynb --script
[W 07:21:15.940 NotebookApp] 
            `--script` is deprecated and will be removed in notebook 5.0.

            You can trigger nbconvert via pre- or post-save hooks:

                ContentsManager.pre_save_hook
                FileContentsManager.post_save_hook

            A post-save hook has been registered that calls:

                jupyter nbconvert --to script [notebook]

            which behaves similarly to `--script`.

を読むと、jupyter nbconvert --to script [notebook]を実行すると、その時点でipynbファイルをpyファイルに変換できるようだ。

実行してみた。

$ jupyter nbconvert --to script disp_checkpoint.ipynb 
[NbConvertApp] Converting notebook disp_checkpoint.ipynb to script
[NbConvertApp] Writing 7041 bytes to disp_checkpoint.py

コードをgit管理したい時点で上記コマンドを実行する、という感じだろうか。

5
4
0

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?