python-tabulate
python-tabulate copied to clipboard
[feature request] customize header padding
It appears that tabulate wants at least 3 spaces between a column delimiter and the header text (in the opposite direction indicated by colalign):
print(tabulate([["First name", "Last name"], ["John", "Doe"]],
headers="firstrow", tablefmt="rounded_outline", colalign=("left", "right")))
╭──────────────┬─────────────╮
│ First name │ Last name │
├──────────────┼─────────────┤
│ John │ Doe │
╰──────────────┴─────────────╯
I wish I could customize this, particularly for tables with many columns, and allow, say 1 space of padding instead of 3:
print(tabulate([["First name", "Last name"], ["John", "Doe"]],
headers="firstrow", tablefmt="rounded_outline", colalign=("left", "right"),
min_header_padding=1))
╭────────────┬───────────╮
│ First name │ Last name │
├────────────┼───────────┤
│ John │ Doe │
╰────────────┴───────────╯
It would make sense even to allow this on both sides of the header entry:
print(tabulate([["First name", "Last name"], ["John", "Doe"]],
headers="firstrow", tablefmt="rounded_outline", colalign=("left", "right"),
min_header_left_padding=0, min_header_right_padding=0))
╭──────────┬─────────╮
│First name│Last name│
├──────────┼─────────┤
│ John │ Doe │
╰──────────┴─────────╯
A workaround appears to be to set the value tabulate.MIN_PADDING, whose default value is 2:
import tabulate
table = [
["First name", "Last name"],
["John", "Doe"],
]
fmt = "rounded_outline"
colalign = ("left", "right")
for padding in [2,1,0]:
tabulate.MIN_PADDING = padding
print(tabulate.tabulate(table, tablefmt=fmt, colalign=colalign, headers="firstrow"))
This prints
╭──────────────┬─────────────╮
│ First name │ Last name │
├──────────────┼─────────────┤
│ John │ Doe │
╰──────────────┴─────────────╯
╭─────────────┬────────────╮
│ First name │ Last name │
├─────────────┼────────────┤
│ John │ Doe │
╰─────────────┴────────────╯
╭────────────┬───────────╮
│ First name │ Last name │
├────────────┼───────────┤
│ John │ Doe │
╰────────────┴───────────╯
It's a clunky matplotlib-like global-variable-as-function-parameter, but it works.