-
Notifications
You must be signed in to change notification settings - Fork 4k
[feat] Add help argument and tooltip to st.badge #12897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Add help argument and tooltip to st.badge #12897
Conversation
## Describe your changes This PR adds tooltip support for the `st.badge` component. Badges can now display help text when hovered over using the same `BaseButtonTooltip` as other Streamlit elements. Key changes: - Added `BaseButtonTooltip` wrapper for badges with `help` text - Added functionality so that multiple badges or badges with surrounding text in `st.markdown()` continue to use the inline tooltip instead of the `BaseButtonTooltip` - Maintained consistent styling for new `st.button` tooltip by using `BaseButtonTooltip` - Ensured backward compatibility for existing badges without tooltips ## GitHub Issue Link streamlit#12878 ## Testing Plan - Verified that badges with and without tooltips render correctly - Tested hover behaviour and tooltip positioning - Confirmed multi-badge and badges with text in Markdown still use inline tooltip - Confirmed no regressions in other Markdown-rendered elements --- **Contribution License Agreement** By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
|
Hey @marcolanfranchi! Thanks for the contribution. I just tried this out and it looks good from product side. Will pass this on to our engineers for code review. |
|
@jrieke No problem, thanks for trying it out! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds tooltip support to the st.badge() API, enabling users to display helpful information when hovering over badges. The implementation distinguishes between single badges (which use BaseButtonTooltip) and multiple badges or badges with additional text (which use inline tooltip icons).
Key changes:
- Added
helpparameter to Pythonst.badge()function - Frontend logic detects single badges vs. multiple badges to render appropriate tooltip style
- Comprehensive test coverage for badge tooltip behavior
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/streamlit/elements/markdown.py | Added help parameter to badge() function with documentation and proto assignment |
| frontend/lib/src/components/elements/Markdown/Markdown.tsx | Added single-badge detection logic and conditional tooltip rendering (BaseButtonTooltip for single badges, inline tooltip for others) |
| frontend/lib/src/components/elements/Markdown/Markdown.test.tsx | Added three test cases covering badge tooltip scenarios: with tooltip, without tooltip, and multiple badges |
- Moved 'help' docstring below 'width' to match function signature order - Updated regex for markdown badges to correctly handle escaped brackets and backslashes in labels - Added new Markdown tests for badges with special characters and badges with additional text
|
Hey @sfc-gh-bnisco, thanks for requesting a review. I tried out and implemented the suggested changes and added some more test cases to cover special markdown badge string cases. I appreciate the help! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
lib/streamlit/elements/markdown.py:479
- The new
helpparameter for thebadgefunction lacks Python unit test coverage. While the frontend tests cover the rendering behavior, there should be a test inlib/tests/streamlit/elements/markdown_test.pyverifying that thehelpvalue is correctly set in the protobuf message when provided (and not set whenNone).
if help is not None:
badge_proto.help = help
- Added Python unit tests verifying 'help' parameter is correctly set in badge protobuf and omitted when None - Put single badge regex into a named constant with a descriptive comment that explains what it matches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
- Add a more detailed description of the single badge regex Co-authored-by: Copilot <[email protected]>
- Only run single-badge regex when help text is provided to avoid unnecessary regex execution on most renders
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
frontend/lib/src/components/elements/Markdown/Markdown.test.tsx
Outdated
Show resolved
Hide resolved
|
Skipping Copilot’s latest [nitpick] suggestions about test description, however I'm happy to adjust if reviewers prefer more detail. |
|
Thanks @marcolanfranchi , I made a few small tweaks to this. Should be good to go once CI is green. Thank you for your contribution! |
|
@sfc-gh-bnisco Awesome, thanks for making those tweaks. And no problem, very glad I could contribute! |
Describe your changes
This PR adds tooltip support for the
st.badgecomponent. Badges can now display help text when hovered over using the sameBaseButtonTooltipas other Streamlit elements.Key changes include:
BaseButtonTooltipwrapper for badges withhelptextst.markdown()continue to use the inline tooltip (with '?' icon) instead of theBaseButtonTooltipst.markdown(':grey-badge[Label]', help='...')(st.markdown containing a single badge after trimming whitespace with help) will now behave the same as st.badge('...', help='...') in the demo above, using theBaseButtonTooltip.st.badgetooltip by usingBaseButtonTooltipGitHub Issue Link
#12878
Testing Plan
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.