@@ -1742,42 +1742,129 @@ def set_table_attributes(self, attributes: str) -> Styler:
1742
1742
self .table_attributes = attributes
1743
1743
return self
1744
1744
1745
- def export (self ) -> list [ tuple [ Callable , tuple , dict ] ]:
1745
+ def export (self ) -> dict [ str , Any ]:
1746
1746
"""
1747
- Export the styles applied to the current `` Styler`` .
1747
+ Export the styles applied to the current Styler.
1748
1748
1749
1749
Can be applied to a second Styler with ``Styler.use``.
1750
1750
1751
1751
Returns
1752
1752
-------
1753
- styles : list
1753
+ styles : dict
1754
1754
1755
1755
See Also
1756
1756
--------
1757
- Styler.use: Set the styles on the current ``Styler``.
1757
+ Styler.use: Set the styles on the current Styler.
1758
+ Styler.copy: Create a copy of the current Styler.
1759
+
1760
+ Notes
1761
+ -----
1762
+ This method is designed to copy non-data dependent attributes of
1763
+ one Styler to another. It differs from ``Styler.copy`` where data and
1764
+ data dependent attributes are also copied.
1765
+
1766
+ The following items are exported since they are not generally data dependent:
1767
+
1768
+ - Styling functions added by the ``apply`` and ``applymap``
1769
+ - Whether axes and names are hidden from the display, if unambiguous.
1770
+ - Table attributes
1771
+ - Table styles
1772
+
1773
+ The following attributes are considered data dependent and therefore not
1774
+ exported:
1775
+
1776
+ - Caption
1777
+ - UUID
1778
+ - Tooltips
1779
+ - Any hidden rows or columns identified by Index labels
1780
+ - Any formatting applied using ``Styler.format``
1781
+ - Any CSS classes added using ``Styler.set_td_classes``
1782
+
1783
+ Examples
1784
+ --------
1785
+
1786
+ >>> styler = DataFrame([[1, 2], [3, 4]]).style
1787
+ >>> styler2 = DataFrame([[9, 9, 9]]).style
1788
+ >>> styler.hide_index().highlight_max(axis=1) # doctest: +SKIP
1789
+ >>> export = styler.export()
1790
+ >>> styler2.use(export) # doctest: +SKIP
1758
1791
"""
1759
- return self ._todo
1792
+ return {
1793
+ "apply" : copy .copy (self ._todo ),
1794
+ "table_attributes" : self .table_attributes ,
1795
+ "table_styles" : copy .copy (self .table_styles ),
1796
+ "hide_index" : all (self .hide_index_ ),
1797
+ "hide_columns" : all (self .hide_columns_ ),
1798
+ "hide_index_names" : self .hide_index_names ,
1799
+ "hide_column_names" : self .hide_column_names ,
1800
+ "css" : copy .copy (self .css ),
1801
+ }
1760
1802
1761
- def use (self , styles : list [ tuple [ Callable , tuple , dict ] ]) -> Styler :
1803
+ def use (self , styles : dict [ str , Any ]) -> Styler :
1762
1804
"""
1763
- Set the styles on the current `` Styler`` .
1805
+ Set the styles on the current Styler.
1764
1806
1765
1807
Possibly uses styles from ``Styler.export``.
1766
1808
1767
1809
Parameters
1768
1810
----------
1769
- styles : list
1770
- List of style functions.
1811
+ styles : dict(str, Any)
1812
+ List of attributes to add to Styler. Dict keys should contain only:
1813
+ - "apply": list of styler functions, typically added with ``apply`` or
1814
+ ``applymap``.
1815
+ - "table_attributes": HTML attributes, typically added with
1816
+ ``set_table_attributes``.
1817
+ - "table_styles": CSS selectors and properties, typically added with
1818
+ ``set_table_styles``.
1819
+ - "hide_index": whether the index is hidden, typically added with
1820
+ ``hide_index``, or a boolean list for hidden levels.
1821
+ - "hide_columns": whether column headers are hidden, typically added with
1822
+ ``hide_columns``, or a boolean list for hidden levels.
1823
+ - "hide_index_names": whether index names are hidden.
1824
+ - "hide_column_names": whether column header names are hidden.
1825
+ - "css": the css class names used.
1771
1826
1772
1827
Returns
1773
1828
-------
1774
1829
self : Styler
1775
1830
1776
1831
See Also
1777
1832
--------
1778
- Styler.export : Export the styles to applied to the current ``Styler``.
1779
- """
1780
- self ._todo .extend (styles )
1833
+ Styler.export : Export the non data dependent attributes to the current Styler.
1834
+
1835
+ Examples
1836
+ --------
1837
+
1838
+ >>> styler = DataFrame([[1, 2], [3, 4]]).style
1839
+ >>> styler2 = DataFrame([[9, 9, 9]]).style
1840
+ >>> styler.hide_index().highlight_max(axis=1) # doctest: +SKIP
1841
+ >>> export = styler.export()
1842
+ >>> styler2.use(export) # doctest: +SKIP
1843
+ """
1844
+ self ._todo .extend (styles .get ("apply" , []))
1845
+ table_attributes : str = self .table_attributes or ""
1846
+ obj_table_atts : str = (
1847
+ ""
1848
+ if styles .get ("table_attributes" ) is None
1849
+ else str (styles .get ("table_attributes" ))
1850
+ )
1851
+ self .set_table_attributes ((table_attributes + " " + obj_table_atts ).strip ())
1852
+ if styles .get ("table_styles" ):
1853
+ self .set_table_styles (styles .get ("table_styles" ), overwrite = False )
1854
+
1855
+ for obj in ["index" , "columns" ]:
1856
+ hide_obj = styles .get ("hide_" + obj )
1857
+ if hide_obj is not None :
1858
+ if isinstance (hide_obj , bool ):
1859
+ n = getattr (self , obj ).nlevels
1860
+ setattr (self , "hide_" + obj + "_" , [hide_obj ] * n )
1861
+ else :
1862
+ setattr (self , "hide_" + obj + "_" , hide_obj )
1863
+
1864
+ self .hide_index_names = styles .get ("hide_index_names" , False )
1865
+ self .hide_column_names = styles .get ("hide_column_names" , False )
1866
+ if styles .get ("css" ):
1867
+ self .css = styles .get ("css" ) # type: ignore[assignment]
1781
1868
return self
1782
1869
1783
1870
def set_uuid (self , uuid : str ) -> Styler :
0 commit comments