Skip to main content

flexablelib

Flexible Table Library — format tabular data with pretty styles.

PyPI version Python versions Downloads per month Development status License


flexablelib

Flexible Table Library

This is a python package that aims to provide a simple and pretty way of printing tables to the console making use of a class.

The idea started as an attempt to reproduce the behavior of the PM2 package when it displays tables to show data. Later, heavy inspiration came of two other python packages:

A big part of the behavior of this package was replicated from these.

Installation

pip install flexablelib on Windows.

pip3 install flexablelib on Linux

Usage

Creating a table is simple.

Code Example

from flexablelib import Table

new_table = Table()
print(new_table)

Output

++
++
++

This is an empty table. It has no data so it only displays this strange thing.

A table that has columns but no rows — a query that came back empty — shows its headers rather than raising:

┌──────┬───────┐
│ name │ value │
╞══════╪═══════╡
└──────┴───────┘

It's possible to add data as columns or rows, or even alternating each one. Any untitled column will be named automatically.

Code Example

new_table.add_column('Name', ['Jade', 'John', 'Jane'])
new_table.add_column('Age', [20, 30, 40])
new_table.add_column('Results', [9.651, 3, 245.7])
print(new_table)

Output

+----------------------+
| Name   Age   Results |
+======+=====+=========+
| Jade |  20 |   9.651 |
| John |  30 |   3     |
| Jane |  40 | 245.7   |
+------+-----+---------+

Code Example

new_table = Table()
new_table.add_column('Name', ['Jade', 'John', 'Jane'])
new_table.add_column('Age', [20, 30, 40])
new_table.add_column('Test\nResults', [9.651, 3, 245.7])
new_table.add_row(['Piotr\nBaltimore', 27, 3.5])
new_table.add_row(['Sam', 21, 0.6519])
print(new_table)

Output

+----------------------------+
| Name        Age       Test |
|                    Results |
+===========+=====+==========+
| Jade      |  20 |   9.651  |
| John      |  30 |   3      |
| Jane      |  40 | 245.7    |
| Piotr     |  27 |   3.5    |
| Baltimore |     |          |
| Sam       |  21 |   0.6519 |
+-----------+-----+----------+

As it is visible, the table will format automatically new lines and data types, for now without trying to parse strings that could be converted to another type.

The Table class offers a variety of options that allow things like showing the index of each row, changing the style of the table, hiding the headers, etc.

See style examples here.

Code Example

new_table = Table()
new_table.add_column('Name', ['Jade', 'John'])
new_table.add_column('Age', [20, 30])
new_table.add_column('Test\nResults', [9.651, 3, 245.7])
new_table.add_row(['Piotr\nBaltimore', 27, 3.5])
new_table.add_row(['Sam', 21])
new_table.show_index = True
new_table.style_name = 'pretty_columns'
new_table.missing_value = '?'
print(new_table)

Output

╒═══╤═══════════╤═════╤═════════╕
│ i │ Name      │ Age │    Test │
│   │           │     │ Results │
╞═══╪═══════════╪═════╪═════════╡
│ 0 │ Jade      │  20 │   9.651 │
│ 1 │ John      │  30 │   3     │
│ 2 │ ?         │   ? │ 245.7   │
│ 3 │ Piotr     │  27 │   3.5   │
│   │ Baltimore │     │         │
│ 4 │ Sam       │  21 │       ? │
╘═══╧═══════════╧═════╧═════════╛

The missing value aligns as if it was of the same type of the other data in the column.

This shows how to get the row and column count. If the index is shown, this count remains unaffected by that column, although, you can get the internal count.

Code Example

new_table.show_index = False
new_table.show_headers = False
print(new_table)
print('row:', new_table.row_count)
print('columns:', new_table.column_count)
new_table.show_index = True
print(new_table)
print('internal_row_count:', new_table.internal_row_count)
print('internal_column_count:', new_table.internal_column_count)

Output

╒═══════════╤════╤═════════╕
│ Jade      │ 20 │   9.651 │
│ John      │ 30 │   3     │
│ ?         │  ? │ 245.7   │
│ Piotr     │ 27 │   3.5   │
│ Baltimore │    │         │
│ Sam       │ 21 │       ? │
╘═══════════╧════╧═════════╛
row: 5
columns: 3
╒═══╤═══════════╤════╤═════════╕
│ 0 │ Jade      │ 20 │   9.651 │
│ 1 │ John      │ 30 │   3     │
│ 2 │ ?         │  ? │ 245.7   │
│ 3 │ Piotr     │ 27 │   3.5   │
│   │ Baltimore │    │         │
│ 4 │ Sam       │ 21 │       ? │
╘═══╧═══════════╧════╧═════════╛
internal_row_count: 5
internal_column_count: 4

Colour

Colour the header, the borders, whole columns, whole rows, or individual cells. Specs accept names, attributes, 256-colour indexes and hex triples.

