Skip to main content

style.fill

A style specification for the background fill of targeted cells.

Usage

Source

style.fill(color)

The style.fill() class is to be used with the tab_style() method, which itself allows for the setting of custom styles to one or more cells. Specifically, the call to style.fill() should be bound to the style= argument of tab_style().

Parameters

color: str | ColumnExpr
The color to use for the cell background fill. This can be any valid CSS color value, such as a hex code, a named color, or an RGB value. It can also take its values from a column, with from_column() (or, for a Polars DataFrame, a Polars expression), so that each targeted cell gets the color in its row.

Returns

CellStyleFill
A CellStyleFill object, which is used for a style= argument if specifying a cell fill value.

Examples

Here, style.fill() gives the rows with a num value above 100 a light yellow background.

from great_tables import GT, exibble, loc, style

(
    GT(exibble[["num", "char", "fctr"]].head(6))
    .tab_style(
        style=style.fill(color="lightyellow"),
        locations=loc.body(rows=lambda df: df["num"] > 100),
    )
)
num char fctr
0.1111 apricot one
2.222 banana two
33.33 coconut three
444.4 durian four
5550.0 None five
None fig six

The color can also come from a column of the data, with from_column(). Here, each color cell is filled with the color it names, and the color column is then hidden from the table.

import polars as pl
from great_tables import GT, from_column, loc, style

df = pl.DataFrame(
    {"fruit": ["apple", "banana", "plum"], "color": ["#F8C9C9", "#FFF3B0", "#D9C8F0"]}
)

(
    GT(df)
    .tab_style(
        style=style.fill(color=from_column("color")),
        locations=loc.body(columns="fruit"),
    )
    .cols_hide(columns="color")
)
fruit
apple
banana
plum

See GT.tab_style() for more examples.