Skip to content

Conversation

juleswg23
Copy link
Contributor

Summary

Apply fmt() output to group labels in the stub.

This approach goes upstream of the approach in #755. It is admittedly, a little heavier-weight, but does still happen at render time. The advantage to this over #755 is that we are modifying the group label before the html rendering, so this will apply to latex tables. Additionally, it takes advantage of the stubbed out group_row.group_label attribute.

from great_tables import GT
from great_tables.data import countrypops
import polars as pl
import polars.selectors as cs

# Get vectors of 2-letter country codes for each region of Oceania
countries = {
    "[Australasia](http://www.google.com)": ["AU", "NZ"],
    "[Melanesia](http://www.google.com)": ["NC", "PG", "SB", "VU"],
    "[Micronesia](http://www.google.com)": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
    "[Polynesia](http://www.google.com)": ["PF", "WS", "TO", "TV"],
}

# Create a dictionary mapping region to country (e.g. AU -> Australasia)
region_to_country = {
    region: country for country, regions in countries.items() for region in regions
}

wide_pops = (
    pl.from_pandas(countrypops)
    .filter(
        pl.col("country_code_2").is_in(list(region_to_country))
        & pl.col("year").is_in([2000, 2010, 2020])
    )
    .with_columns(pl.col("country_code_2").replace(region_to_country).alias("region"))
    .pivot(index=["country_name", "region"], columns="year", values="population")
    .sort("2020", descending=True)
)

(
    GT(wide_pops, rowname_col="country_name", groupname_col="region")
    .fmt_markdown(columns=["region"])
    .tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020")
    .tab_spanner(label="Total Population", columns=cs.all())
    .fmt_integer()
)

Previously, this code block had the following output.

image

Now we see this output:

image

Related GitHub Issues and PRs

closes #761

We could also close #236 if this PR is merged.

Checklist

Copy link

codecov bot commented Aug 28, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.47%. Comparing base (654757a) to head (bdc4d0f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #769      +/-   ##
==========================================
+ Coverage   91.45%   91.47%   +0.02%     
==========================================
  Files          47       47              
  Lines        5558     5573      +15     
==========================================
+ Hits         5083     5098      +15     
  Misses        475      475              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@juleswg23
Copy link
Contributor Author

This PR is based on a conversation with @machow. I am somewhat indifferent to the approaches here and in #755, but it would be nice to go forward with at least one of these. Whichever makes sense to continue with, I'll commit a new test to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set group_info.group_label to the displayed group label fmt_markdown method doesn't seem to work on groupname_col
1 participant