Python tabulate

Testing tables generated with the Python tabulate package.

Quarto example

Taken from the Quarto tables documentation, the table below uses the Python tabulate package with the IPython display module’s Markdown() function to print a markdown table.

View code
from IPython.display import Markdown
from tabulate import tabulate
table = [["Sun",696000,1989100000],
         ["Earth",6371,5973.6],
         ["Moon",1737,73.5],
         ["Mars",3390,641.85]]
Markdown(tabulate(
  table, 
  headers=["Planet","R (km)", "mass (x 10^29 kg)"]
))
Planets
Planet R (km) mass (x 10^29 kg)
Sun 696000 1.9891e+09
Earth 6371 5973.6
Moon 1737 73.5
Mars 3390 641.85

Table formats

You can use other table formats from tabulate in combination with Markdown(), as long as they’re supported by Quarto’s rendering engine. For example, below is the output from tabulate() with used tablefmt="grid".

View code
table = [["spam",42],["eggs",451],["bacon",0]]
headers = ["item", "qty"]
print(tabulate(table, headers, tablefmt="grid"))
+--------+-------+
| item   |   qty |
+========+=======+
| spam   |    42 |
+--------+-------+
| eggs   |   451 |
+--------+-------+
| bacon  |     0 |
+--------+-------+

When wrapped in Markdown() instead of print(), this renders as a nice HTML table, since Quarto supports grid tables.

View code
Markdown(tabulate(table, headers, tablefmt="grid"))
item qty
spam 42
eggs 451
bacon
0