Skip to content

Commit c5f3b57

Browse files
author
Philipp Bogensberger
committed
- fix rendering tables with None or empty table data
- added constant for MIN_PADDING so it can be overwritten from outside
1 parent 4ac8557 commit c5f3b57

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

tabulate.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
__version__ = "0.7.3"
3232

3333

34+
MIN_PADDING = 2
35+
36+
3437
Line = namedtuple("Line", ["begin", "hline", "sep", "end"])
3538

3639

@@ -764,7 +767,8 @@ def tabulate(tabular_data, headers=[], tablefmt="simple",
764767
\\end{tabular}
765768
766769
"""
767-
770+
if tabular_data is None:
771+
tabular_data = []
768772
list_of_lists, headers = _normalize_tabular_data(tabular_data, headers)
769773

770774
# optimization: look for ANSI control codes once,
@@ -785,15 +789,17 @@ def tabulate(tabular_data, headers=[], tablefmt="simple",
785789

786790
# align columns
787791
aligns = [numalign if ct in [int,float] else stralign for ct in coltypes]
788-
minwidths = [width_fn(h)+2 for h in headers] if headers else [0]*len(cols)
792+
minwidths = [width_fn(h) + MIN_PADDING for h in headers] if headers else [0]*len(cols)
789793
cols = [_align_column(c, a, minw, has_invisible)
790794
for c, a, minw in zip(cols, aligns, minwidths)]
791795

792796
if headers:
793797
# align headers and add headers
794-
minwidths = [max(minw, width_fn(c[0])) for minw, c in zip(minwidths, cols)]
798+
t_cols = cols or [['']] * len(headers)
799+
t_aligns = aligns or [stralign] * len(headers)
800+
minwidths = [max(minw, width_fn(c[0])) for minw, c in zip(minwidths, t_cols)]
795801
headers = [_align_header(h, a, minw)
796-
for h, a, minw in zip(headers, aligns, minwidths)]
802+
for h, a, minw in zip(headers, t_aligns, minwidths)]
797803
rows = list(zip(*cols))
798804
else:
799805
minwidths = [width_fn(c[0]) for c in cols]

test/test_output.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,36 @@ def test_latex_headerless():
189189
assert_equal(expected, result)
190190

191191

192+
def test_no_data():
193+
"Output: table with no data"
194+
expected = "\n".join(['strings numbers',
195+
'--------- ---------'])
196+
result = tabulate(None, _test_table_headers, tablefmt="simple")
197+
assert_equal(expected, result)
198+
199+
200+
def test_empty_data():
201+
"Output: table with empty data"
202+
expected = "\n".join(['strings numbers',
203+
'--------- ---------'])
204+
result = tabulate([], _test_table_headers, tablefmt="simple")
205+
assert_equal(expected, result)
206+
207+
208+
def test_no_data_without_headers():
209+
"Output: table with no data and no headers"
210+
expected = "\n"
211+
result = tabulate(None, tablefmt="simple")
212+
assert_equal(expected, result)
213+
214+
215+
def test_empty_data_without_headers():
216+
"Output: table with empty data and no headers"
217+
expected = "\n"
218+
result = tabulate([], tablefmt="simple")
219+
assert_equal(expected, result)
220+
221+
192222
def test_floatfmt():
193223
"Output: floating point format"
194224
result = tabulate([['1.23456789'],[1.0]], floatfmt=".3f", tablefmt="plain")

0 commit comments

Comments
 (0)