-
Notifications
You must be signed in to change notification settings - Fork 72
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚨 Try these New Features:
|
great_tables/_utils_render_html.py
Outdated
# 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) |
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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!
great_tables/_gt_data.py
Outdated
# - 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: |
There was a problem hiding this comment.
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
great_tables/_utils_render_html.py
Outdated
# 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now done. Thanks!
great_tables/_gt_data.py
Outdated
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] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Now implemented.
This looks great, thanks! |
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):
The colgroup here is now:
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:Here's the resulting
<colgroup>
:Fixes: #491