よしたく blog

ITエンジニアとして自分が知らなかったことをまとめています

.ipynbファイルをコマンドから実行する

Jupyter Notebookで作成したプログラムをインタラクティブに実行できるのはとても便利だ。しかし、ルーティンのように実行するプログラムを毎回ブラウザを起動させて実行するのが煩わしいタイミングもある。そこでJupyter Notebookで作成した.ipynbファイルをコマンドラインで実行できないか調べたら色々できたので備忘録として残す。

基本的にはnbconvertを使う。これはJupyterコマンドのあとに続けて書く。ex) jupyter nbconvert hogehoge

  1. jupyter nbconvert HelloWorld.ipynb --to python

このコマンドはHelloWorld.ipynb ファイルをPythonのファイルに変換し、Pythonコマンドから実行する。実行すると下記のように出力される。記事の本質からはズレてしまうが、やはり.pyファイルに変換して実行できる安心感はあるので載せておく。

yoshitakuMBA:jupyternotebook yoshitaku$ jupyter nbconvert HelloWorld.ipynb --to python
[NbConvertApp] Converting notebook HelloWorld.ipynb to python
[NbConvertApp] Writing 86 bytes to HelloWorld.py

Pythonファイルの中身は下記のように出力される。

#!/usr/bin/env python
# coding: utf-8

# In[1]:


print("Hello World")
  1. jupyter nbconvert --to notebook --execute HelloWorld.ipynb --output HelloWorld2.ipynb --debug

このコマンドはHelloWorld.ipynbを元にHelloworld2.ipynbを作成し、Helloworld2.ipynbを実行する。実行するタイミングでコマンドラインに実行結果を見るために--debugを付ける。

yoshitakuMBA:jupyternotebook yoshitaku$ jupyter nbconvert --to notebook --execute HelloWorld.ipynb --output HelloWorld2.ipynb --debug
[NbConvertApp] Searching ['/Users/yoshitaku/workspace/jupyternotebook', '/Users/yoshitaku/.jupyter', '/Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/etc/jupyter', '/usr/local/etc/jupyter', '/etc/jupyter'] for config files
[NbConvertApp] Looking for jupyter_config in /etc/jupyter
[NbConvertApp] Looking for jupyter_config in /usr/local/etc/jupyter
[NbConvertApp] Looking for jupyter_config in /Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/etc/jupyter
[NbConvertApp] Looking for jupyter_config in /Users/yoshitaku/.jupyter
[NbConvertApp] Looking for jupyter_config in /Users/yoshitaku/workspace/jupyternotebook
[NbConvertApp] Looking for jupyter_nbconvert_config in /etc/jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /usr/local/etc/jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/etc/jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /Users/yoshitaku/.jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /Users/yoshitaku/workspace/jupyternotebook
[NbConvertApp] Converting notebook HelloWorld.ipynb to notebook
[NbConvertApp] Notebook name is 'HelloWorld2'
[NbConvertApp] Applying preprocessor: ExecutePreprocessor
[NbConvertApp] Starting kernel: ['/Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/bin/python', '-m', 'ipykernel_launcher', '-f', '/var/folders/25/wwdn6b2d2n1gynshxmh1c_n40000gn/T/tmphkouzfm0.json']
[NbConvertApp] Connecting to: tcp://127.0.0.1:56322
[NbConvertApp] connecting shell channel to tcp://127.0.0.1:56319
[NbConvertApp] Connecting to: tcp://127.0.0.1:56319
[NbConvertApp] connecting iopub channel to tcp://127.0.0.1:56320
[NbConvertApp] Connecting to: tcp://127.0.0.1:56320
[NbConvertApp] connecting stdin channel to tcp://127.0.0.1:56321
[NbConvertApp] Connecting to: tcp://127.0.0.1:56321
[NbConvertApp] connecting heartbeat channel to tcp://127.0.0.1:56323
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Executing cell:
print("Hello World")
[NbConvertApp] output: status
[NbConvertApp] output: execute_input
[NbConvertApp] output: stream
[NbConvertApp] output: status
[NbConvertApp] Applying preprocessor: coalesce_streams
[NbConvertApp] Writing 812 bytes to HelloWorld2.ipynb
  1. jupyter nbconvert --execute HelloWorld.ipynb --to notebook --inplace --debug

このコマンドはHelloWorld.ipynbを実行し、コマンドラインに実行結果を見ることができる。正確には、--inplaceを付けているために自ファイルを上書きし実行している形になる。

yoshitakuMBA:jupyternotebook yoshitaku$ jupyter nbconvert --execute HelloWorld.ipynb --to notebook --inplace --debug
[NbConvertApp] Searching ['/Users/yoshitaku/workspace/jupyternotebook', '/Users/yoshitaku/.jupyter', '/Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/etc/jupyter', '/usr/local/etc/jupyter', '/etc/jupyter'] for config files
[NbConvertApp] Looking for jupyter_config in /etc/jupyter
[NbConvertApp] Looking for jupyter_config in /usr/local/etc/jupyter
[NbConvertApp] Looking for jupyter_config in /Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/etc/jupyter
[NbConvertApp] Looking for jupyter_config in /Users/yoshitaku/.jupyter
[NbConvertApp] Looking for jupyter_config in /Users/yoshitaku/workspace/jupyternotebook
[NbConvertApp] Looking for jupyter_nbconvert_config in /etc/jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /usr/local/etc/jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/etc/jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /Users/yoshitaku/.jupyter
[NbConvertApp] Looking for jupyter_nbconvert_config in /Users/yoshitaku/workspace/jupyternotebook
[NbConvertApp] Converting notebook HelloWorld.ipynb to notebook
[NbConvertApp] Notebook name is 'HelloWorld'
[NbConvertApp] Applying preprocessor: ExecutePreprocessor
[NbConvertApp] Starting kernel: ['/Users/yoshitaku/.pyenv/versions/anaconda3-5.3.1/bin/python', '-m', 'ipykernel_launcher', '-f', '/var/folders/25/wwdn6b2d2n1gynshxmh1c_n40000gn/T/tmpd5mpe1ec.json']
[NbConvertApp] Connecting to: tcp://127.0.0.1:57529
[NbConvertApp] connecting shell channel to tcp://127.0.0.1:57526
[NbConvertApp] Connecting to: tcp://127.0.0.1:57526
[NbConvertApp] connecting iopub channel to tcp://127.0.0.1:57527
[NbConvertApp] Connecting to: tcp://127.0.0.1:57527
[NbConvertApp] connecting stdin channel to tcp://127.0.0.1:57528
[NbConvertApp] Connecting to: tcp://127.0.0.1:57528
[NbConvertApp] connecting heartbeat channel to tcp://127.0.0.1:57530
[NbConvertApp] Executing notebook with kernel: python3
[NbConvertApp] Executing cell:
print("Hello World")
[NbConvertApp] output: status
[NbConvertApp] output: execute_input
[NbConvertApp] output: stream
[NbConvertApp] output: status
[NbConvertApp] Applying preprocessor: coalesce_streams
[NbConvertApp] Writing 812 bytes to HelloWorld.ipynb