The Processor

The base class for all notebook processors

class Processor

<source>

(notebook: AttributeDictionary)

Base class for all notebook processors. Any processors should inherit this class.

When writing a processor, you can override methods that modify the content of a cell with the process_cell function.

The class stores the entire notebook in the notebook attribute.

When using a processor, simply call the class and pass in a single cell.

Example:

class BasicProcessor(Processor):
    "A basic processor that adds a comment to the top of a cell"
    directive = "process"

    def process(self, cell):
        cell.source = f"# This code has been processed!\n{cell.source}"

has_directives

<source>

(cell: AttributeDictionary)

Checks if cell contains any directives in self.directives

process

<source>

(cell: AttributeDictionary)

Parameters:

  • cell (AttributeDictionary) – A cell from a Jupyter Notebook

A function to apply on a cell. Should use self.has_directives to see if the processor should be applied

Example:

def process(self, cell):
    if self.has_directives(self, cell):
        cell.source = "Found a directive!"

process_cell

<source>

(cell: AttributeDictionary)

Parameters:

  • cell (AttributeDictionary) – A cell from a Jupyter Notebook

Applies the processor to a cell if the cell is of the correct type and contains the correct directive

class NotebookProcessor

<source>

(path: str = None, processors: list = [], notebook: AttributeDictionary = None, config: dict = {}, debug: bool = False, remove_directives: bool = True, process_immediately: bool = False)

Parameters:

  • path (str, optional, defaults to None) – The path to the notebook
  • processors (list, optional, defaults to []) – A list of functions to apply to the notebook
  • notebook (AttributeDictionary, optional, defaults to None) – An object representing all the cells in a Jupyter Notebook. If None, will be loaded from path
  • processor_args (dict, optional, defaults to {}) – A dictionary of arguments to pass to a given processor. Should be structured such as: {"processor_name: {"argument_name": argument_value}}
  • debug (bool, optional, defaults to False) – Whether to print debug statements
  • remove_directives (bool, optional, defaults to True) – Whether to remove directives from each cell after processing
  • process_immediately (bool, optional, defaults to False) – Whether to process the notebook after initialization

Processes notebook cells and comments in a notebook

process_cell

<source>

(processor: callable, cell: AttributeDictionary)

Parameters:

  • processor (callable) – A function to apply to a notebook cell’s content
  • cell (AttributeDictionary) – A cell from a Jupyter Notebook

Processes a single cell of a notebook. Should not be called explicitly and instead a user should use process_notebook

process_notebook

<source>

()

Processes the content of the notebook