@@ -70,6 +70,9 @@ class Styler(object):
70
70
a unique identifier to avoid CSS collisons; generated automatically
71
71
caption: str, default None
72
72
caption to attach to the table
73
+ index_hidden: bool, default False
74
+ hides the index in the html output
75
+ .. versionadded:: 0.20.1
73
76
74
77
Attributes
75
78
----------
@@ -118,7 +121,8 @@ class Styler(object):
118
121
template = env .get_template ("html.tpl" )
119
122
120
123
def __init__ (self , data , precision = None , table_styles = None , uuid = None ,
121
- caption = None , table_attributes = None ):
124
+ caption = None , table_attributes = None , index_hidden = False ,
125
+ hidden_cols = None ):
122
126
self .ctx = defaultdict (list )
123
127
self ._todo = []
124
128
@@ -140,6 +144,11 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
140
144
precision = get_option ('display.precision' )
141
145
self .precision = precision
142
146
self .table_attributes = table_attributes
147
+ self .index_hidden = index_hidden
148
+ if hidden_cols is None :
149
+ hidden_cols = []
150
+ self .hidden_cols = hidden_cols
151
+
143
152
# display_funcs maps (row, col) -> formatting function
144
153
145
154
def default_display_func (x ):
@@ -187,6 +196,8 @@ def _translate(self):
187
196
caption = self .caption
188
197
ctx = self .ctx
189
198
precision = self .precision
199
+ index_hidden = self .index_hidden
200
+ hidden_cols = self .hidden_cols
190
201
uuid = self .uuid or str (uuid1 ()).replace ("-" , "_" )
191
202
ROW_HEADING_CLASS = "row_heading"
192
203
COL_HEADING_CLASS = "col_heading"
@@ -201,7 +212,7 @@ def format_attr(pair):
201
212
202
213
# for sparsifying a MultiIndex
203
214
idx_lengths = _get_level_lengths (self .index )
204
- col_lengths = _get_level_lengths (self .columns )
215
+ col_lengths = _get_level_lengths (self .columns , hidden_cols )
205
216
206
217
cell_context = dict ()
207
218
@@ -224,7 +235,7 @@ def format_attr(pair):
224
235
row_es = [{"type" : "th" ,
225
236
"value" : BLANK_VALUE ,
226
237
"display_value" : BLANK_VALUE ,
227
- "is_visible" : True ,
238
+ "is_visible" : ( not index_hidden ) ,
228
239
"class" : " " .join ([BLANK_CLASS ])}] * (n_rlvls - 1 )
229
240
230
241
# ... except maybe the last for columns.names
@@ -236,7 +247,7 @@ def format_attr(pair):
236
247
"value" : name ,
237
248
"display_value" : name ,
238
249
"class" : " " .join (cs ),
239
- "is_visible" : True })
250
+ "is_visible" : ( not index_hidden ) })
240
251
241
252
for c , value in enumerate (clabels [r ]):
242
253
cs = [COL_HEADING_CLASS , "level%s" % r , "col%s" % c ]
@@ -257,8 +268,8 @@ def format_attr(pair):
257
268
row_es .append (es )
258
269
head .append (row_es )
259
270
260
- if self .data .index .names and not all ( x is None
261
- for x in self .data .index .names ):
271
+ if self .data .index .names and not index_hidden \
272
+ and not all ( x is None for x in self .data .index .names ):
262
273
index_header_row = []
263
274
264
275
for c , name in enumerate (self .data .index .names ):
@@ -272,7 +283,7 @@ def format_attr(pair):
272
283
[{"type" : "th" ,
273
284
"value" : BLANK_VALUE ,
274
285
"class" : " " .join ([BLANK_CLASS ])
275
- }] * len (clabels [0 ]))
286
+ }] * ( len (clabels [0 ]) - len ( hidden_cols ) ))
276
287
277
288
head .append (index_header_row )
278
289
@@ -282,7 +293,8 @@ def format_attr(pair):
282
293
for c , value in enumerate (rlabels [r ]):
283
294
es = {
284
295
"type" : "th" ,
285
- "is_visible" : _is_visible (r , c , idx_lengths ),
296
+ "is_visible" : (_is_visible (r , c , idx_lengths ) &
297
+ (~ index_hidden )),
286
298
"value" : value ,
287
299
"display_value" : value ,
288
300
"class" : " " .join ([ROW_HEADING_CLASS , "level%s" % c ,
@@ -305,7 +317,8 @@ def format_attr(pair):
305
317
"value" : value ,
306
318
"class" : " " .join (cs ),
307
319
"id" : "_" .join (cs [1 :]),
308
- "display_value" : formatter (value )
320
+ "display_value" : formatter (value ),
321
+ "is_visible" : (c not in hidden_cols )
309
322
})
310
323
props = []
311
324
for x in ctx [r , c ]:
@@ -461,7 +474,9 @@ def _update_ctx(self, attrs):
461
474
def _copy (self , deepcopy = False ):
462
475
styler = Styler (self .data , precision = self .precision ,
463
476
caption = self .caption , uuid = self .uuid ,
464
- table_styles = self .table_styles )
477
+ table_styles = self .table_styles ,
478
+ index_hidden = self .index_hidden ,
479
+ hidden_cols = self .hidden_cols )
465
480
if deepcopy :
466
481
styler .ctx = copy .deepcopy (self .ctx )
467
482
styler ._todo = copy .deepcopy (self ._todo )
@@ -717,7 +732,7 @@ def set_uuid(self, uuid):
717
732
718
733
def set_caption (self , caption ):
719
734
"""
720
- Se the caption on a Styler
735
+ Set the caption on a Styler
721
736
722
737
.. versionadded:: 0.17.1
723
738
@@ -763,6 +778,41 @@ def set_table_styles(self, table_styles):
763
778
self .table_styles = table_styles
764
779
return self
765
780
781
+ def hide_index (self ):
782
+ """
783
+ Hide any indices from rendering.
784
+
785
+ .. versionadded:: 0.20.1
786
+
787
+ Returns
788
+ -------
789
+ self : Styler
790
+ """
791
+ self .index_hidden = True
792
+ return self
793
+
794
+ def hide_columns (self , subset ):
795
+ """
796
+ Hide columns from rendering.
797
+
798
+ .. versionadded:: 0.20.1
799
+
800
+ Parameters
801
+ ----------
802
+
803
+ subset: IndexSlice
804
+ An argument to ``DataFrame.loc`` that identifies which columns
805
+ are hidden.
806
+
807
+ Returns
808
+ -------
809
+ self : Styler
810
+ """
811
+ subset = _non_reducing_slice (subset )
812
+ hidden_df = self .data .loc [subset ]
813
+ self .hidden_cols = self .columns .get_indexer_for (hidden_df .columns )
814
+ return self
815
+
766
816
# -----------------------------------------------------------------------
767
817
# A collection of "builtin" styles
768
818
# -----------------------------------------------------------------------
@@ -1147,31 +1197,48 @@ def _is_visible(idx_row, idx_col, lengths):
1147
1197
return (idx_col , idx_row ) in lengths
1148
1198
1149
1199
1150
- def _get_level_lengths (index ):
1200
+ def _get_level_lengths (index , hidden_elements = None ):
1151
1201
"""
1152
1202
Given an index, find the level lenght for each element.
1203
+ Optional argument is a list of index positions which
1204
+ should not be visible.
1153
1205
1154
1206
Result is a dictionary of (level, inital_position): span
1155
1207
"""
1156
1208
sentinel = com .sentinel_factory ()
1157
1209
levels = index .format (sparsify = sentinel , adjoin = False , names = False )
1158
1210
1159
- if index . nlevels == 1 :
1160
- return {( 0 , i ): 1 for i , value in enumerate ( levels )}
1211
+ if hidden_elements is None :
1212
+ hidden_elements = []
1161
1213
1162
1214
lengths = {}
1215
+ if index .nlevels == 1 :
1216
+ for i , value in enumerate (levels ):
1217
+ if (i not in hidden_elements ):
1218
+ lengths [(0 , i )] = 1
1219
+ return lengths
1163
1220
1164
1221
for i , lvl in enumerate (levels ):
1165
1222
for j , row in enumerate (lvl ):
1166
1223
if not get_option ('display.multi_sparse' ):
1167
1224
lengths [(i , j )] = 1
1168
- elif row != sentinel :
1225
+ elif ( row != sentinel ) and ( j not in hidden_elements ) :
1169
1226
last_label = j
1170
1227
lengths [(i , last_label )] = 1
1171
- else :
1228
+ elif (row != sentinel ):
1229
+ # even if its hidden, keep track of it in case
1230
+ # length >1 and later elemens are visible
1231
+ last_label = j
1232
+ lengths [(i , last_label )] = 0
1233
+ elif (j not in hidden_elements ):
1172
1234
lengths [(i , last_label )] += 1
1173
1235
1174
- return lengths
1236
+ non_zero_lengths = {}
1237
+ for element , length in lengths .items ():
1238
+ if (length >= 1 ):
1239
+ non_zero_lengths [element ] = length
1240
+
1241
+ return non_zero_lengths
1175
1242
1176
1243
1177
1244
def _maybe_wrap_formatter (formatter ):
0 commit comments