Skip to content

Commit c0d334a

Browse files
authored
feat(data frame): If a column is marked as str, make sure it is not HTML (#1310)
1 parent b46706a commit c0d334a

File tree

2 files changed

+18
-17
lines changed
  • shiny/render/_data_frame_utils
  • tests/playwright/shiny/components/data_frame/edit

2 files changed

+18
-17
lines changed

shiny/render/_data_frame_utils/_unsafe.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def serialize_numpy_dtypes(df: "pd.DataFrame") -> list[dict[str, Any]]:
2626
return [serialize_numpy_dtype(col) for _, col in df.items()]
2727

2828

29+
def col_contains_shiny_html(col: "pd.Series") -> bool:
30+
return any(is_shiny_html(val) for _, val in enumerate(col))
31+
32+
2933
def serialize_numpy_dtype(
3034
col: "pd.Series",
3135
) -> dict[str, Any]:
@@ -39,20 +43,20 @@ def serialize_numpy_dtype(
3943
res: dict[str, Any] = {}
4044

4145
if t == "string":
42-
pass
46+
if col_contains_shiny_html(col):
47+
t = "html"
48+
else:
49+
pass
50+
# If no HTML (which is a str) is found, then it is a string! (Keep t as `"string"`)
4351
elif t in ["bytes", "floating", "integer", "decimal", "mixed-integer-float"]:
4452
t = "numeric"
4553
elif t == "categorical":
4654
res["categories"] = [str(x) for x in col.cat.categories.to_list()]
4755
else:
48-
t = "unknown"
49-
for _, val in enumerate(col):
50-
# if isinstance(val, TagNode):
51-
52-
if is_shiny_html(val):
53-
# If they do, mark the column and extra htmldeps
54-
t = "html"
55-
break
56+
if col_contains_shiny_html(col):
57+
t = "html"
58+
else:
59+
t = "unknown"
5660

5761
res["type"] = t
5862

tests/playwright/shiny/components/data_frame/edit/app.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,15 @@
4949

5050

5151
# Add some HTML content to the dataframe!
52-
for i in range(1, 5):
53-
df.iloc[i, 1] = ui.p(
54-
ui.HTML(
52+
df["Sample Number"] = df["Sample Number"].apply(
53+
lambda x: ui.HTML( # pyright: ignore[reportUnknownLambdaType]
54+
str(
5555
ui.tags.strong(
56-
ui.tags.em(
57-
str(
58-
df.iloc[i, 1],
59-
)
60-
)
56+
ui.tags.em(str(x)) # pyright: ignore[reportUnknownArgumentType]
6157
)
6258
)
6359
)
60+
)
6461

6562

6663
# Use a non-standard index, just in case

0 commit comments

Comments
 (0)