great_tables
  • Get Started
  • Examples
  • Reference
  • Blog
  1. Style
  2. Styling the whole table
  • Intro
  • Overview
  • Table Structure
    • Header and Footer
    • Stub (Row Labels)
    • Column Labels
  • Format
    • Formatting Values
    • Nanoplots
  • Style
    • Styling the Table Body
    • Styling the whole table
    • Colorizing with Data
  • Theming
    • Table Theme Options
    • Premade Themes
  • Selecting table parts
    • Column Selection
    • Row Selection
    • Location selection
  • Extra
    • Contributing Guidelines
    • Extending Great Tables

On this page

  • Kitchen sink
  • Body
  • Column labels
  • Header
  • Footer
  • Stub
  • Stubhead
  • Grand Summary Rows
  1. Style
  2. Styling the whole table

Styling the whole table

In Styling the Table Body, we discussed styling table data with tab_style(). In this article we’ll cover how the same method can be used to style many other parts of the table, like the header, specific spanner labels, the footer, and more.

Warning

This feature is new, and this page of documentation is still in development.

Kitchen sink

Below is a big example that shows all possible loc specifiers being used.

from great_tables import GT, exibble, loc, style

# https://colorbrewer2.org/#type=qualitative&scheme=Paired&n=12 and grey
brewer_colors = [
    "#a6cee3",
    "#1f78b4",
    "#b2df8a",
    "#33a02c",
    "#fb9a99",
    "#e31a1c",
    "#fdbf6f",
    "#ff7f00",
    "#cab2d6",
    "#6a3d9a",
    "#ffff99",
    "#b15928",
    "#808080",
]

c = iter(brewer_colors)

gt = (
    GT(exibble.loc[[0, 1, 4], ["num", "char", "fctr", "row", "group"]])
    .tab_header("title", "subtitle")
    .tab_stub(rowname_col="row", groupname_col="group")
    .tab_source_note("yo")
    .tab_spanner("spanner", ["char", "fctr"])
    .tab_stubhead("stubhead")
    .grand_summary_rows(fns={"Total": lambda x: x.sum(numeric_only=True)})
)

(
    gt.tab_style(style.fill(next(c)), loc.body())
    # Columns -----------
    # TODO: appears in browser, but not vs code
    .tab_style(style.fill(next(c)), loc.column_labels(columns="num"))
    .tab_style(style.fill(next(c)), loc.column_header())
    .tab_style(style.fill(next(c)), loc.spanner_labels(ids=["spanner"]))
    # Header -----------
    .tab_style(style.fill(next(c)), loc.header())
    .tab_style(style.fill(next(c)), loc.subtitle())
    .tab_style(style.fill(next(c)), loc.title())
    # Footer -----------
    .tab_style(style.borders(weight="3px"), loc.source_notes())
    .tab_style(style.fill(next(c)), loc.footer())
    # Stub --------------
    .tab_style(style.fill(next(c)), loc.row_groups())
    .tab_style(style.borders(weight="3px"), loc.stub(rows=1))
    .tab_style(style.fill(next(c)), loc.stub())
    .tab_style(style.fill(next(c)), loc.stubhead())
    # Summary Rows --------------
    .tab_style(style.fill(next(c)), loc.grand_summary())
    .tab_style(style.fill(next(c)), loc.grand_summary_stub())
)
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Body

gt.tab_style(style.fill("yellow"), loc.body())
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Column labels

(
    gt
    .tab_style(style.fill("yellow"), loc.column_header())
    .tab_style(style.fill("blue"), loc.column_labels(columns="num"))
    .tab_style(style.fill("red"), loc.spanner_labels(ids=["spanner"]))
)
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Header

(
    gt.tab_style(style.fill("yellow"), loc.header())
    .tab_style(style.fill("blue"), loc.title())
    .tab_style(style.fill("red"), loc.subtitle())
)
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Footer

(
    gt.tab_style(
        style.fill("yellow"),
        loc.source_notes(),
    ).tab_style(
        style.borders(weight="3px"),
        loc.footer(),
    )
)
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Stub

(
    gt.tab_style(style.fill("yellow"), loc.stub())
    .tab_style(style.fill("blue"), loc.row_groups())
    .tab_style(
        style.borders(style="dashed", weight="3px", color="red"),
        loc.stub(rows=[1]),
    )
)
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Stubhead

gt.tab_style(style.fill("yellow"), loc.stubhead())
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo

Grand Summary Rows

(
    gt.tab_style(
        style.fill("yellow"),
        loc.grand_summary_stub(),
    ).tab_style(
        style.fill("lightblue"),
        loc.grand_summary(),
    )
)
title
subtitle
stubhead num spanner
char fctr
grp_a
row_1 0.1111 apricot one
row_2 2.222 banana two
grp_b
row_5 5550.0 five
Total 5552.3331 --- ---
yo
Styling the Table Body
Colorizing with Data