diff --git a/doc/source/user_guide/style.ipynb b/doc/source/user_guide/style.ipynb index 1c83b0d3d048b..440cb85d1d6a6 100644 --- a/doc/source/user_guide/style.ipynb +++ b/doc/source/user_guide/style.ipynb @@ -1047,7 +1047,7 @@ "source": [ "### 5. If every byte counts use string replacement\n", "\n", - "You can remove unnecessary HTML, or shorten the default class names with string replace functions." + "You can remove unnecessary HTML, or shorten the default class names by replacing the default css dict. You can read a little more about CSS [below](#More-About-CSS-and-HTML)." ] }, { @@ -1056,21 +1056,24 @@ "metadata": {}, "outputs": [], "source": [ - "html = Styler(df4, uuid_len=0, cell_ids=False)\\\n", - " .set_table_styles([{'selector': 'td', 'props': props},\n", - " {'selector': '.col1', 'props': 'color:green;'},\n", - " {'selector': '.level0', 'props': 'color:blue;'}])\\\n", - " .to_html()\\\n", - " .replace('blank', '')\\\n", - " .replace('data', '')\\\n", - " .replace('level0', 'l0')\\\n", - " .replace('col_heading', '')\\\n", - " .replace('row_heading', '')\n", - "\n", - "import re\n", - "html = re.sub(r'col[0-9]+', lambda x: x.group().replace('col', 'c'), html)\n", - "html = re.sub(r'row[0-9]+', lambda x: x.group().replace('row', 'r'), html)\n", - "print(html)" + "my_css = {\n", + " \"row_heading\": \"\",\n", + " \"col_heading\": \"\",\n", + " \"index_name\": \"\",\n", + " \"col\": \"c\",\n", + " \"row\": \"r\",\n", + " \"col_trim\": \"\",\n", + " \"row_trim\": \"\",\n", + " \"level\": \"l\",\n", + " \"data\": \"\",\n", + " \"blank\": \"\",\n", + "}\n", + "html = Styler(df4, uuid_len=0, cell_ids=False)\n", + "html.set_table_styles([{'selector': 'td', 'props': props},\n", + " {'selector': '.c1', 'props': 'color:green;'},\n", + " {'selector': '.l0', 'props': 'color:blue;'}],\n", + " css_class_names=my_css)\n", + "print(html.to_html())" ] }, { @@ -1079,8 +1082,7 @@ "metadata": {}, "outputs": [], "source": [ - "from IPython.display import HTML\n", - "HTML(html)" + "html" ] }, { @@ -1658,6 +1660,7 @@ " + `row`, where `m` is the numeric position of the cell.\n", " + `col`, where `n` is the numeric position of the cell.\n", "- Blank cells include `blank`\n", + "- Trimmed cells include `col_trim` or `row_trim`\n", "\n", "The structure of the `id` is `T_uuid_level_row_col` where `level` is used only on headings, and headings will only have either `row` or `col` whichever is needed. By default we've also prepended each row/column identifier with a UUID unique to each DataFrame so that the style from one doesn't collide with the styling from another within the same notebook or page. You can read more about the use of UUIDs in [Optimization](#Optimization).\n", "\n", @@ -2020,7 +2023,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -2034,7 +2037,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.6" + "version": "3.9.5" } }, "nbformat": 4, diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index b7efec8fd2e89..877757588a9f1 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -80,6 +80,7 @@ Styler - Global options have been extended to configure default ``Styler`` properties including formatting and encoding and mathjax options and LaTeX (:issue:`41395`) - Naive sparsification is now possible for LaTeX without the multirow package (:issue:`43369`) - :meth:`Styler.to_html` omits CSSStyle rules for hidden table elements (:issue:`43619`) + - Custom CSS classes can now be directly specified without string replacement (:issue:`43686`) Formerly Styler relied on ``display.html.use_mathjax``, which has now been replaced by ``styler.html.mathjax``. diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 8ffb8105d4a90..c0855aedd116b 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -145,7 +145,10 @@ class Styler(StylerRenderer): Attributes ---------- env : Jinja2 jinja2.Environment - template : Jinja2 Template + template_html : Jinja2 Template + template_html_table : Jinja2 Template + template_html_style : Jinja2 Template + template_latex : Jinja2 Template loader : Jinja2 Loader See Also @@ -181,6 +184,11 @@ class Styler(StylerRenderer): * Blank cells include ``blank`` * Data cells include ``data`` + * Trimmed cells include ``col_trim`` or ``row_trim``. + + Any, or all, or these classes can be renamed by using the ``css_class_names`` + argument in ``Styler.set_table_classes``, giving a value such as + *{"row": "MY_ROW_CLASS", "col_trim": "", "row_trim": ""}*. """ def __init__( @@ -1158,6 +1166,7 @@ def _copy(self, deepcopy: bool = False) -> Styler: - caption Non-data dependent attributes [copied and exported]: + - css - hidden index state and hidden columns state (.hide_index_, .hide_columns_) - table_attributes - table_styles @@ -1184,6 +1193,7 @@ def _copy(self, deepcopy: bool = False) -> Styler: "template_html", ] deep = [ # nested lists or dicts + "css", "_display_funcs", "_display_funcs_index", "_display_funcs_columns", @@ -1947,9 +1957,10 @@ def set_sticky( def set_table_styles( self, - table_styles: dict[Any, CSSStyles] | CSSStyles, + table_styles: dict[Any, CSSStyles] | CSSStyles | None = None, axis: int = 0, overwrite: bool = True, + css_class_names: dict[str, str] | None = None, ) -> Styler: """ Set the table styles included within the `` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 n1a
 n2c
n1n2 
ac0
+ """ + ) + result = styler_mi.to_html() + assert result == expected + + def test_include_css_style_rules_only_for_visible_cells(styler_mi): # GH 43619 result = ( diff --git a/pandas/tests/io/formats/style/test_style.py b/pandas/tests/io/formats/style/test_style.py index 6a09018c5b20b..10b98ed6af375 100644 --- a/pandas/tests/io/formats/style/test_style.py +++ b/pandas/tests/io/formats/style/test_style.py @@ -42,6 +42,7 @@ def mi_styler(mi_df): @pytest.fixture def mi_styler_comp(mi_styler): # comprehensively add features to mi_styler + mi_styler.css = {**mi_styler.css, **{"row": "ROW", "col": "COL"}} mi_styler.uuid_len = 5 mi_styler.uuid = "abcde" mi_styler.set_caption("capt")