Format Python data as aligned terminal tables with no dependencies
pip install philiprehberger-cli-tableFormat Python data as aligned terminal tables with no dependencies.
pip install philiprehberger-cli-table
from philiprehberger_cli_table import table
data = [
{"name": "Alice", "age": 30, "city": "Berlin"},
{"name": "Bob", "age": 25, "city": "Vienna"},
{"name": "Charlie", "age": 35, "city": "Zurich"},
]
table(data)
# name age city
# ------- --- ------
# Alice 30 Berlin
# Bob 25 Vienna
# Charlie 35 Zurich
from philiprehberger_cli_table import table
table(
headers=["Product", "Price", "Stock"],
rows=[
["Widget", "9.99", "142"],
["Gadget", "24.99", "38"],
],
)
# Product Price Stock
# ------- ----- -----
# Widget 9.99 142
# Gadget 24.99 38
from philiprehberger_cli_table import table
table(
data=[
{"item": "Coffee", "qty": 3, "price": "4.50"},
{"item": "Tea", "qty": 12, "price": "2.00"},
],
align={"qty": "right", "price": "right"},
)
# item qty price
# ------ --- -----
# Coffee 3 4.50
# Tea 12 2.00
from philiprehberger_cli_table import format_table
# Markdown style
print(format_table(
headers=["Name", "Score"],
rows=[["Alice", "95"], ["Bob", "87"]],
style="markdown",
))
# | Name | Score |
# | ----- | ----- |
# | Alice | 95 |
# | Bob | 87 |
# No borders
print(format_table(
headers=["Name", "Score"],
rows=[["Alice", "95"], ["Bob", "87"]],
style="none",
))
# Name Score
# Alice 95
# Bob 87
from philiprehberger_cli_table import table
table(
data=[{"description": "A very long description that goes on and on"}],
max_width=20,
)
Supports CJK and other wide Unicode characters with correct column alignment.
from philiprehberger_cli_table import table
table(
headers=["Item", "Qty", "Total"],
rows=[
["Coffee", "3", "12.00"],
["Tea", "2", "4.00"],
],
footer=["Total", "5", "16.00"],
)
# Item Qty Total
# ------ --- -----
# Coffee 3 12.00
# Tea 2 4.00
# ------ --- -----
# Total 5 16.00
A footer can also be passed as a dict keyed by header name:
table(
data=[{"item": "Coffee", "n": 3}, {"item": "Tea", "n": 2}],
footer={"item": "Total", "n": 5},
)
| Function | Description |
|---|---|
format_table(headers, rows, *, data, align, max_width, style, footer) | Returns a formatted table as a string |
table(data, headers, rows, **kwargs) | Prints a formatted table to stdout |
| Parameter | Type | Default | Description |
|---|---|---|---|
headers | list[str] | None | None | Column header names |
rows | list[list[Any]] | None | None | List of row value lists |
data | list[dict[str, Any]] | None | None | List of dicts (keys become headers) |
align | dict[str, Align] | None | None | Per-column alignment: "left", "right", "center" |
max_width | int | None | None | Truncate cell values to this width |
style | Style | "simple" | Border style: "simple", "markdown", "none" |
footer | list | dict | None | None | Footer row rendered below a divider (totals/summary) |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: