python-tabulate
python-tabulate copied to clipboard
New feature: provide a means of obtaining table geometry.
- g.w: table width with frame elements,
- g.h: table height with frame elements,
- g.c_w: width of each column without frame elements,
- g.r_h: height of each row without frame elements,
- g.f_w: width of horizontal frame elements,
- g.f_h: the height of vertical frame elements.
Example:
>>> print(tabulate(table, headers, tablefmt="simple"))
item qty
------ ----- )1
spam 42
eggs 451
bacon 0
╰╯
2
>>> g = geometry(table, headers, tablefmt="simple"))
>>> g.w
13
>>> g.h
5
>>> g.c_w
(6, 5)
>>> g.r_h
(1, 1, 1, 1)
>>> g.f_w
(0, 2, 0)
>>> g.f_h
(0, 1, 0, 0, 0)
>>> print(tabulate(table, headers, tablefmt="grid"))
+--------+-------+ )1
| item | qty |
+========+=======+ )1
| spam | 42 |
+--------+-------+ )1
| eggs | 451 |
+--------+-------+ )1
| bacon | 0 |
+--------+-------+ )1
╰╯ ╰─╯ ╰╯
2 3 2
>>> g = geometry(table, headers, tablefmt="grid"))
>>> g.w
18
>>> g.h
9
>>> g.c_w
(6, 5)
>>> g.r_h
(1, 1, 1, 1)
>>> g.f_w
(2, 3, 2)
>>> g.f_h
(1, 1, 1, 1, 1)
Thanks to the information provided, I'm able to configure the table generation correctly, for example, so that it doesn't exceed a maximum width...
Thank you for your work.
I'm not sure if that is what you're looking for, but the information about table format is already structured like this:
TableFormat(
lineabove=Line("+", "-", "+", "+"),
linebelowheader=Line("+", "=", "+", "+"),
linebetweenrows=Line("+", "-", "+", "+"),
linebelow=Line("+", "-", "+", "+"),
headerrow=DataRow("|", "|", "|"),
datarow=DataRow("|", "|", "|"),
padding=1,
with_header_hide=None,
)
From the length of these strings and padding value you can calculate all your geometric parameters. Other formats:
https://github.com/astanin/python-tabulate/blob/master/tabulate/init.py#L338
See also the comment https://github.com/astanin/python-tabulate/blob/master/tabulate/init.py#L57