Easy text table generation with Python. Supports sparse tables
At this point, just copy tabler
folder to the folder with your script or anywhere in the python path.
Pypi and setup.py would be available later.
from tabler import Table
table = Table.from_list([
["Number", "Square"],
*[[x, x ** 2] for x in range(2, 6)]
])
print(table)
result = """
┏━━━━━━┳━━━━━━┓
┃Number┃Square┃
┣━━━━━━╋━━━━━━┫
┃ 2 ┃ 4 ┃
┣━━━━━━╋━━━━━━┫
┃ 3 ┃ 9 ┃
┣━━━━━━╋━━━━━━┫
┃ 4 ┃ 16 ┃
┣━━━━━━╋━━━━━━┫
┃ 5 ┃ 25 ┃
┗━━━━━━┻━━━━━━┛
"""
You can omit values:
from tabler import Table
table = Table.from_list([
[i] * i
for i in range(5)
])
print(table)
result = """
┏━┳━┳━┳━┓
┃ ┃ ┃ ┃ ┃
┣━╋━╋━╋━┫
┃1┃ ┃ ┃ ┃
┣━╋━╋━╋━┫
┃2┃2┃ ┃ ┃
┣━╋━╋━╋━┫
┃3┃3┃3┃ ┃
┣━╋━╋━╋━┫
┃4┃4┃4┃4┃
┗━┻━┻━┻━┛
"""
Or fill in specific cells:
from tabler import Table
table = Table()
table[0, 0] = "Here"
table[2, 2] = "And Here"
print(table)
result = """
┏━━━━┳┳━━━━━━━━┓
┃Here┃┃ ┃
┣━━━━╋╋━━━━━━━━┫
┃ ┃┃ ┃
┣━━━━╋╋━━━━━━━━┫
┃ ┃┃And Here┃
┗━━━━┻┻━━━━━━━━┛
"""
Previous example didn't look nice because of the empty column, let's widen it a bit:
from tabler import Table
table = Table()
table[0, 0] = "Here"
table[2, 2] = "And Here"
print(table.to_string(even_columns=True))
result = """
┏━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓
┃ Here ┃ ┃ ┃
┣━━━━━━━━╋━━━━━━━━╋━━━━━━━━┫
┃ ┃ ┃ ┃
┣━━━━━━━━╋━━━━━━━━╋━━━━━━━━┫
┃ ┃ ┃And Here┃
┗━━━━━━━━┻━━━━━━━━┻━━━━━━━━┛
"""
Same goes for even_rows
parameter, which aligns rows the same way
If you want the table to be stretched, you can use stretch_to
parameter
from tabler import Table
table = Table()
table[0, 0] = "Here"
table[2, 2] = "And Here"
print(
table.to_string(
even_columns=True,
stretch_to=(37, 25)
)
)
result = """
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ Here ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┣━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━━━━┫
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┣━━━━━━━━━━━╋━━━━━━━━━━━╋━━━━━━━━━━━┫
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ And Here ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┃ ┃ ┃ ┃
┗━━━━━━━━━━━┻━━━━━━━━━━━┻━━━━━━━━━━━┛
"""
You can retrieve a slice of the table like this:
from tabler import Table
table = Table()
table[0, 0] = "Here"
table[2, 2] = "And Here"
print(
table[(0, 0):(2, 2)]
)
result = """
┏━━━━┓
┃Here┃
┗━━━━┛
"""
If you need to use other characters as borders, just change table.tablechars
:
from tabler import Table
table = Table()
table[0, 0] = "Here"
table[1, 0] = "There"
print(
table
)
result = """
┏━━━━┳━━━━━┓
┃Here┃There┃
┗━━━━┻━━━━━┛
"""
table.tablechars = " " * 11 # using spaces as separators
print(
table
)
result = """
Here There
"""
Also, there are predefined sets of chars:
from tabler import tablechars
print(tablechars)
"""
{
"default": "┏┓┗┛┃━┣┫┳┻╋",
"double": "╔╗╚╝║═╠╣╦╩╬",
"dots": "···· ·····",
"simple": " |- ",
"dots2": "····|-·····",
"empty": " " * 11,
}
"""