Skip to main content

span-table is a lightweight Python helper for rendering ASCII tables with merged cells using Rich-style markup.

Project description

span-table

span-table is a lightweight Python helper for rendering ASCII tables with merged cells using Rich-style markup.

Features

  • Render table layouts with custom merged spans
  • Support multi-line cell content
  • Uses rich box styles for flexible borders
  • Compatible with Python >=3.14

Installation

pip install span-table

Quick Start

from span_table import span_table
from rich import print

table = [
    ["Header 1", "Header 2", "Header3", "Header 4"],
    ["row 1", "column 2", "column 3", "column 4"],
    ["row 2", "Cells span columns.", "", ""],
    ["row 3", "Cells\nspan rows.", "- Cells\n- contain\n- blocks", ""],
    ["row 4", "", "", ""]
]

# Spans are defined as ((start_row, start_col), (end_row, end_col)) with zero-based indexes
my_spans = [
    ((2, 1), (2, 3)),
    ((3, 1), (4, 1)),
    ((3, 2), (4, 3)),
]

print(span_table(table, my_spans))

This renders:

┌──────────┬────────────┬──────────┬──────────┐
│ Header 1 │ Header 2   │ Header3  │ Header 4 │
├──────────┼────────────┼──────────┼──────────┤
│ row 1    │ column 2   │ column 3 │ column 4 │
├──────────┼────────────┴──────────┴──────────┤
│ row 2    │ Cells span columns.              │
├──────────┼────────────┬─────────────────────┤
│ row 3    │ Cells      │ - Cells             │
├──────────┤ span rows. │ - contain           │
│ row 4    │            │ - blocks            │
└──────────┴────────────┴─────────────────────┘

Advanced Usage

from span_table import span_table
from rich import print

table = [
    ["Time", "[on red]Height[/]", "", "[on green]Weight[/]", ""],
    ["", "Total", "Change", "Total", "Change"],
    ["15:30", "1", "2", "3", "4"]
]

spans = [
    ((0, 0), (1, 0)),
    ((0, 1), (0, 2)),
    ((0, 3), (0, 4))
]

print(span_table(table, spans))

This renders:

┌───────┬────────────────┬────────────────┐
│ Time  │<red bg> Height │<Green BG>Weight│
│       ├───────┬────────┼───────┬────────┤
│       │ Total │ Change │ Total │ Change │
├───────┼───────┼────────┼───────┼────────┤
│ 15:30 │ 1     │ 2      │ 3     │ 4      │
└───────┴───────┴────────┴───────┴────────┘

API

span_table(data, spans, box=rich_box.SQUARE) -> str

Render a text table with optional merged cells.

  • data: list[list[str]] — table rows and columns
  • spans: list[tuple[tuple[int, int], tuple[int, int]]] — list of merge regions
  • box: rich.box.Box — optional Rich box style such as box.SQUARE

Span rules

  • Each span is a rectangular region defined by a start and end coordinate
  • Coordinates are zero-based: (row_index, column_index)
  • Spans can cover a single cell or multiple cells
  • Spans must be within the table bounds

Rich markup support

Cell values may include Rich markup tags such as [bold], [italic], or color tags but does not support emoji yet. The library preserves markup while measuring text layout.

Notes

  • Ensure each row has the same number of columns
  • Non-merged cells may be omitted from spans
  • The library automatically fills any cells not included in spans as standard individual cells

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

span_table-0.3.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

span_table-0.3.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file span_table-0.3.0.tar.gz.

File metadata

  • Download URL: span_table-0.3.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for span_table-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ec8337963518898f2d782da922feb92abc3ac0f66359e1f04e36a437df287b46
MD5 e1338e69b6670a1aaebba6eeba7f3015
BLAKE2b-256 1d0358f55c5fde08bda912f8734e20b162f5f8e3e5d66064ff3f221c352e5b44

See more details on using hashes here.

File details

Details for the file span_table-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: span_table-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for span_table-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c77d4ba66105dbb604913d6c1b99d53d520148e96f338711cc3b5d2f20ddd654
MD5 91e2647e5fc0948ea375562b41da1bda
BLAKE2b-256 2358b387f9ab76a328c8407efd1e6ddd207cb2e8bfb0c4cd7676e9721913e795

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