LoginSignup
55
57

More than 5 years have passed since last update.

Jupyter notebook で HTML も自動作成

Last updated at Posted at 2016-05-04

Jupyter notebook は快適だけれども、昔のノートブックを見るためにサーバーをわざわざ起動させるのが気分的に面倒ということがある。同僚と情報を共有するのに HTML のほうが良いということもあるかもしれない。そういう時のために、自動的に HTML ファイルと py ファイルを保存するようにしておくと便利。

まず、(なければ)設定ファイルを作る。

% jupyter notebook --generate-config

作られた ~/.jupyter/jupyter_notebook_config.py を開いて

#-----------------------------
# Configurable configuration
#-----------------------------

の下にでも

import os
from subprocess import check_call

def post_save(model, os_path, contents_manager):
    """post-save hook for converting notebooks to .py scripts"""
    if model['type'] != 'notebook':
        return # only do this for notebooks
    d, fname = os.path.split(os_path)
    check_call(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d)
    check_call(['jupyter', 'nbconvert', '--to', 'html', fname], cwd=d)

c.FileContentsManager.post_save_hook = post_save

これで、ipynb が作られると同時に同名の html と py (あるいは r など)が作られる。

参考

Jupyter Notebook for Data Science Teams (O'Reilly)

ipython nbconvert は古いという警告がでるので、参考元のコードの ipython を jupyter に変更しました。

55
57
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
55
57