Customization of table with the great_tables library in Python

logo of a chart:TableBig

This post delves more specific usage of the great_tables library in Python. It covers advanced customization of tables using the great_tables library.

For an introduction to great_tables, check this post

Libraries & Dataset

For creating this chart, we will only need to load great_tables!

You can install it via the pip install great_tables command.

Also, since great_tables provides a few datasets, we're going to use them directly.

from great_tables import GT, md, style, loc

# load dataset about pizza
from great_tables.data import pizzaplace as df
df = df[['name', 'size', 'type', 'price']].tail()
df.index = range(5)
df
name size type price
0 four_cheese L veggie 17.95
1 napolitana S classic 12.00
2 ckn_alfredo M chicken 16.75
3 mexicana L veggie 20.25
4 bbq_ckn S chicken 12.75

Simple table with great_tables

Let's start with a simple table with a header and a footer:

my_table = (
   GT(df)
   .tab_header(
      title="🍕 Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
   .tab_source_note(md("**Data source**: the `great_tables` python library"))
   .tab_source_note(md("**Tutorial**: the *[Python Graph Gallery](https://python-graph-gallery.com)*"))
)
my_table
🍕 Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
name size type price
four_cheese L veggie 17.95
napolitana S classic 12.0
ckn_alfredo M chicken 16.75
mexicana L veggie 20.25
bbq_ckn S chicken 12.75
Data source: the great_tables python library
Tutorial: the Python Graph Gallery

Color some row/column

We can color a specific row/col using the tab_style() function. This functions requires 2 arguments:

  • style: the best way is to use it with the style class imported before (with style.fill(), style.text() and style.borders())
  • locations: as for style, it's better to use it with the loc class imported above with loc.body()

_Note that the parenthesis around the code are only here to make the code easier to read. Without it, we would not be able to jump row when calling a tab\__() function*

my_table = (
   GT(df, rowname_col="name")
   .tab_header(
      title="Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
   .tab_source_note(md("**Data source**: the `great_tables` python library"))
   .tab_source_note(md("**Tutorial**: the *Python Graph Gallery*"))
   .tab_style(
      style=style.fill(color="#f0c580"),
      locations=loc.body(columns="price", rows=["mexicana"]),
   )
   .tab_style(
      style=style.fill(color="#c5eceb"),
      locations=loc.body(columns="size", rows=["napolitana"]),
   )
   .tab_style(
      style=style.text(weight='bold', size='20px'),
      locations=loc.body(columns="price", rows=["ckn_alfredo"]),
   )
   .tab_style(
      style=style.borders(sides=["top", "left"], weight='3px', color="red"),
      locations=loc.body(columns="type", rows=[3]),
   )
)
my_table
Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
size type price
four_cheese L veggie 17.95
napolitana S classic 12.0
ckn_alfredo M chicken 16.75
mexicana L veggie 20.25
bbq_ckn S chicken 12.75
Data source: the great_tables python library
Tutorial: the Python Graph Gallery

Footer

To add a footer, we need to use the tab_source_note() function.

We can wrap our text with the md() function to include markdown formatting such as bold, code-like, and italic styles.

my_table = (
   GT(df)
   .tab_header(
      title="🍕 Pizza 🍕",
      subtitle="A list of pizza available in the Pyzza Graph Gallery",
   )
   .tab_source_note(md("**Data source**: the `great_tables` python library"))
   .tab_source_note(md("**Tutorial**: the *Python Graph Gallery*"))
)
my_table
🍕 Pizza 🍕
A list of pizza available in the Pyzza Graph Gallery
name size type price
four_cheese L veggie 17.95
napolitana S classic 12.0
ckn_alfredo M chicken 16.75
mexicana L veggie 20.25
bbq_ckn S chicken 12.75
Data source: the great_tables python library
Tutorial: the Python Graph Gallery

Going further

This article explains how to use the great_tables library in Python.

You might be interested in the table section of the gallery to learn everything about tables in python

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!