Skip to content

Commit 47eee83

Browse files
authored
chore(pkg-py): Use .lazy() for consistent head/nrow (#74)
1 parent 53ee1f6 commit 47eee83

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

pkg-py/src/querychat/querychat.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,9 @@ def df_to_html(df: IntoFrame, maxrows: int = 5) -> str:
224224
HTML string representation of the table
225225
226226
"""
227-
df_short: nw.DataFrame[Any]
228-
229-
if isinstance(df, nw.LazyFrame):
230-
ndf_eager = df.collect()
231-
df_short = df.head(maxrows).collect()
232-
elif isinstance(df, nw.DataFrame):
233-
ndf_eager = df
234-
df_short = df.head(maxrows)
227+
if isinstance(df, (nw.LazyFrame, nw.DataFrame)):
228+
df_short = df.lazy().head(maxrows).collect()
229+
nrow_full = df.lazy().select(nw.len()).collect().item()
235230
else:
236231
raise TypeError("df must be a Narwhals DataFrame or LazyFrame")
237232

@@ -242,9 +237,9 @@ def df_to_html(df: IntoFrame, maxrows: int = 5) -> str:
242237
)
243238

244239
# Add note about truncated rows if needed
245-
if len(df_short) != len(ndf_eager):
240+
if len(df_short) != nrow_full:
246241
rows_notice = (
247-
f"\n\n(Showing only the first {maxrows} rows out of {len(ndf_eager)}.)\n"
242+
f"\n\n(Showing only the first {maxrows} rows out of {nrow_full}.)\n"
248243
)
249244
else:
250245
rows_notice = ""

0 commit comments

Comments
 (0)