Skip to main content

Convert 2D Python lists into Unicode/Ascii tables

Project description

table2ascii

build version downloads license discord

An intuitive and type-safe library for converting 2D Python lists to fancy ASCII/Unicode tables

Documentation and examples are available at table2ascii.rtfd.io

๐Ÿ“ฅ Installation

pip install -U table2ascii

Requirements: Python 3.7+

๐Ÿง‘โ€๐Ÿ’ป Usage

๐Ÿš€ Convert lists to ASCII tables

from table2ascii import table2ascii

output = table2ascii(
    header=["#", "G", "H", "R", "S"],
    body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
    footer=["SUM", "130", "140", "135", "130"],
)

print(output)

"""
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘  #     G     H     R     S  โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘  1    30    40    35    30  โ•‘
โ•‘  2    30    40    35    30  โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ SUM   130   140   135   130 โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
"""

๐Ÿ† Set first or last column headings

from table2ascii import table2ascii

output = table2ascii(
    body=[["Assignment", "30", "40", "35", "30"], ["Bonus", "10", "20", "5", "10"]],
    first_col_heading=True,
)

print(output)

"""
โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ Assignment โ•‘ 30   40   35   30 โ•‘
โ•‘    Bonus   โ•‘ 10   20    5   10 โ•‘
โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
"""

๐Ÿ“ฐ Set column widths and alignments

from table2ascii import table2ascii, Alignment

output = table2ascii(
    header=["#", "G", "H", "R", "S"],
    body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
    first_col_heading=True,
    column_widths=[5, 5, 5, 5, 5],
    alignments=[Alignment.LEFT] + [Alignment.RIGHT] * 4,
)

print(output)

"""
โ•”โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—
โ•‘ #   โ•‘   G     H     R     S โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ•ซโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘ 1   โ•‘  30    40    35    30 โ•‘
โ•‘ 2   โ•‘  30    40    35    30 โ•‘
โ•šโ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
"""

๐ŸŽจ Use a preset style

See a list of 30+ preset styles here.

from table2ascii import table2ascii, Alignment, PresetStyle

output = table2ascii(
    header=["First", "Second", "Third", "Fourth"],
    body=[["10", "30", "40", "35"], ["20", "10", "20", "5"]],
    column_widths=[10, 10, 10, 10],
    style=PresetStyle.ascii_box
)

print(output)

"""
+----------+----------+----------+----------+
|  First   |  Second  |  Third   |  Fourth  |
+----------+----------+----------+----------+
|    10    |    30    |    40    |    35    |
+----------+----------+----------+----------+
|    20    |    10    |    20    |    5     |
+----------+----------+----------+----------+
"""

output = table2ascii(
    header=["First", "Second", "Third", "Fourth"],
    body=[["10", "30", "40", "35"], ["20", "10", "20", "5"]],
    style=PresetStyle.plain,
    cell_padding=0,
    alignments=[Alignment.LEFT] * 4,
)

print(output)

"""
First Second Third Fourth
10    30     40    35
20    10     20    5
"""

๐ŸŽฒ Define a custom style

Check TableStyle for more info and PresetStyle for examples.

from table2ascii import table2ascii, TableStyle

my_style = TableStyle.from_string("*-..*||:+-+:+     *''*")

output = table2ascii(
    header=["First", "Second", "Third"],
    body=[["10", "30", "40"], ["20", "10", "20"], ["30", "20", "30"]],
    style=my_style
)

print(output)

"""
*-------.--------.-------*
| First : Second : Third |
+-------:--------:-------+
|  10   :   30   :  40   |
|  20   :   10   :  20   |
|  30   :   20   :  30   |
*-------'--------'-------*
"""

๐Ÿช„ Merge adjacent cells

from table2ascii import table2ascii, Merge, PresetStyle

output = table2ascii(
    header=["#", "G", "Merge", Merge.LEFT, "S"],
    body=[
        [1, 5, 6, 200, Merge.LEFT],
        [2, "E", "Long cell", Merge.LEFT, Merge.LEFT],
        ["Bonus", Merge.LEFT, Merge.LEFT, "F", "G"],
    ],
    footer=["SUM", "100", "200", Merge.LEFT, "300"],
    style=PresetStyle.double_thin_box,
    first_col_heading=True,
)

print(output)

"""
โ•”โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•—
โ•‘  #  โ•‘  G  โ”‚ Merge โ”‚  S  โ•‘
โ• โ•โ•โ•โ•โ•โ•ฌโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•คโ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•ฃ
โ•‘  1  โ•‘  5  โ”‚ 6 โ”‚   200   โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ•ซโ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘  2  โ•‘  E  โ”‚  Long cell  โ•‘
โ•Ÿโ”€โ”€โ”€โ”€โ”€โ•จโ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ•ข
โ•‘     Bonus     โ”‚ F โ”‚  G  โ•‘
โ• โ•โ•โ•โ•โ•โ•ฆโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•งโ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ฃ
โ•‘ SUM โ•‘ 100 โ”‚  200  โ”‚ 300 โ•‘
โ•šโ•โ•โ•โ•โ•โ•ฉโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•
"""

โš™๏ธ Options

All parameters are optional. At least one of header, body, and footer must be provided.

Refer to the documentation for more information.

Option Type Default Description
header Sequence[SupportsStr] None First table row seperated by header row separator. Values should support str()
body Sequence[Sequence[Sequence]] None 2D List of rows for the main section of the table. Values should support str()
footer Sequence[Sequence] None Last table row seperated by header row separator. Values should support str()
column_widths Sequence[Optional[int]] None (automatic) List of column widths in characters for each column
alignments Sequence[Alignment] None (all centered) Column alignments
(ex. [Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT])
style TableStyle double_thin_compact Table style to use for the table*
first_col_heading bool False Whether to add a heading column separator after the first column
last_col_heading bool False Whether to add a heading column separator before the last column
cell_padding int 1 The minimum number of spaces to add between the cell content and the cell border
use_wcwidth bool True Whether to use wcwidth instead of len() to calculate cell width

*See a list of all preset styles here.

See the API Reference for more info.

๐Ÿ‘จโ€๐ŸŽจ Use cases

๐Ÿ—จ๏ธ Discord messages and embeds

  • Display tables nicely inside markdown code blocks on Discord
  • Useful for making Discord bots with Discord.py

image

๐Ÿ’ป Terminal outputs

  • Tables display nicely whenever monospace fonts are fully supported
  • Tables make terminal outputs look more professional

image

๐Ÿค— Contributing

Contributions are welcome!

See CONTRIBUTING.md for more details on how to get involved.

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

table2ascii-1.0.1.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

table2ascii-1.0.1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file table2ascii-1.0.1.tar.gz.

File metadata

  • Download URL: table2ascii-1.0.1.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.8

File hashes

Hashes for table2ascii-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0947e844a61097e887e723ccd22bca8bed232819682c45c51b96ed1e822caf94
MD5 021de3b6cfe5d2a8e217d46b39425572
BLAKE2b-256 177fb6719e31c618737cd4a2710e48f3de460da4bbaeaefeff76315daa3bc255

See more details on using hashes here.

File details

Details for the file table2ascii-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: table2ascii-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.10.8

File hashes

Hashes for table2ascii-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1a1b54ce7cc8c2079ffc2cf2c053d33213089968e11a8c737b15e6086f940bfe
MD5 5b08401e4124b4b98a399f8fff4762c8
BLAKE2b-256 0847695175b33d4d9ba1db451bf3f9d0d8e9daa0ff5b7541edba0587ab25cd21

See more details on using hashes here.

Supported by

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