From af99333fe5fe9cdd0f446d12e46306ff9490290c Mon Sep 17 00:00:00 2001 From: "JHM Darbyshire (iMac)" Date: Fri, 7 Aug 2020 11:58:43 +0200 Subject: [PATCH 01/11] ENH: add named based column css styling to pandas Styler --- doc/source/user_guide/style.ipynb | 44 +++++++++++++-- pandas/io/formats/style.py | 80 +++++++++++++++++++++++++++ pandas/tests/io/formats/test_style.py | 22 ++++++++ 3 files changed, 141 insertions(+), 5 deletions(-) diff --git a/doc/source/user_guide/style.ipynb b/doc/source/user_guide/style.ipynb index 77a1fef28f373..460eef0b19508 100644 --- a/doc/source/user_guide/style.ipynb +++ b/doc/source/user_guide/style.ipynb @@ -793,7 +793,8 @@ "source": [ "The next option you have are \"table styles\".\n", "These are styles that apply to the table as a whole, but don't look at the data.\n", - "Certain sytlings, including pseudo-selectors like `:hover` can only be used this way." + "Certain stylings, including pseudo-selectors like `:hover` can only be used this way.\n", + "These can also be used to set specific row or column based class selectors, as will be shown." ] }, { @@ -831,9 +832,31 @@ "The value for `props` should be a list of tuples of `('attribute', 'value')`.\n", "\n", "`table_styles` are extremely flexible, but not as fun to type out by hand.\n", - "We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here." + "We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here.\n", + "\n", + "`table_styles` can be used to add column and row based class descriptors. For large tables this can increase performance by avoiding repetitive individual css for each cell, and it can also simplify style construction in some cases.\n", + "`Styler.extend_column_styles` is a specific function to set styles to individual columns.\n", + "\n", + "Note that `Styler.set_table_styles` will overwrite existing styles (including those column based) whilst the preferred `Styler.extend_table_styles` will add to the current items." ] }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "html = html.extend_column_styles({\n", + " 'B': [dict(selector='', props=[('color', 'blue')])]\n", + "})\n", + "html" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, { "cell_type": "markdown", "metadata": {}, @@ -922,10 +945,12 @@ "- DataFrame only `(use Series.to_frame().style)`\n", "- The index and columns must be unique\n", "- No large repr, and performance isn't great; this is intended for summary DataFrames\n", - "- You can only style the *values*, not the index or columns\n", + "- You can only style the *values*, not the index or columns (except with `table_styles` above)\n", "- You can only apply styles, you can't insert new HTML entities\n", "\n", - "Some of these will be addressed in the future.\n" + "Some of these will be addressed in the future.\n", + "Performance can suffer when adding styles to each cell in a large DataFrame.\n", + "It is recommended to apply table or column based styles where possible to limit overall HTML length, as well as setting a shorter UUID to avoid unnecessary repeated data transmission. \n" ] }, { @@ -1237,8 +1262,17 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" + }, + "pycharm": { + "stem_cell": { + "cell_type": "raw", + "source": [], + "metadata": { + "collapsed": false + } + } } }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 3bbb5271bce61..86169cdc8c8dd 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -939,6 +939,86 @@ def set_table_styles(self, table_styles) -> "Styler": self.table_styles = table_styles return self + def extend_table_styles(self, table_styles) -> "Styler": + """ + Extend the existing table styles on a Styler. + + These are placed in a ``