@@ -132,11 +132,33 @@ def _compute(self):
132
132
r = func (self )(* args , ** kwargs )
133
133
return r
134
134
135
- def _translate (self ):
135
+ def _translate (
136
+ self , sparsify_index : bool | None = None , sparsify_cols : bool | None = None
137
+ ):
136
138
"""
137
- Convert the DataFrame in `self.data` and the attrs from `_build_styles`
138
- into a dictionary of {head, body, uuid, cellstyle}.
139
+ Process Styler data and settings into a dict for template rendering.
140
+
141
+ Convert data and settings from ``Styler`` attributes such as ``self.data``,
142
+ ``self.tooltips`` including applying any methods in ``self._todo``.
143
+
144
+ Parameters
145
+ ----------
146
+ sparsify_index : bool, optional
147
+ Whether to sparsify the index or print all hierarchical index elements
148
+ sparsify_cols : bool, optional
149
+ Whether to sparsify the columns or print all hierarchical column elements
150
+
151
+ Returns
152
+ -------
153
+ d : dict
154
+ The following structure: {uuid, table_styles, caption, head, body,
155
+ cellstyle, table_attributes}
139
156
"""
157
+ if sparsify_index is None :
158
+ sparsify_index = get_option ("display.multi_sparse" )
159
+ if sparsify_cols is None :
160
+ sparsify_cols = get_option ("display.multi_sparse" )
161
+
140
162
ROW_HEADING_CLASS = "row_heading"
141
163
COL_HEADING_CLASS = "col_heading"
142
164
INDEX_NAME_CLASS = "index_name"
@@ -153,14 +175,14 @@ def _translate(self):
153
175
}
154
176
155
177
head = self ._translate_header (
156
- BLANK_CLASS , BLANK_VALUE , INDEX_NAME_CLASS , COL_HEADING_CLASS
178
+ BLANK_CLASS , BLANK_VALUE , INDEX_NAME_CLASS , COL_HEADING_CLASS , sparsify_cols
157
179
)
158
180
d .update ({"head" : head })
159
181
160
182
self .cellstyle_map : DefaultDict [tuple [CSSPair , ...], list [str ]] = defaultdict (
161
183
list
162
184
)
163
- body = self ._translate_body (DATA_CLASS , ROW_HEADING_CLASS )
185
+ body = self ._translate_body (DATA_CLASS , ROW_HEADING_CLASS , sparsify_index )
164
186
d .update ({"body" : body })
165
187
166
188
cellstyle : list [dict [str , CSSList | list [str ]]] = [
@@ -185,20 +207,47 @@ def _translate(self):
185
207
return d
186
208
187
209
def _translate_header (
188
- self , blank_class , blank_value , index_name_class , col_heading_class
210
+ self ,
211
+ blank_class : str ,
212
+ blank_value : str ,
213
+ index_name_class : str ,
214
+ col_heading_class : str ,
215
+ sparsify_cols : bool ,
189
216
):
190
217
"""
191
- Build each <tr> within table <head>, using the structure:
218
+ Build each <tr> within table <head> as a list
219
+
220
+ Using the structure:
192
221
+----------------------------+---------------+---------------------------+
193
222
| index_blanks ... | column_name_0 | column_headers (level_0) |
194
223
1) | .. | .. | .. |
195
224
| index_blanks ... | column_name_n | column_headers (level_n) |
196
225
+----------------------------+---------------+---------------------------+
197
226
2) | index_names (level_0 to level_n) ... | column_blanks ... |
198
227
+----------------------------+---------------+---------------------------+
228
+
229
+ Parameters
230
+ ----------
231
+ blank_class : str
232
+ CSS class added to elements within blank sections of the structure.
233
+ blank_value : str
234
+ HTML display value given to elements within blank sections of the structure.
235
+ index_name_class : str
236
+ CSS class added to elements within the index_names section of the structure.
237
+ col_heading_class : str
238
+ CSS class added to elements within the column_names section of structure.
239
+ sparsify_cols : bool
240
+ Whether column_headers section will add colspan attributes (>1) to elements.
241
+
242
+ Returns
243
+ -------
244
+ head : list
245
+ The associated HTML elements needed for template rendering.
199
246
"""
200
247
# for sparsifying a MultiIndex
201
- col_lengths = _get_level_lengths (self .columns , self .hidden_columns )
248
+ col_lengths = _get_level_lengths (
249
+ self .columns , sparsify_cols , self .hidden_columns
250
+ )
202
251
203
252
clabels = self .data .columns .tolist ()
204
253
if self .data .columns .nlevels == 1 :
@@ -268,18 +317,36 @@ def _translate_header(
268
317
269
318
return head
270
319
271
- def _translate_body (self , data_class , row_heading_class ):
320
+ def _translate_body (
321
+ self , data_class : str , row_heading_class : str , sparsify_index : bool
322
+ ):
272
323
"""
273
- Build each <tr> in table <body> in the following format:
324
+ Build each <tr> within table <body> as a list
325
+
326
+ Use the following structure:
274
327
+--------------------------------------------+---------------------------+
275
328
| index_header_0 ... index_header_n | data_by_column |
276
329
+--------------------------------------------+---------------------------+
277
330
278
331
Also add elements to the cellstyle_map for more efficient grouped elements in
279
332
<style></style> block
333
+
334
+ Parameters
335
+ ----------
336
+ data_class : str
337
+ CSS class added to elements within data_by_column sections of the structure.
338
+ row_heading_class : str
339
+ CSS class added to elements within the index_header section of structure.
340
+ sparsify_index : bool
341
+ Whether index_headers section will add rowspan attributes (>1) to elements.
342
+
343
+ Returns
344
+ -------
345
+ body : list
346
+ The associated HTML elements needed for template rendering.
280
347
"""
281
348
# for sparsifying a MultiIndex
282
- idx_lengths = _get_level_lengths (self .index )
349
+ idx_lengths = _get_level_lengths (self .index , sparsify_index )
283
350
284
351
rlabels = self .data .index .tolist ()
285
352
if self .data .index .nlevels == 1 :
@@ -520,14 +587,26 @@ def _element(
520
587
}
521
588
522
589
523
- def _get_level_lengths (index , hidden_elements = None ):
590
+ def _get_level_lengths (
591
+ index : Index , sparsify : bool , hidden_elements : Sequence [int ] | None = None
592
+ ):
524
593
"""
525
594
Given an index, find the level length for each element.
526
595
527
- Optional argument is a list of index positions which
528
- should not be visible.
596
+ Parameters
597
+ ----------
598
+ index : Index
599
+ Index or columns to determine lengths of each element
600
+ sparsify : bool
601
+ Whether to hide or show each distinct element in a MultiIndex
602
+ hidden_elements : sequence of int
603
+ Index positions of elements hidden from display in the index affecting
604
+ length
529
605
530
- Result is a dictionary of (level, initial_position): span
606
+ Returns
607
+ -------
608
+ Dict :
609
+ Result is a dictionary of (level, initial_position): span
531
610
"""
532
611
if isinstance (index , MultiIndex ):
533
612
levels = index .format (sparsify = lib .no_default , adjoin = False )
@@ -546,7 +625,7 @@ def _get_level_lengths(index, hidden_elements=None):
546
625
547
626
for i , lvl in enumerate (levels ):
548
627
for j , row in enumerate (lvl ):
549
- if not get_option ( "display.multi_sparse" ) :
628
+ if not sparsify :
550
629
lengths [(i , j )] = 1
551
630
elif (row is not lib .no_default ) and (j not in hidden_elements ):
552
631
last_label = j
0 commit comments