Last active
May 8, 2025 09:05
-
-
Save amnuts/ff361cf098ded4c7f8e4a11f17d8b858 to your computer and use it in GitHub Desktop.
Print out a Rich (python library) Table as either a table, a json structure, or a csv file - handy for piping into jq or saving to a file for processing later, or simply just displaying to screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dateutil.parser | |
from rich.table import Table | |
def table_to_dict(rich_table: Table, transposed: bool = True) -> dict|list: | |
""" | |
Convert a rich.Table into dict | |
Args: | |
rich_table (Table): A rich Table that should be populated by the DataFrame values | |
transposed (bool): If True, the table is transposed (list of objects), otherwise it is a dict of lists | |
Returns: | |
Union[List[dict[str, Any]], dict[str, list[Any]]]: The data extracted from the Table | |
""" | |
data = {x.header: [y for y in x.cells] for x in rich_table.columns} | |
if not transposed: | |
return data | |
keys = list(data.keys()) | |
values = zip(*[data[key] for key in keys]) | |
return [dict(zip(keys, row)) for row in values] | |
def table_to_json(rich_table: Table) -> str: | |
import json | |
return json.dumps(table_to_dict(rich_table), indent=4) | |
def table_to_csv(rich_table: Table) -> str: | |
""" | |
Convert a rich.Table into CSV format | |
Args: | |
rich_table (Table): A rich Table that should be populated by the DataFrame values | |
Returns: | |
str: A CSV string with the Table data as its values | |
""" | |
import csv | |
from io import StringIO | |
data = table_to_dict(rich_table) | |
headers = data[0].keys() if len(data) > 0 else [] | |
output = StringIO() | |
writer = csv.writer(output) | |
writer.writerow(headers) | |
for row in data: | |
writer.writerow(row.values()) | |
return output.getvalue() | |
def print_table(rich_table: Table, output: str): | |
""" | |
Print the rich table to the console or convert it to JSON or CSV format. | |
Args: | |
rich_table: The rich table to print or convert | |
output: What to do with the table - print to console, convert to JSON or CSV | |
""" | |
from rich import print as pprint | |
if output == "json": | |
pprint(table_to_json(rich_table)) | |
elif output == "csv": | |
pprint(table_to_csv(rich_table)) | |
else: | |
from rich.console import Console | |
Console().print(rich_table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment