urnc.preprocessor package

Function used by urnc.convert for generating student versions of notebooks

Submodules

urnc.preprocessor.add_tags module

class AddTags(**kwargs)

Bases: Preprocessor

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

assignment_keywords

Keywords to search for in the notebook headers

assignment_start_tag

Tag to assign to the first cell of an assignment

assignment_tag

Tag to assign to assignment cells

ignore_tag

Tag to assign to cells that should be ignored

is_assignment_end(cell, header)
Parameters:
  • cell (NotebookNode)

  • header (str | None)

Return type:

bool

is_assignment_start(cell, header)
Parameters:
  • cell (NotebookNode)

  • header (str | None)

Return type:

bool

is_solution(cell, header)
Parameters:
  • cell (NotebookNode)

  • header (str | None)

Return type:

bool

preprocess(nb, resources)

Preprocessing to apply on each notebook.

Must return modified nb, resources.

If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead.

Parameters:
  • nb (NotebookNode) – Notebook being converted

  • resources (dictionary) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.

solution_keywords

Keywords to search for in the notebook headers

solution_tag

Tag to assign to solution cells

extract_header(cell)
header_to_id(header)
Parameters:

header (str)

Return type:

str

urnc.preprocessor.check_outputs module

class CheckOutputs(**kwargs)

Bases: Preprocessor

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

check_output(cell)
Return type:

bool

max_line_count

Maximum number of lines in a cell output

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

urnc.preprocessor.clear_outputs module

class ClearOutputs(**kwargs)

Bases: Preprocessor

Removes the output from all code cells in a notebook.

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

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

urnc.preprocessor.clear_tagged module

class ClearTaggedCells(**kwargs)

Bases: Preprocessor

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

preprocess(nb, resources)

Preprocessing to apply on each notebook.

Must return modified nb, resources.

If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead.

Parameters:
  • nb (NotebookNode) – Notebook being converted

  • resources (dictionary) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.

tags

List of tags to clear from the notebook

urnc.preprocessor.executor module

class ExecutePreprocessor(**kwargs)

Bases: Preprocessor

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

execute_notebook(nb)
Parameters:

nb (NotebookNode)

preprocess(nb, resources)

Preprocessing to apply on each notebook.

Must return modified nb, resources.

If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead.

Parameters:
  • nb (NotebookNode) – Notebook being converted

  • resources (dictionary) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.

supported_kernels

List of supported kernels

urnc.preprocessor.image module

class ImageChecker(**kwargs)

Bases: Preprocessor

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

autofix

Automatically fix paths with a single valid fix

base_path

The base path of the notebook

check_image(nb_path, src)
Parameters:
  • nb_path (Path)

  • src (str)

Return type:

Tuple[bool, str | None]

interactive

Interactive mode for fixing paths

invalid_tag

Tag to assign to cells with invalid images

max_image_size

The maximum size of an image

preprocess(nb, resources)

Preprocessing to apply on each notebook.

Must return modified nb, resources.

If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead.

Parameters:
  • nb (NotebookNode) – Notebook being converted

  • resources (dictionary) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.

replace_src(nb_path, cell, match)
Parameters:

nb_path (Path)

url_is_valid(url)

urnc.preprocessor.solutions module

class LineTags(*values)

Bases: StrEnum

END_KEY = 'end_key'
NONE = 'none'
SKELETON = 'skeleton'
SKELETON_KEY = 'skeleton_key'
SOLUTION = 'solution'
SOLUTION_KEY = 'solution_key'
class SolutionProcessor(**kwargs)

Bases: Preprocessor

Public constructor

Parameters:
  • config (Config) – Configuration file structure

  • **kw – Additional keyword arguments passed to parent

end_keywords

Keywords to search for in the cell source

output

Which lines to keep. Either ‘skeleton’, ‘solution’ of ‘none’

preprocess(nb, resources=None)

Preprocessing to apply on each notebook.

Must return modified nb, resources.

If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead.

Parameters:
  • nb (NotebookNode) – Notebook being converted

  • resources (dictionary) – Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine.

Return type:

Tuple[NotebookNode, Dict | None]

scan_lines(text)
Parameters:

text (str)

Return type:

List[Tuple[str, str]]

skeleton_keywords

Keywords to search for in the cell source

solution_keywords

Keywords to search for in the notebook headers

solution_tag

Tag to assign to solution cells

strip_cell(cell)
Parameters:

cell (NotebookNode)

Return type:

NotebookNode | None

tag_line(line)
Parameters:

line (str)

Return type:

Tuple[str, str]

urnc.preprocessor.util module

cell_preview(cell)
Parameters:

cell (NotebookNode)

has_tag(cell, tag)
Parameters:
  • cell (NotebookNode)

  • tag (str)

Return type:

bool

has_tags(cell, tags)
Parameters:
  • cell (NotebookNode)

  • tags (List[str])

Return type:

bool

remove_tag(cell, tag)
Parameters:
  • cell (NotebookNode)

  • tag (str)

set_tag(cell, tag)
Parameters:
  • cell (NotebookNode)

  • tag (str)

starts_with(string, keywords)
Parameters:
  • string (str | None)

  • keywords (List[str])

Return type:

bool

string_to_byte(input)
Parameters:

input (str)

Return type:

int

to_snake_case(string)
Parameters:

string (str)

Return type:

str