@@ -64,6 +64,11 @@ class Styler(object):
6464 a unique identifier to avoid CSS collisions; generated automatically
6565 caption: str, default None
6666 caption to attach to the table
67+ cell_ids: bool, default True
68+ If True, each cell will have an ``id`` attribute in their HTML tag.
69+ The ``id`` takes the form ``T_<uuid>_row<num_row>_col<num_col>``
70+ where ``<uuid>`` is the unique identifier, ``<num_row>`` is the row
71+ number and ``<num_col>`` is the column number.
6772
6873 Attributes
6974 ----------
@@ -112,7 +117,7 @@ class Styler(object):
112117 template = env .get_template ("html.tpl" )
113118
114119 def __init__ (self , data , precision = None , table_styles = None , uuid = None ,
115- caption = None , table_attributes = None ):
120+ caption = None , table_attributes = None , cell_ids = True ):
116121 self .ctx = defaultdict (list )
117122 self ._todo = []
118123
@@ -136,6 +141,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
136141 self .table_attributes = table_attributes
137142 self .hidden_index = False
138143 self .hidden_columns = []
144+ self .cell_ids = cell_ids
139145
140146 # display_funcs maps (row, col) -> formatting function
141147
@@ -306,14 +312,16 @@ def format_attr(pair):
306312 cs .extend (cell_context .get ("data" , {}).get (r , {}).get (c , []))
307313 formatter = self ._display_funcs [(r , c )]
308314 value = self .data .iloc [r , c ]
309- row_es .append ({
310- "type" : "td" ,
311- "value" : value ,
312- "class" : " " .join (cs ),
313- "id" : "_" .join (cs [1 :]),
314- "display_value" : formatter (value ),
315- "is_visible" : (c not in hidden_columns )
316- })
315+ row_dict = {"type" : "td" ,
316+ "value" : value ,
317+ "class" : " " .join (cs ),
318+ "display_value" : formatter (value ),
319+ "is_visible" : (c not in hidden_columns )}
320+ # only add an id if the cell has a style
321+ if (self .cell_ids or
322+ not (len (ctx [r , c ]) == 1 and ctx [r , c ][0 ] == '' )):
323+ row_dict ["id" ] = "_" .join (cs [1 :])
324+ row_es .append (row_dict )
317325 props = []
318326 for x in ctx [r , c ]:
319327 # have to handle empty styles like ['']
0 commit comments