Skip to content

Commit d932df5

Browse files
committed
Modify the locations parameter of opt_all_caps() to accept Loc objects
1 parent c42c1ad commit d932df5

3 files changed

Lines changed: 116 additions & 16 deletions

File tree

great_tables/_options.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,8 @@ def opt_all_caps(
893893
894894
locations
895895
Which locations should undergo this text transformation? By default it includes all of
896-
the `"column_labels"`, the `"stub"`, and the `"row_group"` locations. However, we could
897-
just choose one or two of those.
896+
the `loc.column_labels`, the `loc.stub"`, and the `loc.row_group` locations. However, we
897+
could just choose one or two of those.
898898
899899
Returns
900900
-------
@@ -909,7 +909,7 @@ def opt_all_caps(
909909
in all row groups is transformed to all caps using the `opt_all_caps()` method.
910910
911911
```{python}
912-
from great_tables import GT, exibble, md
912+
from great_tables import GT, exibble, loc, md
913913
914914
(
915915
GT(
@@ -927,16 +927,46 @@ def opt_all_caps(
927927
.opt_all_caps()
928928
)
929929
```
930+
`opt_all_caps()` accepts a `locations` parameter that allows us to specify which components
931+
should be transformed. For example, if we only want to ensure that all text in the stub and all
932+
row groups is converted to all caps:
933+
```{python}
934+
(
935+
GT(
936+
exibble[["num", "char", "currency", "row", "group"]],
937+
rowname_col="row",
938+
groupname_col="group"
939+
)
940+
.tab_header(
941+
title=md("Data listing from **exibble**"),
942+
subtitle=md("`exibble` is a **Great Tables** dataset.")
943+
)
944+
.fmt_number(columns="num")
945+
.fmt_currency(columns="currency")
946+
.tab_source_note(source_note="This is only a subset of the dataset.")
947+
.opt_all_caps(locations=[loc.stub, loc.row_group])
948+
)
949+
```
930950
"""
951+
from great_tables._locations import Loc, LocColumnLabels, LocStub, LocRowGroups
931952

932-
# If providing a scalar string value, normalize it to be in a list
933-
if not isinstance(locations, list):
934-
locations = _utils._str_scalar_to_list(cast(str, locations))
935-
936-
# Ensure that the `locations` value is a list of strings
937-
_utils._assert_str_list(locations)
953+
if not locations:
954+
locations = [LocColumnLabels, LocStub, LocRowGroups]
938955

939-
# TODO: Ensure that all values within `locations` are valid
956+
# If providing a Loc object, normalize it to be in a list
957+
if not isinstance(locations, list):
958+
locations = [locations]
959+
960+
# Ensure that all values within `locations` are valid
961+
# A `try-except` block is needed here because the first argument of `issubclass()` must be a
962+
# class.
963+
for location in locations:
964+
try:
965+
issubclass(location, Loc)
966+
except TypeError:
967+
raise AssertionError(
968+
"Only `loc.column_labels`, `loc.stub` and `loc.row_group` are allowed in the locations."
969+
)
940970

941971
# if `all_caps` is False, reset options to default, or, set new options
942972
# for `locations` selected
@@ -956,23 +986,23 @@ def opt_all_caps(
956986

957987
info = [
958988
(
959-
"column_labels",
989+
LocColumnLabels,
960990
{
961991
"column_labels_font_size": "80%",
962992
"column_labels_font_weight": "bolder",
963993
"column_labels_text_transform": "uppercase",
964994
},
965995
),
966996
(
967-
"stub",
997+
LocStub,
968998
{
969999
"stub_font_size": "80%",
9701000
"stub_font_weight": "bolder",
9711001
"stub_text_transform": "uppercase",
9721002
},
9731003
),
9741004
(
975-
"row_group",
1005+
LocRowGroups,
9761006
{
9771007
"row_group_font_size": "80%",
9781008
"row_group_font_weight": "bolder",

great_tables/loc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
LocBody as body,
55
LocStub as stub,
66
LocColumnLabels as column_labels,
7+
LocRowGroups as row_group,
78
)
89

9-
__all__ = ("body", "stub", "column_labels")
10+
__all__ = ("body", "stub", "column_labels", "row_group")

tests/test_options.py

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pandas as pd
22
import pytest
3-
from great_tables import GT, exibble, md
3+
from great_tables import GT, exibble, loc, md
44
from great_tables._scss import compile_scss
55
from great_tables._gt_data import default_fonts_list
66

@@ -328,7 +328,6 @@ def test_scss_from_opt_table_outline(gt_tbl: GT, snapshot):
328328

329329

330330
def test_opt_table_font_add_font():
331-
332331
gt_tbl = GT(exibble).opt_table_font(font="Arial", weight="bold", style="italic")
333332

334333
assert gt_tbl._options.table_font_names.value == ["Arial"] + default_fonts_list
@@ -369,3 +368,73 @@ def test_opt_table_font_raises():
369368
GT(exibble).opt_table_font(font=None, stack=None)
370369

371370
assert "Either `font=` or `stack=` must be provided." in exc_info.value.args[0]
371+
372+
373+
def test_opt_all_caps(gt_tbl: GT):
374+
tbl = gt_tbl.opt_all_caps(locations=loc.column_labels)
375+
376+
assert tbl._options.column_labels_font_size.value == "80%"
377+
assert tbl._options.column_labels_font_weight.value == "bolder"
378+
assert tbl._options.column_labels_text_transform.value == "uppercase"
379+
380+
tbl = gt_tbl.opt_all_caps(locations=[loc.column_labels, loc.stub])
381+
382+
assert tbl._options.column_labels_font_size.value == "80%"
383+
assert tbl._options.column_labels_font_weight.value == "bolder"
384+
assert tbl._options.column_labels_text_transform.value == "uppercase"
385+
386+
assert tbl._options.stub_font_size.value == "80%"
387+
assert tbl._options.stub_font_weight.value == "bolder"
388+
assert tbl._options.stub_text_transform.value == "uppercase"
389+
390+
tbl = gt_tbl.opt_all_caps(locations=[loc.column_labels, loc.stub, loc.row_group])
391+
392+
assert tbl._options.column_labels_font_size.value == "80%"
393+
assert tbl._options.column_labels_font_weight.value == "bolder"
394+
assert tbl._options.column_labels_text_transform.value == "uppercase"
395+
396+
assert tbl._options.stub_font_size.value == "80%"
397+
assert tbl._options.stub_font_weight.value == "bolder"
398+
assert tbl._options.stub_text_transform.value == "uppercase"
399+
400+
assert tbl._options.row_group_font_size.value == "80%"
401+
assert tbl._options.row_group_font_weight.value == "bolder"
402+
assert tbl._options.row_group_text_transform.value == "uppercase"
403+
404+
tbl = gt_tbl.opt_all_caps()
405+
406+
assert tbl._options.column_labels_font_size.value == "80%"
407+
assert tbl._options.column_labels_font_weight.value == "bolder"
408+
assert tbl._options.column_labels_text_transform.value == "uppercase"
409+
410+
assert tbl._options.stub_font_size.value == "80%"
411+
assert tbl._options.stub_font_weight.value == "bolder"
412+
assert tbl._options.stub_text_transform.value == "uppercase"
413+
414+
assert tbl._options.row_group_font_size.value == "80%"
415+
assert tbl._options.row_group_font_weight.value == "bolder"
416+
assert tbl._options.row_group_text_transform.value == "uppercase"
417+
418+
tbl = gt_tbl.opt_all_caps(all_caps=False)
419+
420+
assert tbl._options.column_labels_font_size.value == "100%"
421+
assert tbl._options.column_labels_font_weight.value == "normal"
422+
assert tbl._options.column_labels_text_transform.value == "inherit"
423+
424+
assert tbl._options.stub_font_size.value == "100%"
425+
assert tbl._options.stub_font_weight.value == "initial"
426+
assert tbl._options.stub_text_transform.value == "inherit"
427+
428+
assert tbl._options.row_group_font_size.value == "100%"
429+
assert tbl._options.row_group_font_weight.value == "initial"
430+
assert tbl._options.row_group_text_transform.value == "inherit"
431+
432+
433+
def test_opt_all_caps_raises(gt_tbl: GT):
434+
with pytest.raises(AssertionError) as exc_info:
435+
gt_tbl.opt_all_caps(locations="column_labels")
436+
437+
assert (
438+
f"Only `loc.column_labels`, `loc.stub` and `loc.row_group` are allowed in the locations."
439+
in exc_info.value.args[0]
440+
)

0 commit comments

Comments
 (0)