Skip to content

Commit

Permalink
docs: update the related docs about mask= parameter in loc.body() (
Browse files Browse the repository at this point in the history
…#589)

* Update `basic-styling.qmd`

* Update `loc-selection.qmd`

* Fix error messages

* Move `mask=` experimental callout to the top

---------

Co-authored-by: Michael Chow <[email protected]>
  • Loading branch information
jrycw and machow authored Jan 27, 2025
1 parent d71ee99 commit 3290208
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/get-started/basic-styling.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ gt_pl_air.tab_style(
)
```

Lastly, you can use **Polars** selectors or expressions to conditionally select rows on a per-column basis.
```{python}
import polars.selectors as cs
gt_pl_air.tab_style(
style=style.fill(color="yellow"),
locations=loc.body(mask=cs.all().eq(cs.all().max())),
)
```


## Learning more

* API Docs:
Expand Down
15 changes: 12 additions & 3 deletions docs/get-started/loc-selection.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ For example, `loc.header()` includes both `loc.title()` and `loc.subtitle()`.
)
```

## Body columns and rows

Use `loc.body()` to style specific cells in the table body.
## Body columns, rows and mask

Use `columns=` and `rows=` in `loc.body()` to style specific cells in the table body.
```{python}
(
GT(pl_exibble).tab_style(
Expand All @@ -100,6 +99,16 @@ Use `loc.body()` to style specific cells in the table body.
)
```

Alternatively, use `mask=` in `loc.body()` to apply conditional styling to rows on a per-column basis.
```{python}
(
GT(pl_exibble).tab_style(
style.fill("yellow"),
loc.body(mask=cs.string().str.contains("p")),
)
)
```

This is discussed in detail in [Styling the Table Body](./basic-styling.qmd).

## Column labels
Expand Down
12 changes: 6 additions & 6 deletions great_tables/_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ class LocBody(Loc):
custom styling with the [`tab_style()`](`great_tables.GT.tab_style`) method. That method has a
`locations=` argument and this class should be used there to perform the targeting.
:::{.callout-warning}
`mask=` is still experimental.
:::
Parameters
----------
columns
Expand All @@ -507,10 +511,6 @@ class LocBody(Loc):
you can pass a Polars expression for cell-based selection. This argument must be used
exclusively and cannot be combined with the `columns=` or `rows=` arguments.
:::{.callout-warning}
`mask=` is still experimental.
:::
Returns
-------
LocBody
Expand Down Expand Up @@ -864,8 +864,8 @@ def resolve_mask(
if masked.height != frame.height:
raise ValueError(
"The DataFrame length after applying `mask` differs from the original."
"\n\n* Original length: {frame.height}"
"\n* Mask length: {masked.height}"
f"\n\n* Original length: {frame.height}"
f"\n* Mask length: {masked.height}"
)

cellpos_data: list[tuple[int, int, str]] = [] # column, row, colname for `CellPos`
Expand Down

0 comments on commit 3290208

Please sign in to comment.