-
Notifications
You must be signed in to change notification settings - Fork 4k
Support configuring the format in st.metric
#13193
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
Conversation
✅ 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. |
✅ PR preview is ready!
|
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.
This is mostly an extraction of the formatNumber functionality from the dataframe module to a shared module.
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 support for configuring the format parameter in st.metric to control how numeric values and deltas are displayed. The implementation enables users to format numbers using predefined formats (e.g., "dollar", "percent", "compact") or custom printf-style format strings (e.g., "%.2f").
Key changes:
- Added
formatfield to the Metric protobuf message - Extended
st.metricPython API withformatparameter supporting predefined formats and printf-style strings - Refactored
formatNumberfunction into a shared utility module (frontend/lib/src/util/formatNumber.ts) - Added logic in the Metric React component to conditionally apply formatting to numeric values and deltas
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
proto/streamlit/proto/Metric.proto |
Added optional format field to enable number formatting configuration |
lib/streamlit/elements/metric.py |
Added format parameter to metric function with comprehensive documentation |
lib/tests/streamlit/elements/metric_test.py |
Added unit tests for format parameter with various predefined and printf-style formats |
frontend/lib/src/util/formatNumber.ts |
Extracted and consolidated formatNumber utility from DataFrame utils into shared module |
frontend/lib/src/util/formatNumber.test.ts |
Comprehensive test coverage for formatNumber function including all format types |
frontend/lib/src/components/widgets/DataFrame/columns/utils.ts |
Removed formatNumber implementation (moved to shared utility) |
frontend/lib/src/components/widgets/DataFrame/columns/utils.test.ts |
Removed formatNumber tests (moved to new location) |
frontend/lib/src/components/widgets/DataFrame/columns/ProgressColumn.ts |
Updated import to use formatNumber from new shared utility location |
frontend/lib/src/components/widgets/DataFrame/columns/NumberColumn.ts |
Updated import to use formatNumber from new shared utility location |
frontend/lib/src/components/widgets/DataFrame/columns/ChartColumn.ts |
Updated import to use formatNumber from new shared utility location |
frontend/lib/src/components/elements/Metric/Metric.tsx |
Added conditional number formatting logic using isNumericString and formatNumber |
frontend/lib/src/components/elements/Metric/Metric.test.tsx |
Added comprehensive tests for format parameter behavior |
e2e_playwright/st_metric_test.py |
Added E2E test to verify format rendering in the UI |
e2e_playwright/st_metric.py |
Added test cases with various format types for visual verification |
lib/streamlit/elements/metric.py
Outdated
| shorten it using packages like `millify <https://github.com/azaitsev/millify>`_ | ||
| or `numerize <https://github.com/davidsa03/numerize>`_. E.g. ``1234`` can be | ||
| displayed as ``1.2k`` using ``st.metric("Short number", millify(1234))``. | ||
| Tip: If you want to display a large number, it may be a good idea to |
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.
question: Are all of these indentation changes intentional?
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.
Oh, good catch... I don't think so. Not sure what happened here
| shorten it using packages like `millify <https://github.com/azaitsev/millify>`_ | ||
| or `numerize <https://github.com/davidsa03/numerize>`_. E.g. ``1234`` can be | ||
| displayed as ``1.2k`` using ``st.metric("Short number", millify(1234))``. | ||
| Tip: If you want to display a large number, it may be a good idea to |
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.
The existing docstring was a bit broken
|
Thank you for this! Great work. |
Describe your changes
Adds support for configuring a format applied to the value and delta on client-side. This supports the same formats that are supported in
NumberColumnandProgressColumn.GitHub Issue Link (if applicable)
st.metric#12229format_funcforst.metric#6951Testing 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.
Note
Adds a
formatoption tost.metricand applies client-side number formatting for value and delta, with shared formatting utilities and tests.formatparameter tost.metric(Python) andMetricproto (formatfield) to control value/delta formatting.formatNumberwithisNumericStringchecks and safe fallback; renderformattedMetricValue/formattedDelta.~lib/util/formatNumber(formatting +isNumericString) with comprehensive tests.NumberColumn,ProgressColumn,ChartColumn) to use new util; remove inlinedformatNumberfromcolumns/utils.ts.proto/Metric.proto: addstring format = 11.formatoptions ine2e_playwright/st_metric.py.Written by Cursor Bugbot for commit d308c0a. Configure here.