Skip to content

base ¤

Generic formatter for executing code.

Classes:

  • ExecutionError

    Exception raised for errors during execution of a code block.

Functions:

ExecutionError ¤

ExecutionError(message: str, returncode: int | None = None)

Bases: Exception

Exception raised for errors during execution of a code block.

Attributes:

  • message

    The exception message.

  • returncode

    The code returned by the execution of the code block.

base_format ¤

base_format(
    *,
    language: str,
    run: Callable,
    code: str,
    md: Markdown,
    html: bool = False,
    source: str = "",
    result: str = "",
    tabs: tuple[str, str] = default_tabs,
    id: str = "",
    id_prefix: str | None = None,
    returncode: int = 0,
    transform_source: (
        Callable[[str], tuple[str, str]] | None
    ) = None,
    session: str | None = None,
    update_toc: bool = True,
    **options: Any
) -> Markup

Execute code and return HTML.

Parameters:

  • language (str) –

    The code language.

  • run (Callable) –

    Function that runs code and returns output.

  • code (str) –

    The code to execute.

  • md (Markdown) –

    The Markdown instance.

  • html (bool, default: False ) –

    Whether to inject output as HTML directly, without rendering.

  • source (str, default: '' ) –

    Whether to show source as well, and where.

  • result (str, default: '' ) –

    If provided, use as language to format result in a code block.

  • tabs (tuple[str, str], default: default_tabs ) –

    Titles of tabs (if used).

  • id (str, default: '' ) –

    An optional ID for the code block (useful when warning about errors).

  • id_prefix (str | None, default: None ) –

    A string used to prefix HTML ids in the generated HTML.

  • returncode (int, default: 0 ) –

    The expected exit code.

  • transform_source (Callable[[str], tuple[str, str]] | None, default: None ) –

    An optional callable that returns transformed versions of the source. The input source is the one that is ran, the output source is the one that is rendered (when the source option is enabled).

  • session (str | None, default: None ) –

    A session name, to persist state between executed code blocks.

  • update_toc (bool, default: True ) –

    Whether to include generated headings into the Markdown table of contents (toc extension).

  • **options (Any, default: {} ) –

    Additional options passed from the formatter.

Returns:

  • Markup

    HTML contents.