Skip to content

Latest commit

 

History

History
205 lines (151 loc) · 7.8 KB

README.md

File metadata and controls

205 lines (151 loc) · 7.8 KB

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

⚙️ Options

All parameters are optional.

Option Type Default Description
header List[Any] None First table row seperated by header row separator. Values should support str()
body List[List[Any]] None List of rows for the main section of the table. Values should support str()
footer List[Any] None Last table row seperated by header row separator. Values should support str()
column_widths List[Optional[int]] None (automatic) List of column widths in characters for each column
alignments List[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.