posit-dev / great-tables

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

great tables emitting invalid html for group heading rows #306

Closed gordonwoodhull closed 2 months ago

gordonwoodhull commented 2 months ago

Similar to #224, Great Tables is emitting misordered <tr> tags. This doesn't cause any rendering issues in the browser, but it will cause Pandoc to reject the table. You may not notice this in Quarto if your output format is HTML, but the table will get dropped for any other format.

Group heading rows in the Oceania example reproduce this problem.

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
oceania = {
    "Australasia": ["AU", "NZ"],
    "Melanesia": ["NC", "PG", "SB", "VU"],
    "Micronesia": ["FM", "GU", "KI", "MH", "MP", "NR", "PW"],
    "Polynesia": ["PF", "WS", "TO", "TV"],
}

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

wide_pops = (
    pl.from_pandas(countrypops)
    .filter(
        pl.col("country_code_2").is_in(list(country_to_region))
        & pl.col("year").is_in([2000, 2010, 2020])
    )
    .with_columns(pl.col("country_code_2").replace(country_to_region).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")
    .tab_header(title="Populations of Oceania's Countries in 2000, 2010, and 2020")
    .tab_spanner(label="Total Population", columns=cs.all())
    .fmt_integer()
)

will produce 4 instances like this:

<tr>
<tr class=gt_group_heading_row>  <th class="gt_group_heading" colspan="4">Micronesia</th></tr>
  <th class="gt_row gt_left gt_stub">Guam</th>
  <td class="gt_row gt_right">160,188</td>
  <td class="gt_row gt_right">164,905</td>
  <td class="gt_row gt_right">169,231</td>
</tr>

It looks like the open <tr> for the data row is emitted before the group heading row when it should be after.