2024-07-06 18:59:50 +00:00
|
|
|
import sys
|
2024-07-07 13:10:14 +00:00
|
|
|
from typing import Any, Dict, List, Optional, Sequence, Union
|
2024-07-06 16:26:36 +00:00
|
|
|
|
2024-07-07 07:38:02 +00:00
|
|
|
from pandas import DataFrame
|
2024-07-06 16:26:36 +00:00
|
|
|
from rich.console import Console
|
2024-07-07 10:47:27 +00:00
|
|
|
from rich.table import Column, Table
|
2024-07-07 06:36:51 +00:00
|
|
|
from rich.text import Text
|
|
|
|
|
|
|
|
|
|
|
|
TextOrString = Union[str, Text]
|
2024-07-06 16:26:36 +00:00
|
|
|
|
|
|
|
|
2024-07-06 16:30:48 +00:00
|
|
|
def print_rich_table(
|
2024-07-07 06:36:51 +00:00
|
|
|
tabular_data: Sequence[Union[Dict[str, Any], Sequence[TextOrString]]],
|
|
|
|
headers: Sequence[str],
|
2024-07-06 18:59:50 +00:00
|
|
|
summary: Optional[str] = None,
|
|
|
|
*,
|
|
|
|
table_kwargs: Optional[Dict[str, Any]] = None,
|
2024-07-06 16:30:48 +00:00
|
|
|
) -> None:
|
2024-07-07 10:47:27 +00:00
|
|
|
table = Table(
|
|
|
|
*[c if isinstance(c, Column) else Column(c, justify="right") for c in headers],
|
|
|
|
title=summary,
|
|
|
|
**(table_kwargs or {}),
|
|
|
|
)
|
2024-07-06 16:26:36 +00:00
|
|
|
|
|
|
|
for row in tabular_data:
|
2024-07-06 16:30:48 +00:00
|
|
|
if isinstance(row, dict):
|
2024-07-07 10:47:27 +00:00
|
|
|
table.add_row(
|
|
|
|
*[
|
2024-07-07 11:08:52 +00:00
|
|
|
row[header] if isinstance(row[header], Text) else str(row[header])
|
2024-07-07 10:47:27 +00:00
|
|
|
for header in headers
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2024-07-06 16:30:48 +00:00
|
|
|
else:
|
2024-07-07 13:10:14 +00:00
|
|
|
row_to_add: List[Union[str, Text]] = [r if isinstance(r, Text) else str(r) for r in row]
|
|
|
|
table.add_row(*row_to_add)
|
2024-07-06 16:26:36 +00:00
|
|
|
|
2024-07-06 18:59:50 +00:00
|
|
|
console = Console(
|
|
|
|
width=200 if "pytest" in sys.modules else None,
|
|
|
|
)
|
2024-07-06 16:26:36 +00:00
|
|
|
console.print(table)
|
2024-07-07 07:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _format_value(value: Any, *, floatfmt: str) -> str:
|
|
|
|
if isinstance(value, float):
|
|
|
|
return f"{value:{floatfmt}}"
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
|
|
|
|
def print_df_rich_table(
|
|
|
|
tabular_data: DataFrame,
|
|
|
|
headers: Sequence[str],
|
|
|
|
summary: Optional[str] = None,
|
|
|
|
*,
|
|
|
|
show_index=False,
|
|
|
|
index_name: Optional[str] = None,
|
|
|
|
table_kwargs: Optional[Dict[str, Any]] = None,
|
|
|
|
) -> None:
|
|
|
|
table = Table(title=summary, **(table_kwargs or {}))
|
|
|
|
|
|
|
|
if show_index:
|
|
|
|
index_name = str(index_name) if index_name else tabular_data.index.name
|
|
|
|
table.add_column(index_name)
|
|
|
|
|
|
|
|
for header in headers:
|
|
|
|
table.add_column(header, justify="right")
|
|
|
|
|
|
|
|
for value_list in tabular_data.itertuples(index=show_index):
|
|
|
|
row = [_format_value(x, floatfmt=".3f") for x in value_list]
|
|
|
|
table.add_row(*row)
|
|
|
|
|
|
|
|
console = Console(
|
|
|
|
width=200 if "pytest" in sys.modules else None,
|
|
|
|
)
|
|
|
|
console.print(table)
|