Great Tables v0.12.0: Google Fonts and zebra stripes
Author
Rich Iannone
Published
September 30, 2024
In Great Tables 0.12.0 we focused on adding options for customizing the appearance of a table. In this post, we’ll present two new features:
using typefaces from Google Fonts via tab_style() and opt_table_font()
adding table striping via tab_options() and opt_row_striping()
Let’s have a look at how these new features can be used!
Using fonts from Google Fonts
Google Fonts is a free service that allows use of hosted typefaces in your own websites. In Great Tables, we added the google_font() helper function to easily incorporate such fonts in your tables. There are two ways to go about this:
use google_font() with opt_table_font() to set a Google Font for the entire table
invoke google_font() within tab_style(styles=style.text(font=...)) to set the font within a location
Let’s start with this small table that uses the default set of fonts for the entire table.
Show the code
from great_tables import GT, exibble, style, locgt_tbl = ( GT(exibble.head(), rowname_col="row", groupname_col="group") .cols_hide(columns=["char", "fctr", "date", "time"]) .tab_header( title="A small piece of the exibble dataset", subtitle="Displaying the first five rows (of eight)", ) .tab_source_note( source_note="This dataset is included in Great Tables." ))gt_tbl
A small piece of the exibble dataset
Displaying the first five rows (of eight)
num
datetime
currency
grp_a
row_1
0.1111
2018-01-01 02:22
49.95
row_2
2.222
2018-02-02 14:33
17.95
row_3
33.33
2018-03-03 03:44
1.39
row_4
444.4
2018-04-04 15:55
65100.0
grp_b
row_5
5550.0
2018-05-05 04:00
1325.81
This dataset is included in Great Tables.
Now, with opt_table_font() + google_font(), we’ll change the table’s font to one from Google Fonts. I like Noto Serif so let’s use that here!
Looking good! And we don’t have to apply the font to the entire table. We might just wanted to use a Google Font in the table body. For that use case, tab_style() is the preferred method. Here’s an example that uses the IBM Plex Mono typeface.
Nice! And it’s refreshing to see tables with fonts different from default set, as good as it might be. We kept the google_font() helper function as simple as possible, requiring only the font name in its name= argument. There are hundreds of fonts hosted on Google Fonts so look through the site, experiment, and find the fonts that you think look best in your tables!
Striping rows in your table
Some people like having row striping (a.k.a. zebra stripes) in their display tables. We also know that some advise against the practice. We understand it’s a controversial table issue, however, we also want to give you the creative freedom to just include the stripes. To that end, we now have that option in the package. There are two ways to enable this look:
invoking opt_row_striping() to quickly set row stripes in the table body
using some combination of three row_striping_* arguments in tab_options()
Let’s use that example table with opt_row_striping().
gt_tbl.opt_row_striping()
A small piece of the exibble dataset
Displaying the first five rows (of eight)
num
datetime
currency
grp_a
row_1
0.1111
2018-01-01 02:22
49.95
row_2
2.222
2018-02-02 14:33
17.95
row_3
33.33
2018-03-03 03:44
1.39
row_4
444.4
2018-04-04 15:55
65100.0
grp_b
row_5
5550.0
2018-05-05 04:00
1325.81
This dataset is included in Great Tables.
It’s somewhat subtle but there is an alternating, slightly gray background (starting on the "row_2" row). The color is #808080 but with an alpha (transparency) value of 0.05.
If this is not exactly what you want, there is an alternative to this. The tab_options() method has three new arguments:
row_striping_background_color: color to use for row striping
row_striping_include_stub: should striping include cells in the stub?
row_striping_include_table_body: should striping include cells in the body?
With these new options, we can choose to stripe the entire row (stub cells + body cells) and use a darker color like "lightblue".
These alternating fills can be a good idea in some table display circumstances. Now, you can make that call and the functionality is there to support your decision.
Wrapping up
We are excited that this new functionality is now available in Great Tables. As ever, please let us know through GitHub Issues whether you ran into problems with any feature (new or old), or, if you have suggestions for further improvement!