콘텐츠로 이동

rich

rich_print_to_string(objs)

Convert a rich console print output to a string.

Source code in src/slack_helpers/utils/rich.py
def rich_print_to_string(objs: Iterable[Any] | Any) -> str:
    """Convert a rich console print output to a string."""
    console = Console(
        width=CONSOLE_WIDTH,
        no_color=True,
        highlight=False,
        record=True,
        file=open(os.devnull, "w"),  # noqa: SIM115
    )

    if not isinstance(objs, Iterable):
        objs = [objs]
    for obj in objs:
        console.print(obj)

    return console.export_text()

rich_print_to_html(objs, backend='ansi2html')

Convert a rich console print output to an html string.

rich backend can be prettier with the following caveats: 1. It produces a larger file size (2x) 2. It isn't as easy to read as a plain HTML code due to alignment and many html tags. 3. Slack doesn't understand the HTML code produced by console.export_html() and treat it as a binary file.

Parameters:

Name Type Description Default
objs Iterable[Any] | Any

The objects to print. Possibly but not limited to rich objects.

required
backend Literal['ansi2html', 'rich']

"ansi2html" or "rich". Defaults to "ansi2html".

'ansi2html'
Source code in src/slack_helpers/utils/rich.py
def rich_print_to_html(
    objs: Iterable[Any] | Any,
    backend: Literal["ansi2html", "rich"] = "ansi2html",
) -> str:
    """
    Convert a rich console print output to an html string.

    rich backend can be prettier with the following caveats:
    1. It produces a larger file size (2x)
    2. It isn't as easy to read as a plain HTML code due to alignment and many html tags.
    3. Slack doesn't understand the HTML code produced by `console.export_html()` and treat it as a binary file.

    Args:
        objs: The objects to print. Possibly but not limited to rich objects.
        backend: "ansi2html" or "rich". Defaults to "ansi2html".
    """
    assert backend in ("ansi2html", "rich")

    console = Console(width=CONSOLE_WIDTH, record=True, file=open(os.devnull, "w"))  # noqa: SIM115

    if not isinstance(objs, Iterable):
        objs = [objs]
    for obj in objs:
        console.print(obj)

    if backend == "rich":
        return console.export_html(theme=MONOKAI)

    tb_ansi = console.export_text(styles=True)  # text with ansi color codes
    return Ansi2HTMLConverter().convert(tb_ansi)

rich_print_to_svg(objs, title)

Convert a rich console print to an svg string.

Source code in src/slack_helpers/utils/rich.py
def rich_print_to_svg(objs: Iterable[Any] | Any, title: str) -> str:
    """Convert a rich console print to an svg string."""
    console = Console(width=CONSOLE_WIDTH, record=True, file=open(os.devnull, "w"))  # noqa: SIM115

    if not isinstance(objs, Iterable):
        objs = [objs]
    for obj in objs:
        console.print(obj)

    return console.export_svg(title=title)

rich_traceback_to_string(tb)

Convert a rich traceback to a string.

Examples:

>>> try:
...    1 / 0
... except ZeroDivisionError:
...     import rich.traceback
...     tb = rich.traceback.Traceback()
...     print(rich_traceback_to_string(tb))
╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
...
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ZeroDivisionError: division by zero
Source code in src/slack_helpers/utils/rich.py
def rich_traceback_to_string(tb: Traceback) -> str:
    """
    Convert a rich traceback to a string.

    Examples:
        >>> try:   # doctest: +ELLIPSIS
        ...    1 / 0
        ... except ZeroDivisionError:
        ...     import rich.traceback
        ...     tb = rich.traceback.Traceback()
        ...     print(rich_traceback_to_string(tb))
        ╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
        ...
        ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
        ZeroDivisionError: division by zero
        <BLANKLINE>
    """
    return rich_print_to_string([tb])

rich_traceback_to_html(tb, backend='ansi2html')

Convert a rich traceback to an html string.

Examples:

>>> try:
...    1 / 0
... except ZeroDivisionError:
...     import rich.traceback
...     tb = rich.traceback.Traceback()
...     print(rich_traceback_to_html(tb))
<!DOCTYPE HTML ...
</html>
Source code in src/slack_helpers/utils/rich.py
def rich_traceback_to_html(
    tb: Traceback, backend: Literal["ansi2html", "rich"] = "ansi2html"
) -> str:
    """
    Convert a rich traceback to an html string.

    Examples:
        >>> try:   # doctest: +ELLIPSIS
        ...    1 / 0
        ... except ZeroDivisionError:
        ...     import rich.traceback
        ...     tb = rich.traceback.Traceback()
        ...     print(rich_traceback_to_html(tb))
        <!DOCTYPE HTML ...
        </html>
        <BLANKLINE>
    """
    return rich_print_to_html([tb], backend=backend)

rich_traceback_to_svg(tb, title)

Convert a rich traceback to an svg string.

Examples:

>>> try:
...    1 / 0
... except ZeroDivisionError:
...     import rich.traceback
...     tb = rich.traceback.Traceback()
...     print(rich_traceback_to_svg(tb, "title"))
<svg class="rich-terminal" ...
</svg>
Source code in src/slack_helpers/utils/rich.py
def rich_traceback_to_svg(tb: Traceback, title: str) -> str:
    """
    Convert a rich traceback to an svg string.

    Examples:
        >>> try:   # doctest: +ELLIPSIS
        ...    1 / 0
        ... except ZeroDivisionError:
        ...     import rich.traceback
        ...     tb = rich.traceback.Traceback()
        ...     print(rich_traceback_to_svg(tb, "title"))
        <svg class="rich-terminal" ...
        </svg>
        <BLANKLINE>
    """
    return rich_print_to_svg([tb], title)