Skip to main content

stub

Sentinel for selecting the stub column(s).

stub=_StubSentinel()

Use it in cols_width(), or in the columns= of methods that can target the stub (e.g., the fmt_*() methods, or cols_hide()), as in fmt_number(columns=stub). It refers to the stub whatever its column is named, so the same code works for tables with different stubs. Methods that don’t apply to the stub (e.g., spanners) skip it, just as they skip a stub column given by name.

With a hierarchical stub (where rowname_col= is a list of columns), stub refers to every stub column. Call it with a number to refer to a single level, counting from the right: stub(1) is the last (row label) column, stub(2) the one to its left, and so on.

Examples

Pass stub as a dictionary key to set the width of the stub column without risking a collision with a data column named "stub".

from great_tables import GT, exibble, stub

GT(exibble[["row", "num", "char"]], rowname_col="row").cols_width(
    cases={stub: "150px", "num": "100px"}
)
/home/runner/work/great-tables/great-tables/great_tables/_render_checks.py:37: RenderWarning: Rendering table with .cols_width() in Quarto may result in unexpected behavior. This is because Quarto performs custom table processing. Either use all percentage widths, or set .tab_options(quarto_disable_processing=True) to disable Quarto table processing.
  warnings.warn(
num char
row_1 0.1111 apricot
row_2 2.222 banana
row_3 33.33 coconut
row_4 444.4 durian
row_5 5550.0 None
row_6 None fig
row_7 777000.0 grapefruit
row_8 8880000.0 honeydew

With a hierarchical stub, stub(1) and stub(2) set the widths of its two columns separately.

GT(exibble[["group", "row", "num"]].head(4), rowname_col=["group", "row"]).cols_width(
    cases={stub(2): "120px", stub(1): "80px"}
)
/home/runner/work/great-tables/great-tables/great_tables/_render_checks.py:37: RenderWarning: Rendering table with .cols_width() in Quarto may result in unexpected behavior. This is because Quarto performs custom table processing. Either use all percentage widths, or set .tab_options(quarto_disable_processing=True) to disable Quarto table processing.
  warnings.warn(
num
grp_a row_1 0.1111
row_2 2.222
row_3 33.33
row_4 444.4

The stub’s values can be formatted like any column’s. Here the stub holds doses, which fmt_number() gives one decimal place and a unit.

import polars as pl

df = pl.DataFrame({"dose": [0.5, 1, 2.5], "response": [12.1, 25.4, 47.9]})

GT(df, rowname_col="dose").fmt_number(columns=stub, decimals=1, pattern="{x} mg")
response
0.5 mg 12.1
1.0 mg 25.4
2.5 mg 47.9