@@ -1566,3 +1566,99 @@ def test_dataframe_repr_html_values(df):
15661566 print (f"HTML output snippet: { html [:500 ]} ..." )
15671567
15681568 assert len (matches ) > 0 , "Expected pattern of values not found in HTML output"
1569+
1570+
1571+ def test_html_formatter_shared_styles (df , clean_formatter_state ):
1572+ """Test that shared styles work correctly across multiple tables."""
1573+ from datafusion .html_formatter import (
1574+ get_formatter ,
1575+ configure_formatter ,
1576+ reset_styles_loaded_state ,
1577+ )
1578+
1579+ # First, ensure we're using shared styles
1580+ configure_formatter (use_shared_styles = True )
1581+ formatter = get_formatter ()
1582+
1583+ # Get HTML output for first table - should include styles
1584+ html_first = df ._repr_html_ ()
1585+
1586+ # Verify styles are included in first render
1587+ assert "<style>" in html_first
1588+ assert ".expandable-container" in html_first
1589+
1590+ # Get HTML output for second table - should NOT include styles
1591+ html_second = df ._repr_html_ ()
1592+
1593+ # Verify styles are NOT included in second render
1594+ assert "<style>" not in html_second
1595+ assert ".expandable-container" not in html_second
1596+
1597+ # Reset the styles loaded state and verify styles are included again
1598+ reset_styles_loaded_state ()
1599+ html_after_reset = df ._repr_html_ ()
1600+
1601+ # Verify styles are included after reset
1602+ assert "<style>" in html_after_reset
1603+ assert ".expandable-container" in html_after_reset
1604+
1605+
1606+ def test_html_formatter_no_shared_styles (df , clean_formatter_state ):
1607+ """Test that styles are always included when shared styles are disabled."""
1608+ from datafusion .html_formatter import configure_formatter
1609+
1610+ # Configure formatter to NOT use shared styles
1611+ configure_formatter (use_shared_styles = False )
1612+
1613+ # Generate HTML multiple times
1614+ html_first = df ._repr_html_ ()
1615+ html_second = df ._repr_html_ ()
1616+
1617+ # Verify styles are included in both renders
1618+ assert "<style>" in html_first
1619+ assert "<style>" in html_second
1620+ assert ".expandable-container" in html_first
1621+ assert ".expandable-container" in html_second
1622+
1623+
1624+ def test_html_formatter_manual_format_html (clean_formatter_state ):
1625+ """Test direct usage of format_html method with shared styles."""
1626+ from datafusion .html_formatter import (
1627+ get_formatter ,
1628+ DataFrameHtmlFormatter ,
1629+ reset_styles_loaded_state ,
1630+ )
1631+ import pyarrow as pa
1632+
1633+ # Create sample data
1634+ batch = pa .RecordBatch .from_arrays (
1635+ [pa .array ([1 , 2 , 3 ]), pa .array ([4 , 5 , 6 ])],
1636+ names = ["a" , "b" ],
1637+ )
1638+
1639+ formatter = get_formatter ()
1640+
1641+ # First call should include styles
1642+ html_first = formatter .format_html ([batch ], batch .schema )
1643+ assert "<style>" in html_first
1644+
1645+ # Second call should not include styles (using shared styles by default)
1646+ html_second = formatter .format_html ([batch ], batch .schema )
1647+ assert "<style>" not in html_second
1648+
1649+ # Reset loaded state
1650+ reset_styles_loaded_state ()
1651+
1652+ # After reset, styles should be included again
1653+ html_reset = formatter .format_html ([batch ], batch .schema )
1654+ assert "<style>" in html_reset
1655+
1656+ # Create a new formatter with shared_styles=False
1657+ local_formatter = DataFrameHtmlFormatter (use_shared_styles = False )
1658+
1659+ # Both calls should include styles
1660+ local_html_1 = local_formatter .format_html ([batch ], batch .schema )
1661+ local_html_2 = local_formatter .format_html ([batch ], batch .schema )
1662+
1663+ assert "<style>" in local_html_1
1664+ assert "<style>" in local_html_2
0 commit comments