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

feat: implement row striping options #461

Merged
merged 9 commits into from
Sep 25, 2024
Merged

feat: implement row striping options #461

merged 9 commits into from
Sep 25, 2024

Conversation

rich-iannone
Copy link
Member

This PR addresses #435 by making row striping options functional in rendered tables. An example of using row striping involves three new options in tab_options():

from great_tables import GT, exibble

(
    GT(exibble[["num", "char", "row", "group"]], rowname_col="row", groupname_col="group")
    .tab_options(
        row_striping_include_table_body=True,
        row_striping_include_stub=True,
        row_striping_background_color="lightblue"
    )
)
image

Alternatively, the opt_row_striping() method allows for a simplified shortcut for row_striping_include_table_body=True so the user doesn't have to look through tab_options() for this basic functionality:

from great_tables import GT, exibble

(
    GT(exibble[["num", "char", "row", "group"]], rowname_col="row", groupname_col="group")
    .opt_row_striping()
)
image

Note that the default background color for striping is a very light gray (by design).

Copy link

codecov bot commented Sep 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.80%. Comparing base (fd3ac52) to head (663fce8).
Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #461      +/-   ##
==========================================
+ Coverage   86.77%   86.80%   +0.03%     
==========================================
  Files          42       42              
  Lines        4749     4760      +11     
==========================================
+ Hits         4121     4132      +11     
  Misses        628      628              

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

@machow
Copy link
Collaborator

machow commented Sep 24, 2024

If #460 looks good, once we merge it and update the build should pass again!

@rich-iannone rich-iannone marked this pull request as ready for review September 24, 2024 16:27
@github-actions github-actions bot temporarily deployed to pr-461 September 24, 2024 16:40 Destroyed
@github-actions github-actions bot temporarily deployed to pr-461 September 24, 2024 16:54 Destroyed
@rich-iannone rich-iannone requested a review from machow September 24, 2024 16:59
@@ -701,3 +701,163 @@

'''
# ---
# name: test_tab_options_striping_snap
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is too big. Can you reduce it to a snapshot that is mostly limited to the piece being tested, or test it another way?

gt_tbl_opt = GT(exibble, id="test").opt_row_striping()
gt_tbl_tab_options = GT(exibble, id="test").tab_options(row_striping_include_table_body=True)

assert gt_tbl_opt.as_raw_html() == gt_tbl_tab_options.as_raw_html()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you test this further upstream? If rendered html strings are the very last step, is there an earlier step that indicates these two approaches do the same thing (e.g. checking the options values directly, etc..)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll check the options values directly here (comparing the two approaches).

assert gt_tbl_opt.as_raw_html() == gt_tbl_tab_options.as_raw_html()


def test_tab_options_striping_snap(snapshot):
Copy link
Collaborator

@machow machow Sep 24, 2024

Choose a reason for hiding this comment

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

See comment on snapshot. It'd be okay to test the css and html separately if it makes it easier to strip down. I wonder if you could easily test the css using an assertion, and snapshot test just the html body (with fewer rows of data; the minimum needed to test--3? or w/e makes sense given there are groupings; no need to have all columns in the snapshot please strip down)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to make the input df with much smaller dimensions and only snapshot the table body (everything inside <tbody>). I don't think there's a need to snapshot the CSS part.

@github-actions github-actions bot temporarily deployed to pr-461 September 25, 2024 03:24 Destroyed
@rich-iannone rich-iannone requested a review from machow September 25, 2024 04:13
@@ -503,10 +515,30 @@ def create_body_component_h(data: GTData) -> str:
cell_styles = ""

if is_stub_cell:
body_cells.append(f""" <th class="gt_row gt_left gt_stub">{cell_str}</th>""")

Copy link
Collaborator

@machow machow Sep 25, 2024

Choose a reason for hiding this comment

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

This is looking good, so just a small nit here 😅. Do you mind consolidating so there is a bit less duplicated code? Seems like the following could happen:

  • change variables like td_classes and th_classes to have the same name. Similar with th_classes_str, etc..
  • add a variable like el_name that is either "th" or "td"
  • move all other logic (e.g. the appending of "gt_striped", the " ".join, the body_cells.append) out of the if / else, so it is only written once (e.g. move it to line 543 and unindent)
    • Use el_name in what should now be a single piece of code creating the final th or td string.

It looks like if something is a stub, that only affects the element name and some of the classes to be set. (But other logic is being included)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, will make these changes!

Comment on lines 419 to 420
row_striping_include_table_body=True,
row_striping_include_stub=True,
Copy link
Collaborator

@machow machow Sep 25, 2024

Choose a reason for hiding this comment

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

Can you test these separately, so that we know if one option is specified, it does not set both things?

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. There is now two distinct snapshots where one tests striping only of the stub, the other only of the body cells.

@github-actions github-actions bot temporarily deployed to pr-461 September 25, 2024 16:37 Destroyed
Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

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

Yo, thanks for all these tweaks, it's beautiful!

@machow machow merged commit 5548da1 into main Sep 25, 2024
13 checks passed
@rich-iannone
Copy link
Member Author

Thanks @machow! Always a pleasure.

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.

2 participants