@@ -69,6 +69,9 @@ class Styler(object):
69
69
a unique identifier to avoid CSS collisons; generated automatically
70
70
caption: str, default None
71
71
caption to attach to the table
72
+ index_hidden: bool, default False
73
+ hides the index in the html output
74
+ .. versionadded:: 0.20.1
72
75
73
76
Attributes
74
77
----------
@@ -117,7 +120,8 @@ class Styler(object):
117
120
template = env .get_template ("html.tpl" )
118
121
119
122
def __init__ (self , data , precision = None , table_styles = None , uuid = None ,
120
- caption = None , table_attributes = None ):
123
+ caption = None , table_attributes = None , index_hidden = False ,
124
+ hidden_cols = None ):
121
125
self .ctx = defaultdict (list )
122
126
self ._todo = []
123
127
@@ -139,6 +143,11 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
139
143
precision = get_option ('display.precision' )
140
144
self .precision = precision
141
145
self .table_attributes = table_attributes
146
+ self .index_hidden = index_hidden
147
+ if hidden_cols is None :
148
+ hidden_cols = []
149
+ self .hidden_cols = hidden_cols
150
+
142
151
# display_funcs maps (row, col) -> formatting function
143
152
144
153
def default_display_func (x ):
@@ -186,6 +195,8 @@ def _translate(self):
186
195
caption = self .caption
187
196
ctx = self .ctx
188
197
precision = self .precision
198
+ index_hidden = self .index_hidden
199
+ hidden_cols = self .hidden_cols
189
200
uuid = self .uuid or str (uuid1 ()).replace ("-" , "_" )
190
201
ROW_HEADING_CLASS = "row_heading"
191
202
COL_HEADING_CLASS = "col_heading"
@@ -200,7 +211,7 @@ def format_attr(pair):
200
211
201
212
# for sparsifying a MultiIndex
202
213
idx_lengths = _get_level_lengths (self .index )
203
- col_lengths = _get_level_lengths (self .columns )
214
+ col_lengths = _get_level_lengths (self .columns , hidden_cols )
204
215
205
216
cell_context = dict ()
206
217
@@ -223,7 +234,7 @@ def format_attr(pair):
223
234
row_es = [{"type" : "th" ,
224
235
"value" : BLANK_VALUE ,
225
236
"display_value" : BLANK_VALUE ,
226
- "is_visible" : True ,
237
+ "is_visible" : ( not index_hidden ) ,
227
238
"class" : " " .join ([BLANK_CLASS ])}] * (n_rlvls - 1 )
228
239
229
240
# ... except maybe the last for columns.names
@@ -235,7 +246,7 @@ def format_attr(pair):
235
246
"value" : name ,
236
247
"display_value" : name ,
237
248
"class" : " " .join (cs ),
238
- "is_visible" : True })
249
+ "is_visible" : ( not index_hidden ) })
239
250
240
251
for c , value in enumerate (clabels [r ]):
241
252
cs = [COL_HEADING_CLASS , "level%s" % r , "col%s" % c ]
@@ -256,8 +267,8 @@ def format_attr(pair):
256
267
row_es .append (es )
257
268
head .append (row_es )
258
269
259
- if self .data .index .names and not all ( x is None
260
- for x in self .data .index .names ):
270
+ if self .data .index .names and not index_hidden \
271
+ and not all ( x is None for x in self .data .index .names ):
261
272
index_header_row = []
262
273
263
274
for c , name in enumerate (self .data .index .names ):
@@ -271,7 +282,7 @@ def format_attr(pair):
271
282
[{"type" : "th" ,
272
283
"value" : BLANK_VALUE ,
273
284
"class" : " " .join ([BLANK_CLASS ])
274
- }] * len (clabels [0 ]))
285
+ }] * ( len (clabels [0 ]) - len ( hidden_cols ) ))
275
286
276
287
head .append (index_header_row )
277
288
@@ -281,7 +292,8 @@ def format_attr(pair):
281
292
for c , value in enumerate (rlabels [r ]):
282
293
es = {
283
294
"type" : "th" ,
284
- "is_visible" : _is_visible (r , c , idx_lengths ),
295
+ "is_visible" : (_is_visible (r , c , idx_lengths ) &
296
+ (~ index_hidden )),
285
297
"value" : value ,
286
298
"display_value" : value ,
287
299
"class" : " " .join ([ROW_HEADING_CLASS , "level%s" % c ,
@@ -304,7 +316,8 @@ def format_attr(pair):
304
316
"value" : value ,
305
317
"class" : " " .join (cs ),
306
318
"id" : "_" .join (cs [1 :]),
307
- "display_value" : formatter (value )
319
+ "display_value" : formatter (value ),
320
+ "is_visible" : (c not in hidden_cols )
308
321
})
309
322
props = []
310
323
for x in ctx [r , c ]:
@@ -460,7 +473,9 @@ def _update_ctx(self, attrs):
460
473
def _copy (self , deepcopy = False ):
461
474
styler = Styler (self .data , precision = self .precision ,
462
475
caption = self .caption , uuid = self .uuid ,
463
- table_styles = self .table_styles )
476
+ table_styles = self .table_styles ,
477
+ index_hidden = self .index_hidden ,
478
+ hidden_cols = self .hidden_cols )
464
479
if deepcopy :
465
480
styler .ctx = copy .deepcopy (self .ctx )
466
481
styler ._todo = copy .deepcopy (self ._todo )
@@ -716,7 +731,7 @@ def set_uuid(self, uuid):
716
731
717
732
def set_caption (self , caption ):
718
733
"""
719
- Se the caption on a Styler
734
+ Set the caption on a Styler
720
735
721
736
.. versionadded:: 0.17.1
722
737
@@ -762,6 +777,41 @@ def set_table_styles(self, table_styles):
762
777
self .table_styles = table_styles
763
778
return self
764
779
780
+ def hide_index (self ):
781
+ """
782
+ Hide any indices from rendering.
783
+
784
+ .. versionadded:: 0.20.1
785
+
786
+ Returns
787
+ -------
788
+ self : Styler
789
+ """
790
+ self .index_hidden = True
791
+ return self
792
+
793
+ def hide_columns (self , subset ):
794
+ """
795
+ Hide columns from rendering.
796
+
797
+ .. versionadded:: 0.20.1
798
+
799
+ Parameters
800
+ ----------
801
+
802
+ subset: IndexSlice
803
+ An argument to ``DataFrame.loc`` that identifies which columns
804
+ are hidden.
805
+
806
+ Returns
807
+ -------
808
+ self : Styler
809
+ """
810
+ subset = _non_reducing_slice (subset )
811
+ hidden_df = self .data .loc [subset ]
812
+ self .hidden_cols = self .columns .get_indexer_for (hidden_df .columns )
813
+ return self
814
+
765
815
# -----------------------------------------------------------------------
766
816
# A collection of "builtin" styles
767
817
# -----------------------------------------------------------------------
@@ -1009,31 +1059,48 @@ def _is_visible(idx_row, idx_col, lengths):
1009
1059
return (idx_col , idx_row ) in lengths
1010
1060
1011
1061
1012
- def _get_level_lengths (index ):
1062
+ def _get_level_lengths (index , hidden_elements = None ):
1013
1063
"""
1014
1064
Given an index, find the level lenght for each element.
1065
+ Optional argument is a list of index positions which
1066
+ should not be visible.
1015
1067
1016
1068
Result is a dictionary of (level, inital_position): span
1017
1069
"""
1018
1070
sentinel = com .sentinel_factory ()
1019
1071
levels = index .format (sparsify = sentinel , adjoin = False , names = False )
1020
1072
1021
- if index . nlevels == 1 :
1022
- return {( 0 , i ): 1 for i , value in enumerate ( levels )}
1073
+ if hidden_elements is None :
1074
+ hidden_elements = []
1023
1075
1024
1076
lengths = {}
1077
+ if index .nlevels == 1 :
1078
+ for i , value in enumerate (levels ):
1079
+ if (i not in hidden_elements ):
1080
+ lengths [(0 , i )] = 1
1081
+ return lengths
1025
1082
1026
1083
for i , lvl in enumerate (levels ):
1027
1084
for j , row in enumerate (lvl ):
1028
1085
if not get_option ('display.multi_sparse' ):
1029
1086
lengths [(i , j )] = 1
1030
- elif row != sentinel :
1087
+ elif ( row != sentinel ) and ( j not in hidden_elements ) :
1031
1088
last_label = j
1032
1089
lengths [(i , last_label )] = 1
1033
- else :
1090
+ elif (row != sentinel ):
1091
+ # even if its hidden, keep track of it in case
1092
+ # length >1 and later elemens are visible
1093
+ last_label = j
1094
+ lengths [(i , last_label )] = 0
1095
+ elif (j not in hidden_elements ):
1034
1096
lengths [(i , last_label )] += 1
1035
1097
1036
- return lengths
1098
+ non_zero_lengths = {}
1099
+ for element , length in lengths .items ():
1100
+ if (length >= 1 ):
1101
+ non_zero_lengths [element ] = length
1102
+
1103
+ return non_zero_lengths
1037
1104
1038
1105
1039
1106
def _maybe_wrap_formatter (formatter ):
0 commit comments