Skip to content

feat: Add configurable font size and weight for metric values (fixes #12300)Theming metric font size weight#12344

Closed
DHANUSHRAJA22 wants to merge 4 commits intostreamlit:developfrom
DHANUSHRAJA22:theming-metric-font-size-weight
Closed

feat: Add configurable font size and weight for metric values (fixes #12300)Theming metric font size weight#12344
DHANUSHRAJA22 wants to merge 4 commits intostreamlit:developfrom
DHANUSHRAJA22:theming-metric-font-size-weight

Conversation

@DHANUSHRAJA22
Copy link
Copy Markdown

Summary

This PR implements customizable font size and weight for metric values, addressing issue #12300. Users can now configure metric_value_font_size and metric_value_font_weight in their theme settings to customize the appearance of metric components.

Changes Made

Backend Changes

  • Added metric_value_font_size and metric_value_font_weight fields to the CustomThemeConfig protobuf
  • Created validation functions in theme_util.py to ensure proper CSS font values (size > 0, weight 100-900)
  • Added parsing functions to handle theme configuration from config.toml files

Frontend Changes

  • Extended EmotionTheme interface with new optional properties: metricValueFontSize and metricValueFontWeight
  • Updated StyledMetricValueText component to use theme-specific font properties with fallback to defaults
  • Maintained backward compatibility by using nullish coalescing operator (??)

Testing

  • Existing tests should pass as changes maintain backward compatibility
  • Manual testing recommended to verify font customization works correctly
  • The implementation uses proper fallbacks so existing metrics will continue to render with default styling

Configuration Example

Users can now add this to their config.toml:

[theme]
metric_value_font_size = 32
metric_value_font_weight = 600

Closes #12300## Describe your changes

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.

…metric value font size and weight fields to CustomThemeConfigUpdate NewSession.proto
…ont config parsing functions to theme_util.pyUpdate theme_util.py

- Added _parse_metric_font_config() function to validate metric font size and weight parameters
- Added parse_theme_config() function to process theme configuration from dictionaries  
- Added apply_theme_config_from_toml() function to handle config.toml theme parsing
- These functions support the new 'metric_value_font_size' and 'metric_value_font_weight' parameters
- Includes comprehensive validation and error handling with proper comments for maintainability
- Enables passing these new config parameters from config.toml or Python theme dicts into CustomThemeConfig
…onThemeAdd metricValueFontSize and metricValueFontWeight to EmotionTheme interfaceAdd metricValueFontSize and metricValueFontWeight to EmotionTheme interfaceUpdate types.ts

- Added optional metricValueFontSize property (number) for customizing metric value font size in pixels
- Added optional metricValueFontWeight property (number) for customizing metric value font weight (100-900)
- Added comprehensive comments for clarity and future maintainability
- These properties extend the EmotionTheme interface to support metric component styling customization
…dMetricValueText to use theme font propertiesUpdate styled-components.ts

- Updated StyledMetricValueText to use theme.metricValueFontSize with fallback to theme.fontSizes.threeXL
- Updated StyledMetricValueText to use theme.metricValueFontWeight with fallback to theme.fontWeights.bold
- Added inline comments for better maintainability
- Enables customizable metric value font styling through theme configuration
@DHANUSHRAJA22 DHANUSHRAJA22 requested a review from a team as a code owner August 27, 2025 10:37
@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Aug 27, 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)

@lukasmasuch lukasmasuch added change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users status:needs-product-approval PR requires product approval before merging labels Aug 27, 2025
@lukasmasuch lukasmasuch requested a review from Copilot August 27, 2025 10:41
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 implements configurable font size and weight for metric values to address issue #12300. Users can now customize the appearance of metric components through theme configuration settings.

  • Adds metric_value_font_size and metric_value_font_weight theme configuration options
  • Implements validation for CSS font property values (size > 0, weight 100-900)
  • Updates frontend components to use theme-specific styling with backward compatibility

Reviewed Changes

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

File Description
proto/streamlit/proto/NewSession.proto Added metric_value_font_size and metric_value_font_weight fields to CustomThemeConfig
lib/streamlit/runtime/theme_util.py Added validation and parsing functions for metric font configuration
frontend/lib/src/theme/types.ts Extended EmotionTheme interface with optional metric font properties
frontend/lib/src/components/elements/Metric/styled-components.ts Updated metric value styling to use theme properties with fallbacks
Comments suppressed due to low confidence (1)

Comment on lines +95 to +98

This function validates the font size and weight values for metric components
to ensure they fall within acceptable ranges for CSS font properties.

Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

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

The docstring should follow the existing pattern in the file with proper spacing. There should be no blank line before 'Args:' section.

Suggested change
This function validates the font size and weight values for metric components
to ensure they fall within acceptable ranges for CSS font properties.
This function validates the font size and weight values for metric components
to ensure they fall within acceptable ranges for CSS font properties.

Copilot uses AI. Check for mistakes.
@sfc-gh-jrieke
Copy link
Copy Markdown
Contributor

Hey, thanks for the contribution! Can you convert the config option names to camel case, e.g. metricValueFontSize, just like all other config options?

@jrieke jrieke added the status:waiting-for-requested-changes PR has changes requested and awaiting submitter update label Oct 31, 2025
@jrieke
Copy link
Copy Markdown
Collaborator

jrieke commented Dec 29, 2025

Going to close this because it seems to be stale. Feel free to reopen in case you want to continue working on this!

@jrieke jrieke closed this Dec 29, 2025
sfc-gh-lwilby added a commit that referenced this pull request Feb 9, 2026
…#13550)

## feat: add metricValueFontSize and metricValueFontWeight theme options

Add `metricValueFontSize` and `metricValueFontWeight` theme
configuration options to customize st.metric value text appearance.

### Example configuration

```toml
[theme]
metricValueFontSize = "3rem"    # or "48px" or "48"
metricValueFontWeight = 300
```

### Supported formats for `metricValueFontSize`

| Format | Example | Result |
|--------|---------|--------|
| rem | `"3rem"` | 3rem (48px with 16px base) |
| px | `"48px"` | 48px |
| number (string) | `"48"` | 48px |

Invalid values (negative, zero, invalid format) log a warning and
fallback to the default (2.25rem ≈ 36px).

## Screenshots

### Before (Default)
`metricValueFontSize` not set → uses default (2.25rem ≈ 36px)

<img width="2072" height="3516" alt="metric_before"
src="https://github.com/user-attachments/assets/b8c3097f-adf6-4e31-8b23-27777425c0ce"
/>

### Supported formats
Shows valid values (`"48px"`, `"3rem"`, `"36"`, `"24px"`) and invalid
values that fallback to default (`"-10px"`, `"0"`, `"aaa"`).

<img width="2072" height="3516" alt="pr_screenshot_metricValueFontSize"
src="https://github.com/user-attachments/assets/076ea5a8-5147-429f-a749-87cdcb5eb64c"
/>

## GitHub Issue Link (if applicable)

Closes #12300

## Testing Plan

- [x] Unit Tests (JS and Python)
- Theme utils tests for font size/weight validation (17 test cases for
metricValueFontSize)
  - Metric component tests for styling
  - Python config tests
  - Proto compatibility tests
- [x] E2E test
(`e2e_playwright/theming/theme_metric_value_style_test.py`)
  - Verifies rem string parsing (3rem → 48px)
  - Verifies CSS properties are applied correctly
  - Snapshot test for visual verification
- [x] Manual testing with custom config.toml

### Notes

- **rem/px support**: `metricValueFontSize` accepts string values with
rem/px units (like `headingFontSizes`).
- **Backward compatibility**: When options are not configured, the
default behavior is unchanged. No breaking changes.
- **Previous PR**: This is a fresh implementation addressing feedback
from #12344 (using camelCase for config options).
- **Documentation**: Based on other PRs, docs updates are typically
handled in separate issues. Happy to take on a docs issue if assigned.

---------

Co-authored-by: Laura Wilby <[email protected]>
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:needs-product-approval PR requires product approval before merging status:waiting-for-requested-changes PR has changes requested and awaiting submitter update

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change st.metric font size and weight through theming

5 participants