77import warnings
88from dataclasses import dataclass
99from pathlib import Path
10- from typing import TYPE_CHECKING , Any , Callable , Optional , Protocol , Union
10+ from typing import (
11+ TYPE_CHECKING ,
12+ Any ,
13+ Callable ,
14+ Optional ,
15+ Protocol ,
16+ Union ,
17+ )
1118
1219import chatlas
1320import chevron
@@ -217,8 +224,16 @@ def df_to_html(df: IntoFrame, maxrows: int = 5) -> str:
217224 HTML string representation of the table
218225
219226 """
220- ndf = nw .from_native (df )
221- df_short = nw .from_native (df ).head (maxrows )
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 )
235+ else :
236+ raise TypeError ("df must be a Narwhals DataFrame or LazyFrame" )
222237
223238 # Generate HTML table
224239 table_html = df_short .to_pandas ().to_html (
@@ -227,9 +242,9 @@ def df_to_html(df: IntoFrame, maxrows: int = 5) -> str:
227242 )
228243
229244 # Add note about truncated rows if needed
230- if len (df_short ) != len (ndf ):
245+ if len (df_short ) != len (ndf_eager ):
231246 rows_notice = (
232- f"\n \n (Showing only the first { maxrows } rows out of { len (ndf )} .)\n "
247+ f"\n \n (Showing only the first { maxrows } rows out of { len (ndf_eager )} .)\n "
233248 )
234249 else :
235250 rows_notice = ""
@@ -400,11 +415,15 @@ def init(
400415 data_source_obj : DataSource
401416 if isinstance (data_source , sqlalchemy .Engine ):
402417 data_source_obj = SQLAlchemySource (data_source , table_name )
403- else :
418+ elif isinstance ( data_source , ( nw . DataFrame , nw . LazyFrame )) :
404419 data_source_obj = DataFrameSource (
405- nw .from_native (data_source ). to_pandas ( ),
420+ nw .to_native (data_source ),
406421 table_name ,
407422 )
423+ else :
424+ raise TypeError (
425+ "`data_source` must be a Narwhals DataFrame or LazyFrame, or a SQLAlchemy Engine" ,
426+ )
408427 # Process greeting
409428 if greeting is None :
410429 print (
0 commit comments