2021-04-13 04:02:29 +00:00
|
|
|
from collections.abc import Mapping
|
2022-01-24 04:07:52 +00:00
|
|
|
from typing import Any, Optional
|
|
|
|
import warnings
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
from rich.console import JustifyMethod
|
|
|
|
|
|
|
|
from . import box
|
|
|
|
from .highlighter import ReprHighlighter
|
|
|
|
from .pretty import Pretty
|
|
|
|
from .table import Table
|
|
|
|
|
|
|
|
|
|
|
|
def tabulate_mapping(
|
2022-01-24 04:07:52 +00:00
|
|
|
mapping: "Mapping[Any, Any]",
|
|
|
|
title: Optional[str] = None,
|
|
|
|
caption: Optional[str] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
title_justify: Optional[JustifyMethod] = None,
|
|
|
|
caption_justify: Optional[JustifyMethod] = None,
|
|
|
|
) -> Table:
|
|
|
|
"""Generate a simple table from a mapping.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
mapping (Mapping): A mapping object (e.g. a dict);
|
|
|
|
title (str, optional): Optional title to be displayed over the table.
|
|
|
|
caption (str, optional): Optional caption to be displayed below the table.
|
|
|
|
title_justify (str, optional): Justify method for title. Defaults to None.
|
|
|
|
caption_justify (str, optional): Justify method for caption. Defaults to None.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Table: A table instance which may be rendered by the Console.
|
|
|
|
"""
|
2022-01-24 04:07:52 +00:00
|
|
|
warnings.warn("tabulate_mapping will be deprecated in Rich v11", DeprecationWarning)
|
2021-04-13 04:02:29 +00:00
|
|
|
table = Table(
|
|
|
|
show_header=False,
|
|
|
|
title=title,
|
|
|
|
caption=caption,
|
|
|
|
box=box.ROUNDED,
|
|
|
|
border_style="blue",
|
|
|
|
)
|
|
|
|
table.title = title
|
|
|
|
table.caption = caption
|
|
|
|
if title_justify is not None:
|
|
|
|
table.title_justify = title_justify
|
|
|
|
if caption_justify is not None:
|
|
|
|
table.caption_justify = caption_justify
|
|
|
|
highlighter = ReprHighlighter()
|
|
|
|
for key, value in mapping.items():
|
|
|
|
table.add_row(
|
|
|
|
Pretty(key, highlighter=highlighter), Pretty(value, highlighter=highlighter)
|
|
|
|
)
|
|
|
|
return table
|