Skip to content

Conversation

@marcolanfranchi
Copy link
Contributor

@marcolanfranchi marcolanfranchi commented Oct 30, 2025

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.

hoverdemo-stbadge

Key changes include:

  • 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 (with '?' icon) instead of the BaseButtonTooltip
    • st.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 the BaseButtonTooltip.
  • Maintained consistent styling for new st.badge tooltip by using BaseButtonTooltip
  • Ensured backward compatibility for existing badges without tooltips

GitHub Issue Link

#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 the inline tooltip
  • Confirmed no regressions in other Markdown-rendered badges

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

## 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-io
Copy link
Contributor

snyk-io bot commented Oct 30, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

marcolanfranchi and others added 2 commits October 30, 2025 02:53
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>
@marcolanfranchi marcolanfranchi changed the title [feat] Add tooltip support for st.badge (#12878) [feat] Add help argument and tooltip to st.badge Oct 30, 2025
@jrieke jrieke added status:needs-product-approval PR requires product approval before merging change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Oct 30, 2025
@jrieke
Copy link
Collaborator

jrieke commented Oct 30, 2025

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 jrieke added status:product-approved Community PR is approved by product team and removed status:needs-product-approval PR requires product approval before merging labels Oct 30, 2025
@marcolanfranchi
Copy link
Contributor Author

@jrieke No problem, thanks for trying it out!

@sfc-gh-bnisco sfc-gh-bnisco requested a review from Copilot October 31, 2025 22:19
Copy link
Contributor

Copilot AI left a 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 help parameter to Python st.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

marcolanfranchi and others added 3 commits October 31, 2025 19:01
- 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
@marcolanfranchi
Copy link
Contributor Author

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!

Copy link
Contributor

Copilot AI left a 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 help parameter for the badge function lacks Python unit test coverage. While the frontend tests cover the rendering behavior, there should be a test in lib/tests/streamlit/elements/markdown_test.py verifying that the help value is correctly set in the protobuf message when provided (and not set when None).
        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
Copy link
Contributor

Copilot AI left a 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.

marcolanfranchi and others added 2 commits November 2, 2025 13:44
- 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
Copy link
Contributor

Copilot AI left a 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.

Copy link
Contributor

Copilot AI left a 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.

@marcolanfranchi
Copy link
Contributor Author

Skipping Copilot’s latest [nitpick] suggestions about test description, however I'm happy to adjust if reviewers prefer more detail.

@sfc-gh-bnisco sfc-gh-bnisco added the security-assessment-completed Security assessment has been completed for PR label Nov 3, 2025
@sfc-gh-bnisco
Copy link
Collaborator

Thanks @marcolanfranchi , I made a few small tweaks to this. Should be good to go once CI is green. Thank you for your contribution!

@marcolanfranchi
Copy link
Contributor Author

@sfc-gh-bnisco Awesome, thanks for making those tweaks. And no problem, very glad I could contribute!

@sfc-gh-bnisco sfc-gh-bnisco merged commit 01e5cbd into streamlit:develop Nov 5, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR status:product-approved Community PR is approved by product team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants