diff --git a/gt_extras/colors.py b/gt_extras/colors.py index f3e9512..ffab65a 100644 --- a/gt_extras/colors.py +++ b/gt_extras/colors.py @@ -84,6 +84,13 @@ def gt_highlight_cols( gte.gt_highlight_cols(gt, columns="hp") ``` """ + # Throw if `font_weight` is not one of the allowed values + if isinstance(font_weight, str): + if font_weight not in ["normal", "bold", "bolder", "lighter"]: + raise ValueError("Font_weight must be one of 'normal', 'bold', 'bolder', or 'lighter', or an integer") + elif not isinstance(font_weight, (int, float)): + raise TypeError("Font_weight must be an int, float, or str") + if alpha: fill = _html_color(colors=[fill], alpha=alpha)[0] diff --git a/gt_extras/html.py b/gt_extras/html.py index d3cff79..91b3de5 100644 --- a/gt_extras/html.py +++ b/gt_extras/html.py @@ -70,7 +70,7 @@ def with_tooltip( An HTML string containing the formatted tooltip element. """ - # Throw if `text_decoration_style` is not one of the three allowed values + # Throw if `text_decoration_style` is not one of the allowed values if text_decoration_style not in [None, "solid", "dotted"]: raise ValueError("Text_decoration_style must be one of `None`, 'solid', or 'dotted'") diff --git a/gt_extras/icons.py b/gt_extras/icons.py index 03c8477..0023b5b 100644 --- a/gt_extras/icons.py +++ b/gt_extras/icons.py @@ -105,6 +105,10 @@ def fa_icon_repeat( -------- See `icon_svg()` in the `faicons` package for further implementation details. """ + # Throw if `a11y` is not one of the allowed values + if a11y not in [None, "deco", "sem"]: + raise ValueError("A11y must be one of `None`, 'deco', or 'sem'") + if repeats < 0: raise ValueError("repeats must be >= 0") diff --git a/gt_extras/plotting.py b/gt_extras/plotting.py index 75b8f09..f2b6f07 100644 --- a/gt_extras/plotting.py +++ b/gt_extras/plotting.py @@ -114,6 +114,10 @@ def gt_plt_bar( """ # A version with svg.py + # Throw if `scale_type` is not one of the allowed values + if scale_type not in [None, "percent", "number"]: + raise ValueError("Scale_type must be one of `None`, 'percent', or 'number'") + if bar_height > height: bar_height = height warnings.warn( @@ -186,7 +190,7 @@ def _make_bar_html( if stroke_color is None: stroke_color = "#FFFFFF00" - def make_bar(scaled_val: int, original_val: int) -> str: + def _make_bar(scaled_val: int, original_val: int) -> str: return _make_bar_html( scaled_val=scaled_val, original_val=original_val, @@ -215,7 +219,7 @@ def make_bar(scaled_val: int, original_val: int) -> str: # Apply the scaled value for each row, so the bar is proportional for i, scaled_val in enumerate(scaled_vals): res = res.fmt( - lambda original_val, scaled_val=scaled_val: make_bar( + lambda original_val, scaled_val=scaled_val: _make_bar( original_val=original_val, scaled_val=scaled_val, ), diff --git a/gt_extras/tests/test_colors.py b/gt_extras/tests/test_colors.py index d864de1..c2e5ef5 100644 --- a/gt_extras/tests/test_colors.py +++ b/gt_extras/tests/test_colors.py @@ -22,6 +22,20 @@ def test_gt_highlight_cols_alpha(mini_gt): assert "#80bcd833" in html +def test_gt_highlight_cols_font_weight_invalid_string(mini_gt): + with pytest.raises( + ValueError, + match="Font_weight must be one of 'normal', 'bold', 'bolder', or 'lighter', or an integer", + ): + gt_highlight_cols(mini_gt, font_weight="invalid") + + +@pytest.mark.parametrize("invalid_weight", [(1.5, 5), [], {}, None]) +def test_gt_highlight_cols_font_weight_invalid_type(mini_gt, invalid_weight): + with pytest.raises(TypeError, match="Font_weight must be an int, float, or str"): + gt_highlight_cols(mini_gt, font_weight=invalid_weight) + + def test_gt_hulk_col_numeric_snap(snapshot, mini_gt): res = gt_hulk_col_numeric(mini_gt) assert_rendered_body(snapshot, gt=res) diff --git a/gt_extras/tests/test_html.py b/gt_extras/tests/test_html.py index 2fcb13f..1507ca7 100644 --- a/gt_extras/tests/test_html.py +++ b/gt_extras/tests/test_html.py @@ -3,39 +3,48 @@ from great_tables import GT from gt_extras.html import gt_hyperlink, with_tooltip + def test_gt_hyperlink_basic(): result = gt_hyperlink("Google", "https://google.com") expected = 'Google' assert result == expected + def test_gt_hyperlink_new_tab_false(): result = gt_hyperlink("Google", "https://google.com", new_tab=False) expected = 'Google' assert result == expected + def test_gt_hyperlink_new_tab_true(): result = gt_hyperlink("GitHub", "https://github.com", new_tab=True) expected = 'GitHub' assert result == expected + def test_gt_hyperlink_empty_text(): result = gt_hyperlink("", "https://example.com") expected = '' assert result == expected + def test_gt_hyperlink_in_table(): - df = pd.DataFrame({ - "Name": ["Google", "GitHub"], - "Link": [ - gt_hyperlink("Visit Google", "https://google.com"), - gt_hyperlink("View GitHub", "https://github.com", new_tab=False) - ] - }) - + df = pd.DataFrame( + { + "Name": ["Google", "GitHub"], + "Link": [ + gt_hyperlink("Visit Google", "https://google.com"), + gt_hyperlink("View GitHub", "https://github.com", new_tab=False), + ], + } + ) + gt_table = GT(df) html_output = gt_table.as_raw_html() - - assert 'Visit Google' in html_output + + assert ( + 'Visit Google' in html_output + ) assert "https://github.com" in html_output assert 'target="_blank"' in html_output assert 'target="_self"' in html_output @@ -46,40 +55,49 @@ def test_with_tooltip_basic(): expected = '1' assert result == expected + def test_with_tooltip_underline_style(): result = with_tooltip("1", "Number One", text_decoration_style="solid") expected = '1' assert result == expected + def test_with_tooltip_underline_fail(): with pytest.raises(ValueError): with_tooltip("1", "Number One", text_decoration_style="underline") + def test_with_tooltip_no_decoration(): result = with_tooltip("1", "Number One", text_decoration_style=None) expected = '1' assert result == expected + def test_with_tooltip_no_color(): result = with_tooltip("1", "Number One", color=None) expected = '1' assert result == expected -def test_with_tooltip_in_table(): - df = pd.DataFrame({ - "Number": ["1", "2"], - "Description": [ - with_tooltip("1", "Number One"), - with_tooltip("2", "Number Two", text_decoration_style="solid", color="red") - ] - }) - + +def test_with_tooltip_in_table(): + df = pd.DataFrame( + { + "Number": ["1", "2"], + "Description": [ + with_tooltip("1", "Number One"), + with_tooltip( + "2", "Number Two", text_decoration_style="solid", color="red" + ), + ], + } + ) + html_output = GT(df).as_raw_html() - + assert 'title="Number One"' in html_output assert 'title="Number Two"' in html_output - assert 'cursor: help' in html_output - assert 'text-decoration-style: dotted' in html_output - assert 'text-decoration-style: solid' in html_output - assert 'color: blue' in html_output - assert 'color: red' in html_output \ No newline at end of file + assert "cursor: help" in html_output + assert "text-decoration-style: dotted" in html_output + assert "text-decoration-style: solid" in html_output + assert "color: blue" in html_output + assert "color: red" in html_output diff --git a/gt_extras/tests/test_icons.py b/gt_extras/tests/test_icons.py index 24f6c62..9593397 100644 --- a/gt_extras/tests/test_icons.py +++ b/gt_extras/tests/test_icons.py @@ -135,3 +135,10 @@ def test_gt_fa_rating_multiple_columns(): assert html.count(" ' in html + def test_theme_pff_rank_col(mini_gt): themed_gt = gt_theme_pff(gt=mini_gt, rank_col="num") html = themed_gt.as_raw_html() - assert "#e4e8ec" in html \ No newline at end of file + assert "#e4e8ec" in html