Python 将参数传递到以nbformat开头的jupyter笔记本

Python 将参数传递到以nbformat开头的jupyter笔记本,python,python-3.x,jupyter-notebook,jupyter,papermill,Python,Python 3.x,Jupyter Notebook,Jupyter,Papermill,我是Jupyter的初学者。我有一个Python程序,它使用 它工作得很好,但有一件事我还没有弄明白;如何将程序中的数据传递给即将执行的笔记本 为了完整起见,以下是我用来运行笔记本的代码: import nbformat from nbconvert.preprocessors import ExecutePreprocessor class NotebookExecutor: def __init__(self, name, base_path, notebook_filename_

我是Jupyter的初学者。我有一个
Python
程序,它使用

它工作得很好,但有一件事我还没有弄明白;如何将程序中的数据传递给即将执行的笔记本

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

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()

我建议使用造纸机,我不确定这是否能帮你解决问题

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