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: add style.from_column, implement for loc.body #83

Merged
merged 8 commits into from
Dec 14, 2023

Conversation

machow
Copy link
Collaborator

@machow machow commented Dec 13, 2023

This PR implements the from_column() behavior from gt, which allows for setting a style based on values in the data.

e.g.

import pandas as pd
from great_tables import GT, exibble, loc, style

df = pd.DataFrame({"x": [1, 2], "color": ["red", "blue"]})

gt = GT(df)
gt.tab_style(
    style = style.text(color = style.from_column("color")),
    locations = loc.body(columns = ["x"])
)
image

NOTES:

  • scope: currently only implemented for setting in loc.body(), raises if you try to use it with loc.title()
  • implementation: the R gt library handles from_column inside each fmt_* function. This PR puts the handling inside set_style, by implementing a CellStyle._from_row() method. This gives each style the chance to return a new version of itself based on the data, just before setting it on the GT object.
  • importing: I put it under style.from_column(), but another place it could go is with helpers like md() at the top-level of the package?

@machow
Copy link
Collaborator Author

machow commented Dec 13, 2023

Reference page!

image

@machow machow requested a review from rich-iannone December 13, 2023 17:52
@machow machow marked this pull request as ready for review December 13, 2023 17:52
Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

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

This is great! I think the main changes needed (as discussed) are: (1) having from_column() is not part of style, (2) enabling support for pl.col() from Polars.

@machow
Copy link
Collaborator Author

machow commented Dec 13, 2023

Alright, now we have:

  • support for polars expressions in addition to from_colums()
  • from_columns now only exported at top-level (i.e. from great_tables import from_column)

Note that I added a new internal class called FromValues, to represent a materialized column of values (e.g. from the polars Series that results from executing the polars expression). I didn't move FromColumn out of _styles.py, but that was mostly me being unsure of where to put it. I wonder if we might just leave it there until it gets used for other things? (e.g. fmt_*)

(down for anything though!)

@machow
Copy link
Collaborator Author

machow commented Dec 13, 2023

Here's the example from the from_column docs

image

Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

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

LGTM!!!

@machow
Copy link
Collaborator Author

machow commented Dec 14, 2023

noyce

@machow machow merged commit 24d24b4 into main Dec 14, 2023
6 checks passed
@rich-iannone rich-iannone deleted the feat-style-from-column branch December 14, 2023 21:03
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