【问题标题】:Pass arguments to jupyter notebook started with nbformat将参数传递给以 nbformat 开头的 jupyter notebook
【发布时间】:2017-08-08 10:16:55
【问题描述】:

我是 Jupyter 的初学者。我有一个Python 程序,它使用nbformat execute API 启动我的笔记本。

效果很好,但有一件事我还没有弄清楚; 如何将数据从我的程序传递到我将要执行的笔记本?

为了完整起见,这里是我用来运行笔记本的代码:

import nbformat
from nbconvert.preprocessors import ExecutePreprocessor

class NotebookExecutor:
    def __init__(self, name, base_path, notebook_filename_in, notebook_filename_out, timeout=-1):
        self.name = name
        if base_path.endswith('/') is False:
            base_path = base_path + '/'
        self.base_path = base_path
        self.notebook_filename_in = notebook_filename_in
        self.notebook_filename_out = notebook_filename_out
        self.timeout = timeout

    def run(self):
        print("Running notebook '" + self.name + "'")
        nb = nbformat.read(open(self.base_path + self.notebook_filename_in), as_version=4)
        ep = ExecutePreprocessor(timeout=self.timeout, kernel_name='python3', allow_errors=True)
        try:
            ep.preprocess(nb, {'metadata': {'path': self.base_path}})
        except CellExecutionError:
            msg = 'Error executing the notebook "%s".\n\n' % self.notebook_filename_in
            msg += 'See notebook "%s" for the traceback.' % self.notebook_filename_out
            print(msg)
        # raise
        finally:
            nbformat.write(nb, open(self.base_path + self.notebook_filename_out, mode='wt'))

ne = NotebookExecutor('Test', '/my/path', 'MyBook.ipynb', 'MyBook_out.ipynb')
ne.run()

【问题讨论】:

    标签: python python-3.x jupyter-notebook jupyter papermill


    【解决方案1】:

    我推荐使用造纸厂,我不确定这是否能帮助您解决问题。

    https://github.com/nteract/papermill

    我可以用它来循环使用不同参数的笔记本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 2016-09-28
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-26
      相关资源
      最近更新 更多