Skip to content

Commit c837771

Browse files
committed
refactor: add reset_formatter function to reset global HTML formatter state
- Implemented reset_formatter to create a new default DataFrame HTML formatter and update the global reference. - Added clean_formatter_state fixture in tests to ensure a fresh formatter state for each test case. - Updated test cases to use clean_formatter_state instead of the previous reset_formatter implementation.
1 parent 1c6e189 commit c837771

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

python/datafusion/html_formatter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,19 @@ def configure_formatter(**kwargs: Any) -> None:
415415
_refresh_formatter_reference()
416416

417417

418+
def reset_formatter() -> None:
419+
"""Reset the global DataFrame HTML formatter to default settings.
420+
421+
This function creates a new formatter with default configuration
422+
and sets it as the global formatter for all DataFrames.
423+
"""
424+
global _default_formatter
425+
_default_formatter = DataFrameHtmlFormatter()
426+
427+
# Ensure the changes are reflected in existing DataFrames
428+
_refresh_formatter_reference()
429+
430+
418431
def _refresh_formatter_reference() -> None:
419432
"""Refresh formatter reference in any modules using it.
420433

python/tests/test_dataframe.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
_default_formatter,
3737
configure_formatter,
3838
get_formatter,
39+
reset_formatter,
3940
)
4041
from pyarrow.csv import write_csv
4142

@@ -109,6 +110,12 @@ def partitioned_df():
109110
return ctx.create_dataframe([[batch]])
110111

111112

113+
@pytest.fixture
114+
def clean_formatter_state():
115+
"""Reset the HTML formatter after each test."""
116+
reset_formatter()
117+
118+
112119
def test_select(df):
113120
df_1 = df.select(
114121
column("a") + column("b"),
@@ -663,20 +670,7 @@ def test_window_frame_defaults_match_postgres(partitioned_df):
663670
assert df_2.sort(col_a).to_pydict() == expected
664671

665672

666-
@pytest.fixture
667-
def reset_formatter():
668-
"""Reset the HTML formatter after each test."""
669-
670-
original = _default_formatter
671-
672-
# Give the test a fresh formatter
673-
configure_formatter()
674-
675-
yield
676-
globals()["_default_formatter"] = original
677-
678-
679-
def test_html_formatter_configuration(df, reset_formatter):
673+
def test_html_formatter_configuration(df, clean_formatter_state):
680674
"""Test configuring the HTML formatter with different options."""
681675
# Configure with custom settings
682676
configure_formatter(
@@ -695,7 +689,7 @@ def test_html_formatter_configuration(df, reset_formatter):
695689
assert "expandable-container" not in html_output
696690

697691

698-
def test_html_formatter_custom_style_provider(df, reset_formatter):
692+
def test_html_formatter_custom_style_provider(df, clean_formatter_state):
699693
"""Test using custom style providers with the HTML formatter."""
700694

701695
class CustomStyleProvider:
@@ -716,7 +710,7 @@ def get_header_style(self) -> str:
716710
assert "background-color: #f5f5f5" in html_output
717711

718712

719-
def test_html_formatter_type_formatters(df, reset_formatter):
713+
def test_html_formatter_type_formatters(df, clean_formatter_state):
720714
"""Test registering custom type formatters for specific data types."""
721715

722716
# Get current formatter and register custom formatters
@@ -736,7 +730,7 @@ def format_int(value):
736730
assert '<span style="color: blue">1</span>' in html_output
737731

738732

739-
def test_html_formatter_custom_cell_builder(df, reset_formatter):
733+
def test_html_formatter_custom_cell_builder(df, clean_formatter_state):
740734
"""Test using a custom cell builder function."""
741735

742736
# Create a custom cell builder that changes background color based on value
@@ -764,7 +758,7 @@ def custom_cell_builder(value, row, col, table_id):
764758
assert "background-color: #d3e9f0" in html_output # For values 1,2
765759

766760

767-
def test_html_formatter_custom_header_builder(df, reset_formatter):
761+
def test_html_formatter_custom_header_builder(df, clean_formatter_state):
768762
"""Test using a custom header builder function."""
769763

770764
# Create a custom header builder with tooltips
@@ -792,7 +786,7 @@ def custom_header_builder(field):
792786
assert "background-color: #333; color: white" in html_output
793787

794788

795-
def test_html_formatter_complex_customization(df, reset_formatter):
789+
def test_html_formatter_complex_customization(df, clean_formatter_state):
796790
"""Test combining multiple customization options together."""
797791

798792
# Create a dark mode style provider
@@ -1423,6 +1417,8 @@ def add_with_parameter(df_internal, value: Any) -> DataFrame:
14231417

14241418

14251419
def test_dataframe_repr_html(df) -> None:
1420+
"""Test that DataFrame._repr_html_ produces expected HTML output."""
1421+
14261422
output = df._repr_html_()
14271423

14281424
# Since we've added a fair bit of processing to the html output, lets just verify

0 commit comments

Comments
 (0)