Mutate the render dictionary to allow for tooltips: - Add ``<span>`` HTML element to each data cells ``display_value``. Ignores headers. - Add table level CSS styles to control pseudo classes. Parameters ---------- styler_data : DataFrame
(self, styler: StylerRenderer, d: dict)
| 2237 | ] |
| 2238 | |
| 2239 | def _translate(self, styler: StylerRenderer, d: dict): |
| 2240 | """ |
| 2241 | Mutate the render dictionary to allow for tooltips: |
| 2242 | |
| 2243 | - Add ``<span>`` HTML element to each data cells ``display_value``. Ignores |
| 2244 | headers. |
| 2245 | - Add table level CSS styles to control pseudo classes. |
| 2246 | |
| 2247 | Parameters |
| 2248 | ---------- |
| 2249 | styler_data : DataFrame |
| 2250 | Underlying ``Styler`` DataFrame used for reindexing. |
| 2251 | uuid : str |
| 2252 | The underlying ``Styler`` uuid for CSS id. |
| 2253 | d : dict |
| 2254 | The dictionary prior to final render |
| 2255 | |
| 2256 | Returns |
| 2257 | ------- |
| 2258 | render_dict : Dict |
| 2259 | """ |
| 2260 | self.tt_data = self.tt_data.reindex_like(styler.data) |
| 2261 | if self.tt_data.empty: |
| 2262 | return d |
| 2263 | |
| 2264 | mask = (self.tt_data.isna()) | (self.tt_data.eq("")) # empty string = no ttip |
| 2265 | # this conditional adds tooltips via pseudo css and <span> elements. |
| 2266 | if not self.as_title_attribute: |
| 2267 | name = self.class_name |
| 2268 | self.table_styles = [ |
| 2269 | style |
| 2270 | for sublist in [ |
| 2271 | self._pseudo_css( |
| 2272 | styler.uuid, name, i, j, str(self.tt_data.iloc[i, j]) |
| 2273 | ) |
| 2274 | for i in range(len(self.tt_data.index)) |
| 2275 | for j in range(len(self.tt_data.columns)) |
| 2276 | if not ( |
| 2277 | mask.iloc[i, j] |
| 2278 | or i in styler.hidden_rows |
| 2279 | or j in styler.hidden_columns |
| 2280 | ) |
| 2281 | ] |
| 2282 | for style in sublist |
| 2283 | ] |
| 2284 | |
| 2285 | # add span class to every cell since there is at least 1 non-empty tooltip |
| 2286 | if self.table_styles: |
| 2287 | for row in d["body"]: |
| 2288 | for item in row: |
| 2289 | if item["type"] == "td": |
| 2290 | item["display_value"] = ( |
| 2291 | str(item["display_value"]) |
| 2292 | + f'<span class="{self.class_name}"></span>' |
| 2293 | ) |
| 2294 | d["table_styles"].extend(self._class_styles) |
| 2295 | d["table_styles"].extend(self.table_styles) |
| 2296 | # this conditional adds tooltips as extra "title" attribute on a <td> element |