Convert 2D Python lists into Unicode/Ascii tables
Project description
table2ascii
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, 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.
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
Terminal outputs
- Tables display nicely whenever monospace fonts are fully supported
- Tables make terminal outputs look more professional
๐งฐ Development
Running tests
-
Install
tox
with the commandpip install -U tox
-
Run tests with the command
tox
Linting
Run the following command to lint with flake8
python setup.py lint
(Note: The exact command may vary depending on your Python version and environment)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file table2ascii-0.1.3.tar.gz
.
File metadata
- Download URL: table2ascii-0.1.3.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77398c10b223b57e53c272fc78f3e0488dbe22f29cbf30e32db7576294aecf42 |
|
MD5 | 85a963abf9df331d77c2d4c5332c6d84 |
|
BLAKE2b-256 | b8017b7d4b86b6587efdd510bf6ca7b55bc3c51ef1df8594a55644cfb76862f3 |
File details
Details for the file table2ascii-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: table2ascii-0.1.3-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e46f3132ce8bb10d4404eb7b974eca1e95ba2f9e0262f9261ca536ccb386738a |
|
MD5 | 482d1a7c0277b1a64c5b57da27f0d2e8 |
|
BLAKE2b-256 | ae113eaac64397f0cc5ee2c60f9d070e11ed278b08f5121eca75078f9b9a0c6f |