Skip to content

analog_daddy.mootils

A collection of marimo utility functions.

While marimo is quite feature-rich, these functions are intended to be convenience functions for common tasks that are not yet implemented in marimo. These might be deprecated if they become available in marimo.

df_repr_docstr_clip(df, max_rows=3)

Copy the repr of a polars DataFrame to the clipboard.

Parameters:

Name Type Description Default
df DataFrame

The DataFrame to copy the repr of.

required
max_rows int

The maximum number of rows to include in the repr.

3

Returns:

Type Description
CopyToClipboard

A CopyToClipboard object containing the repr of the DataFrame.

Examples:

>>> a = pl.DataFrame(
...     {"x": [9.0, 11.0], "foo": [10.0, 10.0], "bar": [5.0, 5.0]},
... )
>>> df_repr_docstr_clip(a).text_to_copy.startswith("shape: (2, 3)")
True
Source code in src/analog_daddy/mootils.py
def df_repr_docstr_clip(
    df: pl.DataFrame, max_rows: int = 3
) -> CopyToClipboard:
    """
    Copy the repr of a polars DataFrame to the clipboard.

    Parameters
    ----------
    df : pl.DataFrame
        The DataFrame to copy the repr of.
    max_rows : int, default=3
        The maximum number of rows to include in the repr.

    Returns
    -------
    CopyToClipboard
        A CopyToClipboard object containing the repr of the DataFrame.

    Examples
    --------
    >>> a = pl.DataFrame(
    ...     {"x": [9.0, 11.0], "foo": [10.0, 10.0], "bar": [5.0, 5.0]},
    ... )
    >>> df_repr_docstr_clip(a).text_to_copy.startswith("shape: (2, 3)")
    True
    """

    return CopyToClipboard(
        text_to_copy=df.slice(offset=0, length=max_rows).__repr__(),
    )

format_mapping_num_to_si(df)

Build a format mapping that renders numeric columns as SI strings.

Intended for the format_mapping argument of mo.ui.table and mo.ui.dataframe. Columns are matched with cs.numeric() and rendered with :func:render.

Parameters:

Name Type Description Default
df DataFrame

DataFrame whose numeric columns to map.

required

Returns:

Type Description
dict[str, Callable]

Column name to formatter mapping.

References
Source code in src/analog_daddy/mootils.py
def format_mapping_num_to_si(df: pl.DataFrame) -> dict[str, Callable]:
    """
    Build a format mapping that renders numeric columns as SI strings.

    Intended for the ``format_mapping`` argument of ``mo.ui.table`` and
    ``mo.ui.dataframe``. Columns are matched with ``cs.numeric()`` and
    rendered with :func:`render`.

    Parameters
    ----------
    df : pl.DataFrame
        DataFrame whose numeric columns to map.

    Returns
    -------
    dict[str, Callable]
        Column name to formatter mapping.

    References
    ----------
    - [marimo#9663](https://github.com/marimo-team/marimo/issues/9663)
    - [marimo#7568](https://github.com/marimo-team/marimo/issues/7568)
    """
    return dict.fromkeys(cs.expand_selector(df, cs.numeric()), render)

rich_md(*renderables, console_kwargs=None, print_kwargs=None, export_html_kwargs=None)

A minimal helper function to utilize rich markup within marimo.

Created this for shits-n-giggles. This is in no way complete! I don't use this as much as I would like to, however, in case you have some text, that you would like to keep consistent with your console markup, this might be an option.

Parameters:

Name Type Description Default
*renderables RenderableType

The renderable objects to be printed using rich.

()
console_kwargs dict[str, Any] | None

Keyword arguments to customize the rich Console.

None
print_kwargs dict[str, Any] | None

Keyword arguments to customize the rich print method.

None
export_html_kwargs dict[str, Any] | None

Keyword arguments to customize the rich export_html method.

None

Returns:

Type Description
Html

The HTML representation of the rich renderables.

Examples:

>>> "font-weight: bold" in rich_md("[bold green]Hello[/] World").text
True
>>> "font-style: italic" in rich_md(
...     "[bold]Bold[italic] bold and italic [/bold]italic[/italic]"
... ).text
True
>>> "⚠️" in rich_md(":warning-emoji: you have been warned!").text
True
Source code in src/analog_daddy/mootils.py
def rich_md(
    *renderables: RenderableType,
    console_kwargs: dict[str, Any] | None = None,
    print_kwargs: dict[str, Any] | None = None,
    export_html_kwargs: dict[str, Any] | None = None,
) -> mo.Html:
    """
    A minimal helper function to utilize rich markup within marimo.

    Created this for shits-n-giggles. This is in no way complete! I
    don't use this as much as I would like to, however, in case you have
    some text, that you would like to keep consistent with your console
    markup, this might be an option.

    Parameters
    ----------
    *renderables : RenderableType
        The renderable objects to be printed using rich.
    console_kwargs : dict[str, Any] | None, default=None
        Keyword arguments to customize the rich Console.
    print_kwargs : dict[str, Any] | None, default=None
        Keyword arguments to customize the rich print method.
    export_html_kwargs : dict[str, Any] | None, default=None
        Keyword arguments to customize the rich export_html method.

    Returns
    -------
    mo.Html
        The HTML representation of the rich renderables.

    Examples
    --------
    >>> "font-weight: bold" in rich_md("[bold green]Hello[/] World").text
    True
    >>> "font-style: italic" in rich_md(
    ...     "[bold]Bold[italic] bold and italic [/bold]italic[/italic]"
    ... ).text
    True
    >>> "⚠️" in rich_md(":warning-emoji: you have been warned!").text
    True
    """
    console_kwargs = {
        "color_system": "truecolor",
        "record": True,
        **(console_kwargs or {}),
    }
    print_kwargs = print_kwargs or {}
    export_html_kwargs = {
        "inline_styles": True,
        "code_format": "{code}",
        **(export_html_kwargs or {}),
    }

    capture = io.StringIO()
    console = Console(file=capture, **console_kwargs)
    console.print(*renderables, **print_kwargs)
    return mo.Html(console.export_html(**export_html_kwargs))