table.header_color = 'bold cyan'
table.border_color = 'grey'
table.column_colors = {'Service': 'bright_white'}
table.color_rule = lambda value, row, column: (
    'red' if column == 'Delta' and value < 0 else None
)

color_rule receives the original value, so numeric comparisons work directly.

Colour is emitted only to a terminal by default, and never when NO_COLOR is set. FORCE_COLOR or table.use_colors = True overrides that. Because compose() returns a string you may send anywhere, the default errs toward not embedding escape sequences in something bound for a file.

Widths are measured in terminal columns, not characters, so coloured cells and CJK or emoji data stay aligned.

Reading and writing

Table.from_csv('sales.csv')          # numeric columns parsed and aligned
Table.from_html(markup)              # standard library parser, no dependency
Table.from_dicts(records)
Table.from_pandas(dataframe)         # pip install flexablelib[pandas]
Table.from_excel('report.xlsx')      # pip install flexablelib[excel]

table.to_csv('out.csv')
table.to_markdown()
table.to_html('report.html', paginate=25)
table.to_excel('report.xlsx')

to_html writes one self-contained file — the stylesheet and the sorting, filtering and pagination script are inline — so it works offline and from a file:// URL.

Text formats deliver everything as strings, and a column of strings is left-aligned. parse_str_numbers converts numeric-looking text so it aligns as numbers; the readers turn it on for you:

table.parse_str_numbers = True

Values with leading zeros are left alone, so '007' stays a string.

Merging cells

Render a rectangular block as a single cell, across columns, down rows, or both. Coordinates are inclusive, zero-based, and ignore the index column.

table.merge_cells(0, 0, last_column=2, value='Quarter summary')
table.merge_cells(1, 0, last_row=3)                # keeps the top-left content
table.merge_cells(0, 0, 2, 2, value='Total', align='r')

A merge never widens the table -- columns are still sized by their unmerged content -- so text longer than its span is truncated.

Performance

An optional C extension accelerates text measurement, and the render pipeline avoids the second measuring pass when the table already fits. On identical data:

Library 100x4 2000x4 10000x6
flexablelib 1.0 ms 16.9 ms 147 ms
prettytable 1.4 ms 23.2 ms 174 ms
tabulate 1.7 ms 27.5 ms 193 ms
pandas .to_string() 1.7 ms 22.9 ms 228 ms

Reproduce with python3 tools/benchmark.py. This measures one job: turning data already in memory into formatted text. pandas is an analysis engine and to_string is a convenience within it, so this says nothing about groupby or joins.

The extension is built automatically where a compiler is available and falls back to pure Python where it is not, so installation never fails for want of a toolchain.

from flexablelib.fast import implementation
print(implementation())   # 'C extension' or 'pure Python'

Known Issues

None of the three historical known issues remain:

  • A column named "i" no longer collides with the index column (utils.IndexColumnTitle).
  • Exponential numbers align on the decimal / exponent axis with the rest of the float column.
  • Shrinking a float column drops decimals instead of chopping characters, so decimal alignment survives a terminal fit (#23).

Open work and the full issue map live in docs/ISSUES.md.

Project Layout

prettyTables/                 # GitHub repository (name unchanged)
├── flexablelib/            the Python package (no runtime dependencies)
│   ├── __init__.py          public API: Table, TableComposition, SeparatorLine
│   ├── table.py             the Table class — state and render orchestration
│   ├── columns.py           type inference, alignment, column widths
│   ├── style_compositions.py   the 42 border styles, as data
│   ├── table_strings.py     separator lines and data rows
│   ├── cells.py             cell padding, justification, wrapping
│   ├── options.py           constants and defaults
│   └── utils.py             type predicates and small helpers
├── tests/                   pytest suite — see ARCHITECTURE.md#testing
├── docs/                    issue history and design notes
├── logos/                   brand assets — see logos/README.md
├── ARCHITECTURE.md          how it all fits together
├── style_examples.md        all 42 styles rendered
├── package.json             canonical version number; drives the release
└── setup.py                 reads the version from package.json

Run the tests from the repository root:

pip install -r requirements-dev.txt
python -m pytest

See ARCHITECTURE.md for the rendering pipeline, how styles are defined, and how to add one.

Metadata

Release files for flexablelib 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flexablelib 2.0.0
File Size Uploaded Provenance Actions
flexablelib-2.0.0.tar.gz 129.0 kB Without provenance Details for flexablelib-2.0.0.tar.gz

Release files / flexablelib-2.0.0.tar.gz

Download URL flexablelib-2.0.0.tar.gz
Size 129.0 kB
Tags Source
SHA-256 checksum
How to use checksums
347b301c97083207e9a71237d2d35e04dab953da2c3496ccdc40bef0b89e578a
BLAKE2b-256 checksum
How to use checksums
902b660ed3b832199df7050f2a8747c7c2e1023e1d018d132b7e98d47abdd94f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for flexablelib 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page