自定义执行处理器nbconverter

2024-09-23 06:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个自定义ExecuteProcessor,它将计算笔记本中每个单元格的执行时间Implementation of the ExecutePreprocessor 。 为了做到这一点,我想重写preprocess_cell方法。我的做法:

class MyExecutePreprocessor(ExecutePreprocessor):
    def __init__(self, *args, **kw):
        super(MyExecutePreprocessor, self).__init__(*args, **kwargs)
    def preprocess_cell(self, cell, resources, index):
        """
        Override if you want to apply some preprocessing to each cell.
        Must return modified cell and resource dictionary.
        Parameters
        ----------
        cell : NotebookNode cell
            Notebook cell being processed
        resources : dictionary
            Additional resources used in the conversion process.  Allows
            preprocessors to pass variables into the Jinja engine.
        index : int
            Index of the cell being processed
        """
        time_start = time.time()
        self._check_assign_resources(resources)
        cell = self.execute_cell(cell, index, store_history=True)
        print(time.time() - time_start)
        return cell, self.resources

但当我试图对笔记本进行预处理时

nb = nbformat.read('my_notebook.ipynb', as_version=4)
ep = MyExecutePreprocessor(kernel_name='python 3', timeout=100)
ep.preprocess(nb, {'metadata': {'path': 'nbs/'}})

AttributeError被抛出AttributeError: 'MyExecutePreprocessor' object has no attribute '_check_assign_resources' 发生了什么?我用错了super


Tags: ofthetoselfindextimeinitdef