Skip to main content

Format Python data as aligned terminal tables with no dependencies

Project description

philiprehberger-cli-table

Tests PyPI version License

Format Python data as aligned terminal tables with no dependencies.

Installation

pip install philiprehberger-cli-table

Usage

Dict list mode

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

Headers + rows mode

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

Column alignment

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

Styles

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

Cell truncation

from philiprehberger_cli_table import table

table(
    data=[{"description": "A very long description that goes on and on"}],
    max_width=20,
)

Wide character support

Supports CJK and other wide Unicode characters with correct column alignment.

API

Function / Class Description
format_table(headers, rows, *, data, align, max_width, style) 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"

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_cli_table-0.1.6.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_cli_table-0.1.6-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_cli_table-0.1.6.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_cli_table-0.1.6.tar.gz
Algorithm Hash digest
SHA256 69e98a8b1e79d84e05b6a845f26da2c60dc5dcc421be7a8877a21a353b28bd35
MD5 11aad17def9bd1e1da2428cec3b64eb2
BLAKE2b-256 dad2169ad1de76578c77f9167070d543c1b41b14a27d2c8e961f02a49ea5b8a5

See more details on using hashes here.

File details

Details for the file philiprehberger_cli_table-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_cli_table-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 40f79aa503490c4e8445afe6b857994b083ecb68521092f09bfd7b11acfa7fa7
MD5 5b1078d8c75e1b04b23333480eb0f3e2
BLAKE2b-256 8fa22fec2f4ca604fc535f74944631def40d4f6c5893d8cdbb01a2f9471180b7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page