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

Add val_fmt_image() to enable image rendering in various components #451

Merged
merged 4 commits into from
Sep 30, 2024

Conversation

jrycw
Copy link
Collaborator

@jrycw jrycw commented Sep 18, 2024

Related to #418.

It seems that users are interested in rendering images across different components. To support this, I'd like to propose introducing a val_fmt_image() function. With this function, we can achieve more flexibility, such as:

import polars as pl
from polars import selectors as cs
from great_tables import GT, vals, html
from importlib_resources import files


img_paths = files("great_tables") / "data/metro_images"

df = pl.DataFrame(
    {
        "col1": list("123"),
        "col2": list("123"),
        "col3": list("123"),
        "group": ["grp_a", "grp_a", "grp_b"],
        "row": ["row_1", "row_2", "row_3"],
    }
)

img1, img2, img3, img4, img5, img6 = vals.fmt_image(
    list("123456"), path=img_paths, file_pattern="metro_{}.svg"
)

(
    GT(df)
    .cols_label(**{"col1": html(img1), "col2": html(img2), "col3": html(img3)})
    .tab_stub(groupname_col="group", rowname_col="row")
    .tab_stubhead(html(img4))
    .tab_header(title=html(img5))
    .tab_source_note(html(img6))
    .fmt_image(cs.starts_with("col"), path=img_paths, file_pattern="metro_{}.svg")
)

image

The idea is to wrap the output from val_fmt_image() in the html() function.

Copy link

codecov bot commented Sep 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.84%. Comparing base (c8a7ff8) to head (60d81b8).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #451      +/-   ##
==========================================
+ Coverage   87.70%   87.84%   +0.13%     
==========================================
  Files          42       42              
  Lines        4832     4838       +6     
==========================================
+ Hits         4238     4250      +12     
+ Misses        594      588       -6     

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

@rich-iannone rich-iannone self-requested a review September 18, 2024 16:10
@rich-iannone
Copy link
Member

This will be a great addition, thank you! If you can, could you add some basic tests for this function in a new test_formats_vals.py file? I know we aren't directly testing the other val_fmt_* functions right now but this will get the process started.

@jrycw
Copy link
Collaborator Author

jrycw commented Sep 19, 2024

Sure. Since we have actual SVG files in the project, I thought it might be a good idea to directly test val_fmt_image() using those images. Feel free to update the tests if they need further refinement.

@Yasin197
Copy link

This would be an amazing feature, when do you think it will be released?

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!

@rich-iannone
Copy link
Member

Sorry for taking awhile to get back to this PR. Just reviewed again, did some testing, and everything is really good here!

@machow machow merged commit 908ed6f into posit-dev:main Sep 30, 2024
13 checks passed
@jrycw jrycw deleted the val_fmt_image branch October 1, 2024 00:52
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.

4 participants