@@ -909,117 +909,84 @@ def set_caption(self, caption: str) -> "Styler":
909
909
self .caption = caption
910
910
return self
911
911
912
- def set_table_styles (self , table_styles ) -> "Styler" :
912
+ def set_table_styles (self , table_styles , axis = 0 , overwrite = True ) -> "Styler" :
913
913
"""
914
914
Set the table styles on a Styler.
915
915
916
916
These are placed in a ``<style>`` tag before the generated HTML table.
917
917
918
- Parameters
919
- ----------
920
- table_styles : list
921
- Each individual table_style should be a dictionary with
922
- ``selector`` and ``props`` keys. ``selector`` should be a CSS
923
- selector that the style will be applied to (automatically
924
- prefixed by the table's UUID) and ``props`` should be a list of
925
- tuples with ``(attribute, value)``.
926
-
927
- Returns
928
- -------
929
- self : Styler
930
-
931
- Examples
932
- --------
933
- >>> df = pd.DataFrame(np.random.randn(10, 4))
934
- >>> df.style.set_table_styles(
935
- ... [{'selector': 'tr:hover',
936
- ... 'props': [('background-color', 'yellow')]}]
937
- ... )
938
- """
939
- self .table_styles = table_styles
940
- return self
941
-
942
- def extend_table_styles (self , table_styles ) -> "Styler" :
943
- """
944
- Extend the existing table styles on a Styler.
945
-
946
- These are placed in a ``<style>`` tag before the generated HTML table.
918
+ This function can be used to style the entire table, columns, rows or
919
+ specific HTML selectors.
947
920
948
921
Parameters
949
922
----------
950
- table_styles : list
951
- Each individual table_style should be a dictionary with
952
- ``selector`` and ``props`` keys. ``selector`` should be a CSS
953
- selector that the style will be applied to (automatically
954
- prefixed by the table's UUID) and ``props`` should be a list of
955
- tuples with ``(attribute, value)``.
923
+ table_styles : list or dict
924
+ If supplying a list, each individual table_style should be a
925
+ dictionary with ``selector`` and ``props`` keys. ``selector``
926
+ should be a CSS selector that the style will be applied to
927
+ (automatically prefixed by the table's UUID) and ``props``
928
+ should be a list of tuples with ``(attribute, value)``.
929
+ If supplying a dict, the dict keys should correspond to
930
+ column names or index values, depending upon the specified
931
+ `axis` argument. These will be mapped to row or col CSS
932
+ selectors. MultiIndex values as dict keys should be
933
+ in their respective tuple form. The dict values should be
934
+ a list as specified in the form with CSS selectors and
935
+ props that will be applied to the specified row or column.
936
+ axis : {0 or 'index', 1 or 'columns', None}, default 0
937
+ Apply to each column (``axis=0`` or ``'index'``), to each row
938
+ (``axis=1`` or ``'columns'``). Only used if `table_styles` is
939
+ dict.
940
+ overwrite : boolean, default True
941
+ Styles are replaced if `True`, or extended if `False`. CSS
942
+ rules are preserved so most recent styles set will dominate
943
+ if selectors intersect.
956
944
957
945
Returns
958
946
-------
959
947
self : Styler
960
948
961
949
Examples
962
950
--------
963
- >>> df = pd.DataFrame(np.random.randn(10, 4))
951
+ >>> df = pd.DataFrame(np.random.randn(10, 4),
952
+ ... columns=['A', 'B', 'C', 'D'])
964
953
>>> df.style.set_table_styles(
965
954
... [{'selector': 'tr:hover',
966
955
... 'props': [('background-color', 'yellow')]}]
967
- ... ).extend_table_styles(
968
- ... [{'selector': '.col2',
969
- ... 'props': [('background-color', 'blue')]}]
970
- ...)
971
- """
972
- if self .table_styles is None :
973
- return self .set_table_styles (table_styles )
974
- self .table_styles .extend (table_styles )
956
+ ... )
957
+ >>> df.style.set_table_styles({
958
+ ... 'A': [{'selector': '',
959
+ ... 'props': [('color', 'red')]}],
960
+ ... 'B': [{'selector': 'td',
961
+ ... 'props': [('color', 'blue')]}]
962
+ ... }, overwrite=False)
963
+ >>> df.style.set_table_styles({
964
+ ... 0: [{'selector': 'td:hover',
965
+ ... 'props': [('font-size', '25px')]}]
966
+ ... }, axis=1, overwrite=False)
967
+ """
968
+ if isinstance (table_styles , dict ):
969
+ if axis == 0 or axis == 'index' :
970
+ obj = self .data .columns
971
+ idf = '.col'
972
+ else :
973
+ obj = self .data .index
974
+ idf = '.row'
975
+
976
+ _styles = []
977
+ for key , styles in table_styles .items ():
978
+ for s in styles :
979
+ c = str (obj .get_loc (key ))
980
+ _styles .append (
981
+ {"selector" : s ["selector" ] + idf + c , "props" : s ["props" ]}
982
+ )
983
+ table_styles = _styles
984
+ if not overwrite and self .table_styles is not None :
985
+ self .table_styles .extend (table_styles )
986
+ else :
987
+ self .table_styles = table_styles
975
988
return self
976
989
977
- def extend_column_styles (self , column_styles ) -> "Styler" :
978
- """
979
- Sets class styles for each column on a Styler.
980
-
981
- These are placed in a ``<style>`` tag before the generated HTML table.
982
-
983
- Parameters
984
- ----------
985
- column_styles : dict
986
- Each key of the dict should be a column header with the value
987
- being a list of individual styles. Each style is a dictionary with
988
- ``selector`` and ``props`` keys. ``selector`` should be a CSS
989
- selector that the style will be applied to (automatically
990
- prefixed by the table's UUID) and ``props`` should be a list of
991
- tuples with ``(attribute, value)``.
992
-
993
- Returns
994
- -------
995
- self : Styler
996
-
997
- Notes
998
- -----
999
- Where the ``selector`` is an empty string or `None` the ``props`` will
1000
- be applied to the column class generically.
1001
-
1002
- Examples
1003
- --------
1004
- >>> df = pd.DataFrame(np.random.randn(3, 2), columns=['a', 'b'])
1005
- >>> df.style.extend_column_styles({
1006
- ... 'a': [{'selector': '',
1007
- ... 'props': [('font-size', '20px')]},
1008
- ... {'selector': 'td:hover',
1009
- ... 'props': [('color', 'pink')]}],
1010
- ... 'b': [{'selector': '',
1011
- ... 'props': [('font-size', '10px')]}]
1012
- ... })
1013
- """
1014
- _styles = []
1015
- for col , styles in column_styles .items ():
1016
- for s in styles :
1017
- c = str (self .data .columns .get_loc (col ))
1018
- _styles .append (
1019
- {"selector" : s ["selector" ] + ".col" + c , "props" : s ["props" ]}
1020
- )
1021
- return self .extend_table_styles (_styles )
1022
-
1023
990
def set_na_rep (self , na_rep : str ) -> "Styler" :
1024
991
"""
1025
992
Set the missing data representation on a Styler.
0 commit comments