Skip to content

Commit b1197c4

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

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

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+
f"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")

0 commit comments

Comments
 (0)