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,
    workdir: str | None = None,
    width: int | None = None,
    **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).

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

    The working directory to use for the execution.

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

    Additional options passed from the formatter.

Returns:

  • Markup

    HTML contents.

console_width ¤

console_width(width: int | None = None) -> Iterator[None]

Set the console width for the duration of the context.

The console width is set using the COLUMNS environment variable.

Parameters:

  • width (int | None, default: None ) –

    The width to set the console to.

working_directory ¤

working_directory(
    path: str | None = None,
) -> Iterator[None]

Change the working directory for the duration of the context.

Parameters:

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

    The path to change the working directory to.