posit-dev / great-tables

Make awesome display tables using Python.
https://posit-dev.github.io/great-tables/
MIT License
1.44k stars 50 forks source link

fmt_markdown method doesn't seem to work on groupname_col #236

Open chiaochi opened 4 months ago

chiaochi commented 4 months ago

Prework

Description

fmt_markdown method doesn't seem to work on groupname_col

Reproducible example

Slightly modifying the "Populations of Oceania's Countries in 2000, 2010, and 2020" table to use as example. What I did:

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()
)

Expected result

The row group (i.e. region) should be a hyperlink instead it's just showing the raw text.

Development environment

Additional context

Add any other context about the problem here.

rich-iannone commented 4 months ago

Thank you for submitting this issue! Definitely, that method should work on the column designated for grouping. Will fix this soon.

rasmi commented 2 months ago

Similar (but possibly distinct) issue: If you set rowname_col in GT(), you cannot apply styles to that column with tab_style and loc.body. For example:

(
GT(..., rowname_col='Category')
 .tab_style(
      style=style.borders(sides=["bottom"], style="double", weight="4px"),
      locations=loc.body(rows=[-1])
    )
)

applies a line across the full table if rowname_col is not set, but doesn't apply the line to rowname_col if it is set. Specifying columns=[0, ...] does not fix this.