|
243 | 243 | "Table styles are flexible enough to control all individual parts of the table, including column headers and indexes. \n",
|
244 | 244 | "However, they can be unwieldy to type for individual data cells or for any kind of conditional formatting, so we recommend that table styles are used for broad styling, such as entire rows or columns at a time.\n",
|
245 | 245 | "\n",
|
246 |
| - "Table styles are also used to control features which can apply to the whole table at once such as greating a generic hover functionality. The `:hover` pseudo-selector, as well as other pseudo-selectors, can only be used this way.\n", |
| 246 | + "Table styles are also used to control features which can apply to the whole table at once such as creating a generic hover functionality. The `:hover` pseudo-selector, as well as other pseudo-selectors, can only be used this way.\n", |
247 | 247 | "\n",
|
248 | 248 | "To replicate the normal format of CSS selectors and properties (attribute value pairs), e.g. \n",
|
249 | 249 | "\n",
|
|
295 | 295 | "cell_type": "markdown",
|
296 | 296 | "metadata": {},
|
297 | 297 | "source": [
|
298 |
| - "Next we just add a couple more styling artifacts targeting specific parts of the table, and we add some internally defined CSS classes that we need for the next section. Be careful here, since we are *chaining methods* we need to explicitly instruct the method **not to** ``overwrite`` the existing styles." |
| 298 | + "Next we just add a couple more styling artifacts targeting specific parts of the table. Be careful here, since we are *chaining methods* we need to explicitly instruct the method **not to** ``overwrite`` the existing styles." |
299 | 299 | ]
|
300 | 300 | },
|
301 | 301 | {
|
|
308 | 308 | " {'selector': 'th.col_heading', 'props': 'text-align: center;'},\n",
|
309 | 309 | " {'selector': 'th.col_heading.level0', 'props': 'font-size: 1.5em;'},\n",
|
310 | 310 | " {'selector': 'td', 'props': 'text-align: center; font-weight: bold;'},\n",
|
311 |
| - " # internal CSS classes\n", |
312 |
| - " {'selector': '.true', 'props': 'background-color: #e6ffe6;'},\n", |
313 |
| - " {'selector': '.false', 'props': 'background-color: #ffe6e6;'},\n", |
314 |
| - " {'selector': '.border-red', 'props': 'border: 2px dashed red;'},\n", |
315 |
| - " {'selector': '.border-green', 'props': 'border: 2px dashed green;'},\n", |
316 | 311 | "], overwrite=False)"
|
317 | 312 | ]
|
318 | 313 | },
|
|
394 | 389 | "\n",
|
395 | 390 | "*New in version 1.2.0*\n",
|
396 | 391 | "\n",
|
397 |
| - "The [.set_td_classes()][tdclass] method accepts a DataFrame with matching indices and columns to the underlying [Styler][styler]'s DataFrame. That DataFrame will contain strings as css-classes to add to individual data cells: the `<td>` elements of the `<table>`. Here we add our `.true` and `.false` classes that we created previously. We will save adding the borders until the [section on tooltips](#Tooltips).\n", |
| 392 | + "The [.set_td_classes()][tdclass] method accepts a DataFrame with matching indices and columns to the underlying [Styler][styler]'s DataFrame. That DataFrame will contain strings as css-classes to add to individual data cells: the `<td>` elements of the `<table>`. Rather than use external CSS we will create our classes internally and add them to table style. We will save adding the borders until the [section on tooltips](#Tooltips).\n", |
398 | 393 | "\n",
|
399 | 394 | "[tdclass]: ../reference/api/pandas.io.formats.style.Styler.set_td_classes.rst\n",
|
400 | 395 | "[styler]: ../reference/api/pandas.io.formats.style.Styler.rst"
|
|
406 | 401 | "metadata": {},
|
407 | 402 | "outputs": [],
|
408 | 403 | "source": [
|
| 404 | + "s.set_table_styles([ # create internal CSS classes\n", |
| 405 | + " {'selector': '.true', 'props': 'background-color: #e6ffe6;'},\n", |
| 406 | + " {'selector': '.false', 'props': 'background-color: #ffe6e6;'},\n", |
| 407 | + "], overwrite=False)\n", |
409 | 408 | "cell_color = pd.DataFrame([['true ', 'false ', 'true ', 'false '], \n",
|
410 | 409 | " ['false ', 'true ', 'false ', 'true ']], \n",
|
411 | 410 | " index=df.index, \n",
|
|
622 | 621 | "cell_type": "markdown",
|
623 | 622 | "metadata": {},
|
624 | 623 | "source": [
|
625 |
| - "The only thing left to do for our table is to add the highlighting borders to draw the audience attention to the tooltips. **Setting classes always overwrites** so we need to make sure we add the previous classes." |
| 624 | + "The only thing left to do for our table is to add the highlighting borders to draw the audience attention to the tooltips. We will create internal CSS classes as before using table styles. **Setting classes always overwrites** so we need to make sure we add the previous classes." |
626 | 625 | ]
|
627 | 626 | },
|
628 | 627 | {
|
|
631 | 630 | "metadata": {},
|
632 | 631 | "outputs": [],
|
633 | 632 | "source": [
|
| 633 | + "s.set_table_styles([ # create internal CSS classes\n", |
| 634 | + " {'selector': '.border-red', 'props': 'border: 2px dashed red;'},\n", |
| 635 | + " {'selector': '.border-green', 'props': 'border: 2px dashed green;'},\n", |
| 636 | + "], overwrite=False)\n", |
634 | 637 | "cell_border = pd.DataFrame([['border-green ', ' ', ' ', 'border-red '], \n",
|
635 | 638 | " [' ', ' ', ' ', ' ']], \n",
|
636 | 639 | " index=df.index, \n",
|
|
1381 | 1384 | "source": [
|
1382 | 1385 | "### HTML Escaping\n",
|
1383 | 1386 | "\n",
|
1384 |
| - "Suppose you have to display HTML within HTML, that can be a bit of pain when the renderer can't distinguish. You can use the `escape` formatting option to handle this. Even use it within a formatter that contains HTML itself." |
| 1387 | + "Suppose you have to display HTML within HTML, that can be a bit of pain when the renderer can't distinguish. You can use the `escape` formatting option to handle this, and even use it within a formatter that contains HTML itself." |
1385 | 1388 | ]
|
1386 | 1389 | },
|
1387 | 1390 | {
|
|
1400 | 1403 | "metadata": {},
|
1401 | 1404 | "outputs": [],
|
1402 | 1405 | "source": [
|
1403 |
| - "# df4.style.format(escape=True)" |
| 1406 | + "df4.style.format(escape=True)" |
| 1407 | + ] |
| 1408 | + }, |
| 1409 | + { |
| 1410 | + "cell_type": "code", |
| 1411 | + "execution_count": null, |
| 1412 | + "metadata": {}, |
| 1413 | + "outputs": [], |
| 1414 | + "source": [ |
| 1415 | + "df4.style.format('<a href=\"https://pandas.pydata.org\" target=\"_blank\">{}</a>', escape=True)" |
1404 | 1416 | ]
|
1405 | 1417 | },
|
1406 | 1418 | {
|
|
0 commit comments