@@ -46,34 +46,64 @@ def render_html(
4646 dataframe : pd .DataFrame ,
4747 table_id : str ,
4848 orderable_columns : list [str ] | None = None ,
49+ max_columns : int | None = None ,
4950) -> str :
5051 """Render a pandas DataFrame to HTML with specific styling and nested data support."""
5152 # Flatten nested data first
5253 flatten_result = _flatten .flatten_nested_data (dataframe )
54+ flat_df = flatten_result .dataframe
5355
5456 orderable_columns = orderable_columns or []
5557 classes = "dataframe table table-striped table-hover"
5658 table_html_parts = [f'<table border="1" class="{ classes } " id="{ table_id } ">' ]
59+
60+ # Handle column truncation
61+ columns = list (flat_df .columns )
62+ if max_columns is not None and max_columns > 0 and len (columns ) > max_columns :
63+ half = max_columns // 2
64+ left_columns = columns [:half ]
65+ # Ensure we don't take more than available if half is 0 or calculation is weird,
66+ # but typical case is safe.
67+ right_count = max_columns - half
68+ right_columns = columns [- right_count :] if right_count > 0 else []
69+ show_ellipsis = True
70+ else :
71+ left_columns = columns
72+ right_columns = []
73+ show_ellipsis = False
74+
5775 table_html_parts .append (
58- _render_table_header (flatten_result .dataframe , orderable_columns )
76+ _render_table_header (
77+ flat_df , orderable_columns , left_columns , right_columns , show_ellipsis
78+ )
5979 )
6080 table_html_parts .append (
6181 _render_table_body (
62- flatten_result . dataframe ,
82+ flat_df ,
6383 flatten_result .row_labels ,
6484 flatten_result .continuation_rows ,
6585 flatten_result .cleared_on_continuation ,
6686 flatten_result .nested_columns ,
87+ left_columns ,
88+ right_columns ,
89+ show_ellipsis ,
6790 )
6891 )
6992 table_html_parts .append ("</table>" )
7093 return "" .join (table_html_parts )
7194
7295
73- def _render_table_header (dataframe : pd .DataFrame , orderable_columns : list [str ]) -> str :
96+ def _render_table_header (
97+ dataframe : pd .DataFrame ,
98+ orderable_columns : list [str ],
99+ left_columns : list [Any ],
100+ right_columns : list [Any ],
101+ show_ellipsis : bool ,
102+ ) -> str :
74103 """Render the header of the HTML table."""
75104 header_parts = [" <thead>" , " <tr>" ]
76- for col in dataframe .columns :
105+
106+ def render_col_header (col ):
77107 th_classes = []
78108 if col in orderable_columns :
79109 th_classes .append ("sortable" )
@@ -82,6 +112,18 @@ def _render_table_header(dataframe: pd.DataFrame, orderable_columns: list[str])
82112 f' <th { class_str } ><div class="bf-header-content">'
83113 f"{ html .escape (str (col ))} </div></th>"
84114 )
115+
116+ for col in left_columns :
117+ render_col_header (col )
118+
119+ if show_ellipsis :
120+ header_parts .append (
121+ ' <th><div class="bf-header-content" style="cursor: default;">...</div></th>'
122+ )
123+
124+ for col in right_columns :
125+ render_col_header (col )
126+
85127 header_parts .extend ([" </tr>" , " </thead>" ])
86128 return "\n " .join (header_parts )
87129
@@ -92,6 +134,9 @@ def _render_table_body(
92134 continuation_rows : set [int ] | None ,
93135 clear_on_continuation : list [str ],
94136 nested_originated_columns : set [str ],
137+ left_columns : list [Any ],
138+ right_columns : list [Any ],
139+ show_ellipsis : bool ,
95140) -> str :
96141 """Render the body of the HTML table."""
97142 body_parts = [" <tbody>" ]
@@ -117,7 +162,9 @@ def _render_table_body(
117162 body_parts .append (" <tr>" )
118163
119164 row = dataframe .iloc [i ]
120- for col_name , value in row .items ():
165+
166+ def render_col_cell (col_name ):
167+ value = row [col_name ]
121168 dtype = dataframe .dtypes .loc [col_name ] # type: ignore
122169 cell_html = _render_cell (
123170 value ,
@@ -129,6 +176,17 @@ def _render_table_body(
129176 precision ,
130177 )
131178 body_parts .append (cell_html )
179+
180+ for col in left_columns :
181+ render_col_cell (col )
182+
183+ if show_ellipsis :
184+ # Ellipsis cell
185+ body_parts .append (' <td class="cell-align-left">...</td>' )
186+
187+ for col in right_columns :
188+ render_col_cell (col )
189+
132190 body_parts .append (" </tr>" )
133191 body_parts .append (" </tbody>" )
134192 return "\n " .join (body_parts )
0 commit comments