Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: cols_width() should exclude hidden columns #509

Merged
merged 14 commits into from
Nov 21, 2024

Conversation

rich-iannone
Copy link
Member

This fixes #491, and the problem is that hidden columns are included in the <colgroup> tag when only visible columns should be included.

With the fix, the following table renders correctly (adapted from the issue's table):

from great_tables import GT, exibble

exibble_mini = exibble[["num", "char", "date", "datetime", "row"]].head(1)

exibble_mini["long_text"] = (
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
)


tbl = (
    GT(exibble_mini)
    .cols_hide(columns="datetime")
    .cols_width(
        cases={
            "num": "50px",
            "char": "50px",
            "date": "400px",
            "row": "50px",
            "long_text": "400px",
            "datetime": "20px"}
    )
    .tab_options(
        table_body_vlines_style="solid",
        table_body_vlines_width="3px",
        table_body_vlines_color="red"
    )
)

tbl
image

The colgroup here is now:

<colgroup>
  <col style="width:50px;"/>
  <col style="width:50px;"/>
  <col style="width:400px;"/>
  <col style="width:50px;"/>
  <col style="width:400px;"/>
</colgroup>

which matches the number of columns and has the correct widths for each of the columns. Including or excluding a column width definition for a hidden column in the GT code makes no difference to the final result.

There was also another bug addressed with column widths and it involves not writing a column width in <colgroup> for a stub column. This PR fixes that by retrieving the column width (if any) for a stub column and placing it at the beginning of the series in <colgroup>. Here's a revised version of the above code that uses the "date" column as the row label column in the stub:

...

tbl = (
    GT(exibble_mini, rowname_col="date")
...
image

Here's the resulting <colgroup>:

<colgroup>
  <col style="width:400px;"/>
  <col style="width:50px;"/>
  <col style="width:50px;"/>
  <col style="width:50px;"/>
  <col style="width:400px;"/>
</colgroup>

Fixes: #491

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.11%. Comparing base (336e688) to head (f61f662).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #509      +/-   ##
==========================================
+ Coverage   88.91%   89.11%   +0.20%     
==========================================
  Files          44       44              
  Lines        5212     5220       +8     
==========================================
+ Hits         4634     4652      +18     
+ Misses        578      568      -10     

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


🚨 Try these New Features:

@github-actions github-actions bot temporarily deployed to pr-509 November 14, 2024 16:50 Destroyed
@github-actions github-actions bot temporarily deployed to pr-509 November 14, 2024 18:39 Destroyed
@rich-iannone rich-iannone marked this pull request as ready for review November 14, 2024 19:28
# If there is a stub column, then get the width for that column and insert it at the beginning
if stub_var is not None:

stub_width = data._boxhead._get_column_width_by_var(var=stub_var.var)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a method like Boxhead.canonical_columns() or .final_columns(), that returns a list[ColInfo] for all final visible columns (including stub, and group column if correct option is set), in the order they will be rendered in?

This way, Boxhead becomes the source of truth on the final columns. I realize there's some ambiguity between Stub and Boxhead columns. But it seems okay for the final columns across these two things to live in one place, and the Boxhead seems like it might be the best fit?

In this case, the code could just call something like...

columns = data._boxhead.canonical_columns(options)

# widths for all columns that will be represented as columns
# e.g. groupings are rows by default, so wouldn't be included,
# but if the option to make them a column is set, then they would
widths = [col.width for col in columns]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now done, thanks for your help on this!

@github-actions github-actions bot temporarily deployed to pr-509 November 15, 2024 16:52 Destroyed
# - removes hidden columns
# - places the stub column in the correct position
# - places the row group column in the correct position (if option is set for that)
def final_columns(self, options: Options) -> Self:
Copy link
Collaborator

@machow machow Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if rather than...

  • get visible columns
  • get stub
  • if stub exists, remove from visible columns and create a new list of columns with stub at the front
  • get group column
  • if group column exists do something similar (shifting around columns)

We constructed the final column list directly, so something like:

  • get stub column if it exists and append to list
  • get row group column if it exists (and option dictates its a column) and append to list
  • extend list with remaining columns (those marked ColInfoTypeEnum.default?)

This way the code reads in the same order as the final columns returned

# TODO: ensure that the stub column is set first in the list
widths = data._boxhead._get_column_widths()
# Get all the widths for the columns (this includes the stub column)
boxh_final = data._boxhead.final_columns(options=data._options)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we return a list of columns for the result of final_columns? It seems likely this boxhead with columns dropped (rather than hidden) violates some assumption of our data model.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now done. Thanks!

@github-actions github-actions bot temporarily deployed to pr-509 November 15, 2024 19:51 Destroyed
default_columns = self._get_default_columns()
combined = [row_group_column] + [stub_column] + default_columns

return [x.var for x in combined if x is not None]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we just return each x, rather than x.var? That way people can use the ColInfo directly to get any pieces they needed, rather than fetching ColInfo later off the boxhead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Now implemented.

@github-actions github-actions bot temporarily deployed to pr-509 November 16, 2024 17:10 Destroyed
@github-actions github-actions bot temporarily deployed to pr-509 November 21, 2024 17:33 Destroyed
@machow
Copy link
Collaborator

machow commented Nov 21, 2024

This looks great, thanks!

@machow machow merged commit aa7d553 into main Nov 21, 2024
14 checks passed
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.

cols_width() not stretching the cell values
2 participants