2
3

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 3 years have passed since last update.

papermillで、Notebookの実行結果を返り値として取得

Posted at

papermillでnotebookを実行した時に、こんな感じでnotebookの最後のセルの出力を値として取得できます。

nb = papermill.execute_notebook('index.ipynb', ...)
nb['cells'][-1]['outputs'][0]['text']

使い方

環境

使い方の例

下記のような、fizzbuzzを実行するだけのnotebookを定義してみます。

上記notebookをpapermillで実行するコードを記述します。別途papermillのパラメータ機能を使って引数を定義することで、notebookを引数と返り値を持つひとつの関数のように利用できます。

main.py
import papermill

def notebook_result(path, param):
    nb = papermill.execute_notebook(path, '/dev/null', dict(num=param))
    return nb['cells'][-1]['outputs'][0]['text']

print(notebook_result('fizzbuzz.ipynb', 15)) #=> fizzbuzz
$ poetry run python main.py
(中略)
fizzbuzz

参考

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?