Skip to main content

Convert 2D Python lists into Unicode/Ascii tables

Project description

table2ascii

build version license Discord

Module for converting 2D Python lists to a fancy ASCII/Unicode tables

๐Ÿ“ฅ Installation

pip install table2ascii

๐Ÿง‘โ€๐Ÿ’ป 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, 5, 5]
    alignments=[Alignment.LEFT] + [Alignment.RIGHT] * 4, # First is left, remaining 4 are right
)

print(output)

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

Use a preset style

from table2ascii import table2ascii, PresetStyle

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

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   |
*-------'--------'-------*
"""

๐ŸŽจ Preset styles

See a list of all preset styles here.

โš™๏ธ Options

All parameters are optional.

Soon table2ascii will support more options for customization.

Option Type Default Description
header List[str] None First row of table seperated by header row seperator
body List[List[str]] None List of rows for the main section of the table
footer List[str] None Last row of table seperated by header row seperator
column_widths List[int] automatic List of column widths in characters for each column
alignments List[int] all centered Alignments for each column
(ex. [Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT])
first_col_heading bool False Whether to add a heading column seperator after the first column
last_col_heading bool False Whether to add a heading column seperator before the last column

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

Discord messages and embeds

  • Display tables nicely inside markdown codeblocks 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

๐Ÿงฐ Development

To run tests (pytest)

python setup.py test

To lint (flake8):

python setup.py lint

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-0.1.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

table2ascii-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: table2ascii-0.1.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9

File hashes

Hashes for table2ascii-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d9325ab9a39ab19aa2abe40b1fe330c672757be60b611011eb0dbf519db320aa
MD5 371212a7a69d18f3eba44cd808152bcd
BLAKE2b-256 8ea481e74d810d59d5ab771245f069ba61e9fbe34ca50d6d4afb7d6eaecc1430

See more details on using hashes here.

File details

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

File metadata

  • Download URL: table2ascii-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9

File hashes

Hashes for table2ascii-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98fa57381df6b70e6629bf7d7ab945018bd4f98a96751d178753a02adb43078e
MD5 59337b34e59846a89a331833490f33e8
BLAKE2b-256 b891786cf663bead0d4d1815b2e99ae9865e988f12c4b67ef17e0e81711a747e

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