def preprocess(self, nb: NotebookNode, resources=None, km=None): """ Preprocess notebook executing each code cell. The input argument *nb* is modified in-place. Note that this function recalls NotebookClient.__init__, which may look wrong. However since the preprocess call acts line an init on execution state it's expected. Therefore, we need to capture it here again to properly reset because traitlet assignments are not passed. There is a risk if traitlets apply any side effects for dual init. The risk should be manageable, and this approach minimizes side-effects relative to other alternatives. One alternative but rejected implementation would be to copy the client's init internals which has already gotten out of sync with nbclient 0.5 release before nbconvert 6.0 released. Parameters ---------- nb : NotebookNode Notebook being executed. resources : dictionary (optional) Additional resources used in the conversion process. For example, passing ``{'metadata': {'path': run_path}}`` sets the execution path to ``run_path``. km: KernelManager (optional) Optional kernel manager. If none is provided, a kernel manager will be created. Returns ------- nb : NotebookNode The executed notebook. resources : dictionary Additional resources used in the conversion process. """ NotebookClient.__init__(self, nb, km) self.reset_execution_trackers() self._check_assign_resources(resources) with self.setup_kernel(): info_msg = self.wait_for_reply(self.kc.kernel_info()) self.nb.metadata["language_info"] = info_msg["content"][ "language_info"] for index, cell in enumerate(self.nb.cells): self.preprocess_cell(cell, resources, index) self.set_widgets_metadata() return self.nb, self.resources
def __init__(self, **kw): nb = kw.get('nb') Preprocessor.__init__(self, nb=nb, **kw) NotebookClient.__init__(self, nb, **kw)