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 thestyle
class imported before (withstyle.fill()
,style.text()
andstyle.borders()
)locations
: as forstyle
, it's better to use it with theloc
class imported above withloc.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