|
| 1 | +from __future__ import annotations |
| 2 | +from typing import Literal |
| 3 | + |
| 4 | +from great_tables import GT, style, loc |
| 5 | +from great_tables._tbl_data import SelectExpr |
| 6 | + |
| 7 | + |
| 8 | +__all__ = ["gt_add_divider"] |
| 9 | + |
| 10 | + |
| 11 | +def gt_add_divider( |
| 12 | + gt: GT, |
| 13 | + columns: SelectExpr, |
| 14 | + sides: ( |
| 15 | + Literal["right", "left", "top", "bottom", "all"] |
| 16 | + | list[Literal["right", "left", "top", "bottom", "all"]] |
| 17 | + ) = "right", |
| 18 | + color: str = "grey", |
| 19 | + divider_style: Literal["solid", "dashed", "dotted", "hidden", "double"] = "solid", |
| 20 | + weight: int = 2, |
| 21 | + include_labels: bool = True, |
| 22 | +) -> GT: |
| 23 | + """ |
| 24 | + Add dividers to specified columns in a GT table. |
| 25 | +
|
| 26 | + The `gt_add_divider()` function takes an existing `GT` object and adds dividers to the specified |
| 27 | + columns. Dividers can be applied to one or more sides of the cells, with customizable color, |
| 28 | + style, and weight. Optionally, dividers can also be applied to column labels. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + gt |
| 33 | + A `GT` object to modify. |
| 34 | +
|
| 35 | + columns |
| 36 | + The columns to which dividers should be applied. |
| 37 | +
|
| 38 | + sides |
| 39 | + The sides of the cells where dividers should be added. Options include `"right"`, `"left"`, |
| 40 | + `"top"`, `"bottom"`, or `"all"`. A list of sides can also be provided to apply dividers to |
| 41 | + multiple sides. |
| 42 | +
|
| 43 | + color |
| 44 | + The color of the dividers. |
| 45 | +
|
| 46 | + divider_style |
| 47 | + The style of the dividers. Options include `"solid"`, `"dashed"`, `"dotted"`, `"hidden"`, |
| 48 | + and `"double"`. |
| 49 | +
|
| 50 | + weight |
| 51 | + The thickness of the dividers in pixels. |
| 52 | +
|
| 53 | + include_labels |
| 54 | + Whether to include dividers in the column labels. If `True`, dividers will be applied to |
| 55 | + both the body and the column labels. If `False`, dividers will only be applied to the body. |
| 56 | +
|
| 57 | + Returns |
| 58 | + ------- |
| 59 | + GT |
| 60 | + A `GT` object with dividers added to the specified columns. |
| 61 | +
|
| 62 | + Examples |
| 63 | + -------- |
| 64 | + ```{python} |
| 65 | + import pandas as pd |
| 66 | + from great_tables import GT |
| 67 | + from great_tables.data import peeps |
| 68 | + import gt_extras as gte |
| 69 | +
|
| 70 | + peeps_mini = peeps.head(6) |
| 71 | +
|
| 72 | + gt = ( |
| 73 | + GT(peeps_mini) |
| 74 | + .cols_hide([ |
| 75 | + "name_family", "postcode", "country", "country_code", |
| 76 | + "dob", "gender", "state_prov", "email_addr", |
| 77 | + ]) |
| 78 | + .tab_spanner("Location", ["address", "city"]) |
| 79 | + .tab_spanner("Body Measurements", ["height_cm", "weight_kg"]) |
| 80 | + ) |
| 81 | +
|
| 82 | + gt.pipe( |
| 83 | + gte.gt_add_divider, |
| 84 | + columns="name_given", |
| 85 | + color="#FFB90F", |
| 86 | + divider_style="double", |
| 87 | + weight=8, |
| 88 | + ).pipe( |
| 89 | + gte.gt_add_divider, |
| 90 | + columns="phone_number", |
| 91 | + color="purple", |
| 92 | + sides=["right", "left"], |
| 93 | + weight=5, |
| 94 | + ) |
| 95 | + ``` |
| 96 | + """ |
| 97 | + |
| 98 | + locations = [loc.body(columns=columns)] |
| 99 | + |
| 100 | + if include_labels: |
| 101 | + locations.append(loc.column_labels(columns=columns)) |
| 102 | + |
| 103 | + res = gt.tab_style( |
| 104 | + style=style.borders( |
| 105 | + sides=sides, |
| 106 | + color=color, |
| 107 | + weight=weight, |
| 108 | + style=divider_style, |
| 109 | + ), |
| 110 | + locations=locations, |
| 111 | + ) |
| 112 | + |
| 113 | + return res |
0 commit comments