@@ -733,6 +733,83 @@ def format_int(value):
733
733
def test_html_formatter_custom_cell_builder (df , clean_formatter_state ):
734
734
"""Test using a custom cell builder function."""
735
735
736
+ def test_html_formatter_custom_cell_builder (df , clean_formatter_state ):
737
+ """Test using a custom cell builder function that changes style based on value."""
738
+
739
+ # Create a custom cell builder with distinct styling for different value ranges
740
+ def custom_cell_builder (value , row , col , table_id ):
741
+ try :
742
+ num_value = int (value )
743
+ if num_value > 5 : # Values > 5 get green background with indicator
744
+ return f'<td style="background-color: #d9f0d3" data-test="high">{ value } -high</td>'
745
+ if num_value < 3 : # Values < 3 get blue background with indicator
746
+ return f'<td style="background-color: #d3e9f0" data-test="low">{ value } -low</td>'
747
+ except (ValueError , TypeError ):
748
+ pass
749
+
750
+ # Default styling for other cells (3, 4, 5)
751
+ return (
752
+ f'<td style="border: 1px solid #ddd" data-test="mid">{ value } -mid</td>'
753
+ )
754
+
755
+ # Set our custom cell builder
756
+ formatter = get_formatter ()
757
+ formatter .set_custom_cell_builder (custom_cell_builder )
758
+
759
+ html_output = df ._repr_html_ ()
760
+
761
+ # Extract cells with specific styling using regex
762
+ low_cells = re .findall (
763
+ r'<td style="background-color: #d3e9f0"[^>]*>(\d+)-low</td>' , html_output
764
+ )
765
+ mid_cells = re .findall (
766
+ r'<td style="border: 1px solid #ddd"[^>]*>(\d+)-mid</td>' , html_output
767
+ )
768
+ high_cells = re .findall (
769
+ r'<td style="background-color: #d9f0d3"[^>]*>(\d+)-high</td>' , html_output
770
+ )
771
+
772
+ # Sort the extracted values for consistent comparison
773
+ low_cells = sorted (map (int , low_cells ))
774
+ mid_cells = sorted (map (int , mid_cells ))
775
+ high_cells = sorted (map (int , high_cells ))
776
+
777
+ # Verify specific values have the correct styling applied
778
+ assert low_cells == [1 , 2 ] # Values < 3
779
+ assert mid_cells == [3 , 4 , 5 , 5 ] # Values 3-5
780
+ assert high_cells == [6 , 8 , 8 ] # Values > 5
781
+
782
+ # Verify the exact content with styling appears in the output
783
+ assert (
784
+ '<td style="background-color: #d3e9f0" data-test="low">1-low</td>'
785
+ in html_output
786
+ )
787
+ assert (
788
+ '<td style="background-color: #d3e9f0" data-test="low">2-low</td>'
789
+ in html_output
790
+ )
791
+ assert (
792
+ '<td style="border: 1px solid #ddd" data-test="mid">3-mid</td>'
793
+ in html_output
794
+ )
795
+ assert (
796
+ '<td style="border: 1px solid #ddd" data-test="mid">4-mid</td>'
797
+ in html_output
798
+ )
799
+ assert (
800
+ '<td style="background-color: #d9f0d3" data-test="high">6-high</td>'
801
+ in html_output
802
+ )
803
+ assert (
804
+ '<td style="background-color: #d9f0d3" data-test="high">8-high</td>'
805
+ in html_output
806
+ )
807
+
808
+ # Count occurrences to ensure all cells are properly styled
809
+ assert html_output .count ("-low</td>" ) == 2 # Two low values (1, 2)
810
+ assert html_output .count ("-mid</td>" ) == 4 # Four mid values (3, 4, 5, 5)
811
+ assert html_output .count ("-high</td>" ) == 3 # Three high values (6, 8, 8)
812
+
736
813
# Create a custom cell builder that changes background color based on value
737
814
def custom_cell_builder (value , row , col , table_id ):
738
815
# Handle numeric values regardless of their exact type
0 commit comments