Skip to main content

Render data as clean ASCII/Unicode tables in the terminal with zero configuration

Project description

philiprehberger-text-table

Tests PyPI version Last updated

Render data as clean ASCII/Unicode tables in the terminal with zero configuration.

Installation

pip install philiprehberger-text-table

Usage

from philiprehberger_text_table import table

headers = ["Name", "Age", "City"]
rows = [
    ["Alice", 30, "New York"],
    ["Bob", 25, "London"],
    ["Charlie", 35, "Tokyo"],
]

print(table(headers, rows))

Output:

┌─────────┬─────┬──────────┐
│ Name    │ Age │ City     │
├─────────┼─────┼──────────┤
│ Alice   │  30 │ New York │
│ Bob     │  25 │ London   │
│ Charlie │  35 │ Tokyo    │
└─────────┴─────┴──────────┘

From dictionaries

from philiprehberger_text_table import from_dicts

data = [
    {"name": "Alice", "score": 95},
    {"name": "Bob", "score": 87},
]

print(from_dicts(data))

From CSV

from philiprehberger_text_table import from_csv

print(from_csv("data.csv"))

Styles

# ASCII style
print(table(headers, rows, style="ascii"))

# Markdown style
print(table(headers, rows, style="markdown"))

# Minimal style (no borders)
print(table(headers, rows, style="minimal"))

# Compact style (no outer borders)
print(table(headers, rows, style="compact"))

# Rounded Unicode corners
print(table(headers, rows, style="rounded"))

Column alignment

# Override auto-detection for all columns
print(table(headers, rows, align="center"))

# Per-column alignment
print(table(headers, rows, align=["left", "center", "right"]))

From CSV string

from philiprehberger_text_table import from_csv_string

csv_data = "Name,Age\nAlice,30\nBob,25"
print(from_csv_string(csv_data))

From JSON

from philiprehberger_text_table import from_json, from_json_string

# List of dicts (keys become headers)
print(from_json_string('[{"name":"Alice","age":30},{"name":"Bob","age":25}]'))

# List of lists (first inner list is the header row)
print(from_json_string('[["Name","Age"],["Alice",30],["Bob",25]]'))

# Read from a file
print(from_json("data.json"))

Truncation

print(table(headers, rows, max_width=10))

Round-tripping CSV

from philiprehberger_text_table import to_csv, from_csv

csv_text = to_csv(
    [["Alice", 30], ["Bob", 25]],
    headers=["Name", "Age"],
    file="people.csv",  # optional — also writes to disk
)
print(csv_text)
# Name,Age
# Alice,30
# Bob,25

# Re-read and render
print(from_csv("people.csv"))

Computing column widths

from philiprehberger_text_table import column_widths

widths = column_widths(["name", "count"], [["alice", 100], ["bob", 5]])
# [5, 5]  — max of header and cell str-lengths per column

API

Function Description
table(headers, rows, *, style="unicode", max_width=None, align=None) Render a table from headers and row data
from_dicts(data, *, style="unicode", max_width=None, align=None) Render a table from a list of dictionaries
from_csv(path, *, style="unicode", max_width=None, align=None) Read a CSV file and render as a table
from_csv_string(text, *, style="unicode", max_width=None, align=None) Render a table from CSV string content
from_json(path, *, style="unicode", max_width=None, align=None) Read a JSON file and render as a table (list of dicts or list of lists)
from_json_string(text, *, style="unicode", max_width=None, align=None) Render a table from a JSON string
to_csv(rows, *, headers=None, file=None) Render rows back to a CSV string (round-trips with from_csv); optionally writes to a file
column_widths(headers, rows) Return the per-column widths the renderer would compute

Styles: "unicode", "rounded", "ascii", "markdown", "minimal", "compact"

Alignments: "left", "right", "center" (default: auto-detect, numeric columns right-aligned)

Development

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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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_text_table-0.4.0.tar.gz (195.2 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_text_table-0.4.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_text_table-0.4.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_text_table-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7b91922a96b64e6d9a18bead9084ab345e095592483164c62a06242f573f0166
MD5 7dd2167a9edf55f3f373991db5c486ce
BLAKE2b-256 7e4aa1dcc63ea06abf4746fa898fca88f9985ed67f631e9dd732e1002e6c146d

See more details on using hashes here.

File details

Details for the file philiprehberger_text_table-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_text_table-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13a9278afb8c11e32dfcd20080ef8f9342cf10f0bd223bdf64d100d6a658413b
MD5 37d8fdbd986d5a2d0d2b885cbf814304
BLAKE2b-256 e16e420cd0cc4a71a7769d41a499cbef47998198aad81121506d5fa377a69a71

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