Skip to content

Commit

Permalink
fix: support pl.UInt type in nanoplot (#577)
Browse files Browse the repository at this point in the history
* Accept `pl.UInt` type in nanoplot

* Fix alignment for `pl.UInt` type

---------

Co-authored-by: Richard Iannone <[email protected]>
  • Loading branch information
jrycw and rich-iannone authored Jan 14, 2025
1 parent 06d11d9 commit 431bc03
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
6 changes: 5 additions & 1 deletion great_tables/_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -4601,7 +4601,11 @@ def fmt_nanoplot(

col_class = str(column_d_type).lower()

if _str_detect(col_class, "int") or _str_detect(col_class, "float"):
if (
_str_detect(col_class, "int")
or _str_detect(col_class, "uint")
or _str_detect(col_class, "float")
):
scalar_vals = True
else:
scalar_vals = False
Expand Down
6 changes: 5 additions & 1 deletion great_tables/_gt_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,11 @@ def align_from_data(self, data: TblData) -> Self:
align.append("left")
elif col_class == "string":
align.append("left")
elif _str_detect(col_class, "int") or _str_detect(col_class, "float"):
elif (
_str_detect(col_class, "int")
or _str_detect(col_class, "uint")
or _str_detect(col_class, "float")
):
align.append("right")
elif _str_detect(col_class, "date"):
align.append("right")
Expand Down
5 changes: 4 additions & 1 deletion tests/test__boxhead.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def test_cols_align_columns_list_of_str():


def test_cols_align_pl_expr():
df = pl.DataFrame({"col1": [1, 2], "col2": [3.3, 4.4], "c": ["x", "y"]})
df = pl.DataFrame({"col1": [1, 2], "col2": [3.3, 4.4], "c": ["x", "y"]}).with_columns(
pl.col("col1").cast(pl.UInt8).alias("d")
)
table = gt.GT(df)

# Select columns by polars expressions
Expand All @@ -110,6 +112,7 @@ def test_cols_align_pl_expr():
"center", # manually assign
"center", # manually assign
"left", # `auto_align` for `str` is "left"
"right", # `auto_align` for `pl.UInt` is "right"
]


Expand Down
12 changes: 12 additions & 0 deletions tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2236,6 +2236,18 @@ def test_fmt_nanoplot_multi_vals_bar_ref_line_ref_area():
)


def test_fmt_nanoplot_accept_pl_uint():
"""
https://github.com/posit-dev/great-tables/issues/571
"""
single_vals_df = pl.DataFrame({"bars": [1, 2, 3, 5]})

# Call `as_raw_html()` to trigger `_generate_nanoplot()` and expose the rendering issue.
single_vals_df.select("bars").cast(pl.UInt32).style.fmt_nanoplot(
columns="bars", plot_type="bar"
).as_raw_html()


@pytest.mark.parametrize(
"plot_type",
[
Expand Down

0 comments on commit 431bc03

Please sign in to comment.