Skip to content

Metric supports numerics like decimal.#12377

Merged
lukasmasuch merged 16 commits intostreamlit:developfrom
aebrahim:decimal
Sep 12, 2025
Merged

Metric supports numerics like decimal.#12377
lukasmasuch merged 16 commits intostreamlit:developfrom
aebrahim:decimal

Conversation

@aebrahim
Copy link
Copy Markdown
Contributor

@aebrahim aebrahim commented Aug 29, 2025

Describe your changes

Add support for numeric types like decimal.Decimal

GitHub Issue Link (if applicable)

Testing Plan

  • Unit test added

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
Copy Markdown
Contributor

snyk-io bot commented Aug 29, 2025

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

security/snyk check is complete. No issues have been found. (View Details)

license/snyk check is complete. No issues have been found. (View Details)

aebrahim and others added 3 commits August 29, 2025 12:08
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
@lukasmasuch lukasmasuch requested a review from Copilot August 29, 2025 19:52
@lukasmasuch lukasmasuch added security-assessment-completed change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Aug 29, 2025

This comment was marked as outdated.

@lukasmasuch lukasmasuch added status:needs-product-approval PR requires product approval before merging and removed security-assessment-completed labels Aug 29, 2025
@aebrahim aebrahim requested a review from Copilot August 29, 2025 21:45
Copy link
Copy Markdown
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 support for numeric types like decimal.Decimal to the Streamlit metric element by expanding the accepted value types beyond the previously supported int, float, and str types.

Key Changes

  • Extracts numeric-to-string conversion logic into a reusable from_number utility function in string_util.py
  • Updates the metric element to use the new utility function and accept numbers.Number types
  • Updates error messages to reflect the expanded type support

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
lib/streamlit/string_util.py Adds new from_number function and StringCastableNumber type alias to handle numeric-to-string conversion
lib/streamlit/elements/metric.py Refactors _parse_value to use the new utility function and updates type annotations
lib/tests/streamlit/string_util_test.py Adds unit tests for the new from_number function including decimal support
lib/tests/streamlit/elements/metric_test.py Updates error message test to reflect new supported types

@jrieke
Copy link
Copy Markdown
Collaborator

jrieke commented Aug 30, 2025

Hey @aebrahim, thanks for the contribution! Does this PR only add support for decimal in st.metric, or for other elements as well? As I mentioned on the issue, if we want to support this, we should support it everywhere where we accept numbers.

@aebrahim
Copy link
Copy Markdown
Contributor Author

That makes a lot of sense!

I only added it to st.metric, but by doing it in a util, I thought that would make it easy to do it for any other place that needs it in the exact same way.

I thought this was the best way to incorporate your feedback and still keep the pull request small.

It would be very easy to move all calls to this util in every display element that needs to be changed in subsequent PR's.

@jrieke
Copy link
Copy Markdown
Collaborator

jrieke commented Aug 30, 2025

Got it. Would you be willing to work on supporting this for other elements as well, or not at the moment? What I want to avoid is that 1) we'll end up with partial support for this just on st.metric and not on any other elements, or 2) putting a major burden on our engineering team to add support for this in all other commands and maintain that (especially given that the issue only has 4 upvotes so far). I'll also discuss with our engineers what their opinion is :)

@jrieke
Copy link
Copy Markdown
Collaborator

jrieke commented Aug 30, 2025

OK just talked to our engineers and we actually do already support decimal for st.dataframe, st.table, and everything else that accepts dataframe-like data (e.g. st.line_chart). So I think we can go ahead with merging this just for st.metric, marking it as approved by product and we'll give this a proper code review then!

The only other element where this might make sense is st.number_input, but since this is probably not used a ton for showing some existing data, it doesn't seem worth supporting that for now.

@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 Aug 30, 2025
@aebrahim
Copy link
Copy Markdown
Contributor Author

Sounds good, and I definitely don't mind doing a follow-up pull request to add it for any other types you need!

@aebrahim
Copy link
Copy Markdown
Contributor Author

aebrahim commented Sep 3, 2025

just bumping to see if there are any other steps I need to take!

@aebrahim
Copy link
Copy Markdown
Contributor Author

aebrahim commented Sep 6, 2025

@jrieke - anything else needed to get this merged?

@aebrahim
Copy link
Copy Markdown
Contributor Author

Bump!

@GhaithqD
Copy link
Copy Markdown

bump

1 similar comment
@Erik-Dan-Tran
Copy link
Copy Markdown

bump

@LeeJMorel
Copy link
Copy Markdown

bump!

Copy link
Copy Markdown
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 4 comments.

lukasmasuch and others added 4 commits September 12, 2025 02:07
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 I added a few tweaks.

@lukasmasuch lukasmasuch merged commit 824b86f into streamlit:develop Sep 12, 2025
37 checks passed
@aebrahim
Copy link
Copy Markdown
Contributor Author

Thank you so much for helping with this! Best data dashboard there is :)

@aebrahim aebrahim deleted the decimal branch September 12, 2025 15:59
@jrieke
Copy link
Copy Markdown
Collaborator

jrieke commented Sep 16, 2025

@aebrahim Congrats on getting this merged! 🥳 Would love to send you some swag as a little thank you, feel free to just fill out this form and we'll get it on the way!

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 status:product-approved Community PR is approved by product team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support decimal.Decimal in st.metric

7 participants