-
Notifications
You must be signed in to change notification settings - Fork 76
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
Implement GT.pipe()
#363
Merged
Merged
Implement GT.pipe()
#363
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1623719
Implement `GT.pipe()` and `GT.pipes()`
jrycw f9702db
Allow passing a list of functions to `GT.pipes()`
jrycw 7e4a167
Code review change
jrycw 3d7152d
Improve type hints for `GT.pipe()`
jrycw c4f4801
Merge branch 'main' into fix-353
machow 2dafcc7
tests: simplify .pipe test
machow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Callable, Any | ||
|
||
if TYPE_CHECKING: | ||
from .gt import GT | ||
|
||
|
||
def pipe(self: "GT", func: Callable[..., "GT"], *args: Any, **kwargs: Any) -> "GT": | ||
""" | ||
Provide a structured way to chain a function for a GT object. | ||
|
||
This function accepts a function that receives a GT object along with optional positional and | ||
keyword arguments, returning a GT object. This allows users to easily integrate a function | ||
into the chained API offered by **Great Tables**. | ||
|
||
Parameters | ||
---------- | ||
func | ||
A function that receives a GT object along with optional positional and keyword arguments, | ||
returning a GT object. | ||
|
||
*args | ||
Optional positional arguments to be passed to the function. | ||
|
||
**kwargs | ||
Optional keyword arguments to be passed to the function. | ||
|
||
Returns | ||
------- | ||
gt | ||
A GT object. | ||
|
||
Examples: | ||
------ | ||
Let's use the "name", "land_area_km2," and "density_2021" columns of the `towny` dataset to | ||
create a table. First, we'll demonstrate using two consecutive calls to the `.tab_style()` | ||
method to highlight the maximum value of the "land_area_km2" column with "lightgray" and the | ||
maximum value of the "density_2021" column with "lightblue". | ||
|
||
```{python} | ||
import polars as pl | ||
from great_tables import GT, loc, style | ||
from great_tables.data import towny | ||
|
||
|
||
towny_mini = pl.from_pandas(towny).head(10) | ||
|
||
( | ||
GT( | ||
towny_mini[["name", "land_area_km2", "density_2021"]], | ||
rowname_col="name", | ||
) | ||
.tab_style( | ||
style=style.fill(color="lightgray"), | ||
locations=loc.body( | ||
columns="land_area_km2", | ||
rows=pl.col("land_area_km2").eq(pl.col("land_area_km2").max()), | ||
), | ||
) | ||
.tab_style( | ||
style=style.fill(color="lightblue"), | ||
locations=loc.body( | ||
columns="density_2021", | ||
rows=pl.col("density_2021").eq(pl.col("density_2021").max()), | ||
), | ||
) | ||
) | ||
``` | ||
|
||
Next, we'll demonstrate how to achieve the same result using the `.pipe()` method to | ||
programmatically style each column. | ||
|
||
```{python} | ||
columns = ["land_area_km2", "density_2021"] | ||
colors = ["lightgray", "lightblue"] | ||
|
||
|
||
def tbl_style(gtbl: GT, columns: list[str], colors: list[str]) -> GT: | ||
for column, color in zip(columns, colors): | ||
gtbl = gtbl.tab_style( | ||
style=style.fill(color=color), | ||
locations=loc.body(columns=column, rows=pl.col(column).eq(pl.col(column).max())), | ||
) | ||
return gtbl | ||
|
||
|
||
( | ||
GT( | ||
towny_mini[["name", "land_area_km2", "density_2021"]], | ||
rowname_col="name", | ||
).pipe(tbl_style, columns, colors) | ||
) | ||
``` | ||
""" | ||
return func(self, *args, **kwargs) | ||
|
||
|
||
def pipes(self: "GT", *funcs: Callable["GT", "GT"] | list[Callable["GT", "GT"]]) -> "GT": | ||
""" | ||
Provide a structured way to chain functions for a GT object. | ||
|
||
This function accepts multiple functions, each of which receives a GT object and returns a GT | ||
object. This allows users to easily integrate functions into the chained API offered by | ||
**Great Tables**. It serves as a helper function for chaining multiple functions at once. | ||
|
||
Parameters | ||
---------- | ||
*funcs | ||
Multiple functions or a list of functions, each receiving a GT object and returning a GT | ||
object. | ||
|
||
Returns | ||
------- | ||
gt | ||
A GT object. | ||
|
||
Examples: | ||
------ | ||
Let's use the "name", "land_area_km2," and "density_2021" columns of the `towny` dataset to | ||
create a table. First, we'll demonstrate using two consecutive calls to the `.tab_style()` | ||
method to highlight the maximum value of the "land_area_km2" column with "lightgray" and the | ||
maximum value of the "density_2021" column with "lightblue". | ||
|
||
```{python} | ||
import polars as pl | ||
from great_tables import GT, loc, style | ||
from great_tables.data import towny | ||
|
||
|
||
towny_mini = pl.from_pandas(towny).head(10) | ||
|
||
( | ||
GT( | ||
towny_mini[["name", "land_area_km2", "density_2021"]], | ||
rowname_col="name", | ||
) | ||
.tab_style( | ||
style=style.fill(color="lightgray"), | ||
locations=loc.body( | ||
columns="land_area_km2", | ||
rows=pl.col("land_area_km2").eq(pl.col("land_area_km2").max()), | ||
), | ||
) | ||
.tab_style( | ||
style=style.fill(color="lightblue"), | ||
locations=loc.body( | ||
columns="density_2021", | ||
rows=pl.col("density_2021").eq(pl.col("density_2021").max()), | ||
), | ||
) | ||
) | ||
``` | ||
|
||
Next, we'll demonstrate achieving the same result using the `.pipes()` method to | ||
programmatically style each column. You might find leveraging `partial` to bind the other | ||
parameters in advance handy. | ||
|
||
```{python} | ||
from functools import partial | ||
|
||
|
||
columns = ["land_area_km2", "density_2021"] | ||
colors = ["lightgray", "lightblue"] | ||
|
||
|
||
def tbl_style(gtbl: GT, column: str, color: str) -> GT: | ||
return gtbl.tab_style( | ||
style=style.fill(color=color), | ||
locations=loc.body(columns=column, rows=pl.col(column).eq(pl.col(column).max())), | ||
) | ||
|
||
|
||
( | ||
GT( | ||
towny_mini[["name", "land_area_km2", "density_2021"]], | ||
rowname_col="name", | ||
).pipes( | ||
*[partial(tbl_style, column=column, color=color) | ||
for column, color in zip(columns, colors)] | ||
) | ||
) | ||
``` | ||
|
||
Alternatively, you can collect all the functions in a list like this: | ||
|
||
```{python} | ||
( | ||
GT( | ||
towny_mini[["name", "land_area_km2", "density_2021"]], | ||
rowname_col="name", | ||
).pipes( | ||
[partial(tbl_style, column=column, color=color) | ||
for column, color in zip(columns, colors)] | ||
) | ||
) | ||
``` | ||
""" | ||
if isinstance(funcs[0], list) and len(funcs) == 1: | ||
funcs = funcs[0] | ||
for func in funcs: | ||
self = pipe(self, func) | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import polars as pl | ||
from great_tables import GT, loc, style | ||
from functools import partial | ||
|
||
|
||
def test_pipe(): | ||
columns = ["x", "y"] | ||
colors = ["lightgray", "lightblue"] | ||
df = pl.DataFrame(dict(zip(columns, [[1, 2, 3], [3, 2, 1]]))) | ||
|
||
gt1 = ( | ||
GT(df) | ||
.tab_style( | ||
style=style.fill(color=colors[0]), | ||
locations=loc.body( | ||
columns=columns[0], rows=pl.col(columns[0]).eq(pl.col(columns[0]).max()) | ||
), | ||
) | ||
.tab_style( | ||
style=style.fill(color=colors[1]), | ||
locations=loc.body( | ||
columns=columns[1], rows=pl.col(columns[1]).eq(pl.col(columns[1]).max()) | ||
), | ||
) | ||
) | ||
|
||
def tbl_style(gtbl: GT, columns: list[str], colors: list[str]) -> GT: | ||
for column, color in zip(columns, colors): | ||
gtbl = gtbl.tab_style( | ||
style=style.fill(color=color), | ||
locations=loc.body(columns=column, rows=pl.col(column).eq(pl.col(column).max())), | ||
) | ||
return gtbl | ||
|
||
gt2 = GT(df).pipe(tbl_style, columns, colors) # check *args | ||
gt3 = GT(df).pipe(tbl_style, columns=columns, colors=colors) # check **kwargs | ||
|
||
assert gt1._styles == gt2._styles == gt3._styles | ||
|
||
|
||
def test_pipes(): | ||
columns = ["x", "y"] | ||
colors = ["lightgray", "lightblue"] | ||
df = pl.DataFrame(dict(zip(columns, [[1, 2, 3], [3, 2, 1]]))) | ||
|
||
gt1 = ( | ||
GT(df) | ||
.tab_style( | ||
style=style.fill(color=colors[0]), | ||
locations=loc.body( | ||
columns=columns[0], rows=pl.col(columns[0]).eq(pl.col(columns[0]).max()) | ||
), | ||
) | ||
.tab_style( | ||
style=style.fill(color=colors[1]), | ||
locations=loc.body( | ||
columns=columns[1], rows=pl.col(columns[1]).eq(pl.col(columns[1]).max()) | ||
), | ||
) | ||
) | ||
|
||
def tbl_style(gtbl: GT, column: str, color: str) -> GT: | ||
gtbl = gtbl.tab_style( | ||
style=style.fill(color=color), | ||
locations=loc.body(columns=column, rows=pl.col(column).eq(pl.col(column).max())), | ||
) | ||
return gtbl | ||
|
||
gt2 = GT(df).pipes( | ||
*[partial(tbl_style, column=column, color=color) for column, color in zip(columns, colors)] | ||
) | ||
|
||
gt3 = GT(df).pipes( | ||
[partial(tbl_style, column=column, color=color) for column, color in zip(columns, colors)] | ||
) | ||
|
||
assert gt1._styles == gt2._styles == gt3._styles |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I wonder if we could use ParamSpec here? Something like...
https://mypy-play.net/?mypy=latest&python=3.12&gist=23dd2d435071c6e9f5639cfcadf7dd16
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.
Of course! This is somewhat new to me, so please feel free to correct me if I've misunderstood anything.
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.
@machow and @rich-iannone, this PR has been pending for a while. Could we schedule it for merging, or are there any remaining concerns?