Skip to content

Commit

Permalink
Temp solution to fix invalid material icon error rendering (#9113)
Browse files Browse the repository at this point in the history
## Describe your changes

Temp fix for invalid material icon StreamlitAPIException rendering

## GitHub Issue Link (if applicable)

## Testing Plan

- Explanation of why no additional tests are needed
- Unit Tests (JS and/or Python)
- E2E Tests
- Any manual testing needed?

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
  • Loading branch information
kajarenc authored Jul 19, 2024
1 parent b2c88c6 commit 898fd80
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 8 additions & 4 deletions lib/streamlit/string_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ def validate_material_icon(maybe_material_icon: str | None) -> str:

icon_regex = r"^\s*:(.+)\/(.+):\s*$"
icon_match = re.match(icon_regex, maybe_material_icon)
# Since our markdown processing needs to change the `/` to `_` in order to
# correctly render the icon, we need to add a zero-width space before the
# `/` to avoid this transformation here.
invisible_white_space = "\u200b"

if not icon_match:
raise StreamlitAPIException(
f'The value `"{maybe_material_icon}"` is not a valid Material icon. '
f"Please use a Material icon shortcode like **`:material/thumb_up:`**"
f'The value `"{maybe_material_icon.replace("/", invisible_white_space + "/")}"` is not a valid Material icon. '
f"Please use a Material icon shortcode like **`:material{invisible_white_space}/thumb_up:`**"
)

pack_name, icon_name = icon_match.groups()
Expand All @@ -109,8 +113,8 @@ def validate_material_icon(maybe_material_icon: str | None) -> str:
or not is_material_icon(icon_name)
):
raise StreamlitAPIException(
f'The value `"{maybe_material_icon}"` is not a valid Material icon.'
f" Please use a Material icon shortcode like **`:material/thumb_up:`**. "
f'The value `"{maybe_material_icon.replace("/", invisible_white_space + "/")}"` is not a valid Material icon.'
f" Please use a Material icon shortcode like **`:material{invisible_white_space}/thumb_up:`**. "
)

return f":{pack_name}/{icon_name}:"
Expand Down
5 changes: 3 additions & 2 deletions lib/tests/streamlit/elements/layouts_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,13 @@ def test_invalid_emoji_icon(self):
def test_invalid_material_icon(self):
"""Test that it throws an error on invalid material icon"""
icon = ":material/invalid:"
invisible_white_space = "\u200b"
with self.assertRaises(StreamlitAPIException) as e:
st.expander("label", icon=icon)
self.assertEqual(
str(e.exception),
f'The value `"{icon}"` is not a valid Material icon.'
f" Please use a Material icon shortcode like **`:material/thumb_up:`**. ",
f'The value `"{icon.replace("/", invisible_white_space + "/")}"` is not a valid Material icon.'
f" Please use a Material icon shortcode like **`:material{invisible_white_space}/thumb_up:`**. ",
)


Expand Down

0 comments on commit 898fd80

Please sign in to comment.