Skip to content

Commit a5714e4

Browse files
committed
Partial for narwhals serialize frame. Still needs work. Need datetime type
1 parent 271ec5f commit a5714e4

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

shiny/render/_data_frame_utils/_pandas.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def wrap_shiny_html_with_session(x: TagNode):
8080

8181
res["typeHints"] = type_hints
8282

83+
if "index" in res:
84+
del res["index"]
85+
8386
# print(json.dumps(res, indent=4))
8487
return res
8588

shiny/render/_data_frame_utils/_tbl_data.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ def _(data: PdDataFrame) -> FrameJson:
244244

245245

246246
# TODO: test this
247-
@serialize_frame.register(nw.DataFrame)
248247
@serialize_frame.register
249248
def _(data: PlDataFrame) -> FrameJson:
250249
import json
@@ -276,6 +275,51 @@ def wrap_shiny_html_with_session(x: TagNode):
276275
}
277276

278277

278+
@serialize_frame.register(nw.DataFrame)
279+
def _(data: nw.DataFrame[Any]) -> FrameJson:
280+
import json
281+
282+
type_hints = [serialize_dtype(data[col_name]) for col_name in data.columns]
283+
type_hints_type = {type_hint["type"] for type_hint in type_hints}
284+
285+
data_rows = data.rows(named=False)
286+
287+
print(data_rows)
288+
print(data.rows(named=False))
289+
290+
# Shiny tag support
291+
if "html" in type_hints_type:
292+
session = require_active_session(None)
293+
294+
def wrap_shiny_html_with_session(x: TagNode):
295+
return maybe_as_cell_html(x, session=session)
296+
297+
html_column_positions = [
298+
i for i, x in enumerate(type_hints_type) if x == "html"
299+
]
300+
301+
new_rows: list[tuple[Any, ...]] = []
302+
303+
# Wrap the corresponding columns with the cell html object
304+
for row in data_rows:
305+
new_row = list(row)
306+
for html_column_position in html_column_positions:
307+
new_row[html_column_position] = wrap_shiny_html_with_session(
308+
new_row[html_column_position]
309+
)
310+
new_rows.append(tuple(new_row))
311+
312+
data_rows = new_rows
313+
314+
data_val = json.loads(json.dumps(data_rows, default=str))
315+
316+
return {
317+
"columns": data.columns,
318+
"data": data_val,
319+
"typeHints": type_hints,
320+
}
321+
322+
279323
# subset_frame -------------------------------------------------------------------------
280324
def subset_frame__typed(
281325
data: DataFrameLikeT, *, rows: RowsList = None, cols: ColsList = None

tests/pytest/test_render_data_frame_tbl_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def assert_frame_equal2(
123123
(pl.Series([1]), "numeric"),
124124
(pl.Series([1.1]), "numeric"),
125125
(pl.Series(["a"]), "string"),
126-
(pl.Series([datetime.now()]), "unknown"),
126+
(pl.Series([datetime.now()]), "datetime"),
127127
(pl.Series(["a"], dtype=pl.Categorical), "categorical"),
128128
(pl.Series([{"x": 1}]), "unknown"),
129129
(pl.Series([h1("yo")]), "html"),
@@ -163,7 +163,7 @@ def test_serialize_frame(df_f: DataFrameLike):
163163
res = serialize_frame(df_f)
164164
assert res == {
165165
"columns": ["num", "chr", "cat", "dt", "struct", "arr", "object"],
166-
"index": [0, 1],
166+
# "index": [0, 1],
167167
"data": [
168168
[1, "a", "a", "2000-01-02T00:00:00.000", {"x": 1}, [1, 2], "<C object>"],
169169
[2, "b", "a", "2000-01-02T00:00:00.000", {"x": 2}, [3, 4], "D(y=2)"],

0 commit comments

Comments
 (0)