Module for converting 2D Python lists to a fancy ASCII/Unicode tables
pip install table2ascii
Convert Python 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 ║
╚═════════════════════════════╝
"""
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 ║
╚════════════╩═══════════════════╝
"""
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 ║
╚═════╩═══════════════════════╝
"""
from table2ascii import table2ascii, Styles
output = table2ascii(
header=["#", "G", "H", "R", "S"],
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
footer=["SUM", "130", "140", "135", "130"],
style=Styles.ascii_box,
)
print(output)
"""
+-----+-----+-----+-----+-----+
| # | G | H | R | S |
+-----+-----+-----+-----+-----+
| 1 | 30 | 40 | 35 | 30 |
+-----+-----+-----+-----+-----+
| 2 | 30 | 40 | 35 | 30 |
+-----+-----+-----+-----+-----+
| SUM | 130 | 140 | 135 | 130 |
+-----+-----+-----+-----+-----+
"""
See a list of all preset styles here.
All parameters are optional.
Soon table2ascii will support more options for customization.
Option | Type | Default | Description |
---|---|---|---|
header |
List |
None |
First row of table seperated by header row seperator |
body |
2D List |
None |
List of rows for the main section of the table |
footer |
List |
None |
Last row of table seperated by header row seperator |
column_widths |
List |
automatic | List of column widths in characters for each column |
alignments |
List |
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 |
- Display tables nicely inside markdown codeblocks on Discord
- Useful for making Discord bots with Discord.py
- Tables display nicely whenever monospace fonts are fully supported
- Tables make terminal outputs look more professional
To run tests (pytest)
python setup.py test
To lint (flake8):
python setup.py lint