Skip to content

Conversation

@lukasmasuch
Copy link
Collaborator

@lukasmasuch lukasmasuch commented Dec 17, 2025

Describe your changes

Adds support for pre-defined formats in format of st.slider. These are the same formats also supported in NumberColumn, DateTimeColumn, DateColumn, TimeColumn, ProgressColumn, and st.metric.

Testing Plan

  • Added unit tests.

Contribution License Agreement

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

Copilot AI review requested due to automatic review settings December 17, 2025 09:24
@snyk-io
Copy link
Contributor

snyk-io bot commented Dec 17, 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.

@lukasmasuch lukasmasuch added security-assessment-completed Security assessment has been completed for PR change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Dec 17, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 17, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13392/streamlit-1.52.1-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13392.streamlit.app (☁️ Deploy here if not accessible)

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 support for predefined format options in the format parameter of st.slider, bringing it in line with similar formatting capabilities already available in column configs (DateTimeColumn, DateColumn, TimeColumn, NumberColumn, ProgressColumn) and st.metric.

Key changes:

  • Extended Python type signatures to accept NumberFormat for numeric sliders and DateTimeFormat for date-like sliders
  • Created reusable formatMoment utility in TypeScript to handle moment formatting logic
  • Updated slider documentation with comprehensive examples of predefined formats
  • Refactored DataFrame column utilities to use the shared formatMoment utility

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
lib/streamlit/elements/widgets/slider.py Updated type annotations to accept NumberFormat and DateTimeFormat predefined formats, and expanded documentation with examples of all supported format options
lib/streamlit/elements/lib/column_types.py Introduced DateTimeFormat type alias and refactored DatetimeColumn to use it for consistency
frontend/lib/src/util/formatMoment.ts New utility module extracted from DataFrame columns for formatting moment dates with support for predefined formats ("localized", "distance", "calendar", "iso8601")
frontend/lib/src/util/formatMoment.test.ts Comprehensive test suite for the new formatMoment utility covering various format strings and momentKind options
frontend/lib/src/components/widgets/Slider/Slider.tsx Refactored formatValue to use formatNumber and formatMoment utilities, replacing direct sprintf usage and enabling predefined format support
frontend/lib/src/components/widgets/DataFrame/columns/utils.ts Removed duplicated formatMoment implementation and imported from the new shared utility module, with re-export for backward compatibility

)
})

withTimezones(() => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

just moved this into a dedicated shared file

* Otherwise, it is interpreted as momentJS format string: https://momentjs.com/docs/#/displaying/format/
* @returns The formatted date as a string.
*/
export function formatMoment(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just moved this into a dedicated shared file

@lukasmasuch lukasmasuch added the ai-review If applied to PR or issue will run AI review workflow label Dec 22, 2025
@github-actions github-actions bot removed the ai-review If applied to PR or issue will run AI review workflow label Dec 22, 2025
@github-actions
Copy link
Contributor

Summary

This PR adds support for pre-defined format strings in the format parameter of st.slider. Previously, users could only use printf-style format strings (for numbers) or momentJS format strings (for datetimes). Now they can also use predefined format names like "compact", "percent", "localized", etc. This brings the slider's formatting capabilities in line with other Streamlit components like st.metric and the DataFrame column types.

Key Changes:

  1. Python Backend: Updated type hints to accept NumberFormat or DateTimeFormat literals in addition to arbitrary strings. Updated docstrings with comprehensive documentation of all available formats.
  2. Frontend Refactoring: Extracted formatMoment function from utils.ts into a new dedicated utility file formatMoment.ts for better code organization and reusability.
  3. Slider Component: Updated to use both formatNumber and formatMoment utilities for value formatting.
  4. Tests: Added unit tests, typing tests, and E2E snapshot tests.

Code Quality

Python Changes

Well-Implemented:

  • Type hints properly updated across all overloads in slider.py (lines 233, 255, 277, 299, 321, 344, 365, 389, 411)
  • Docstring updated with clear documentation of all predefined formats (lines 520-561)
  • Imports the NumberFormat and DateTimeFormat type aliases from column_types.py (line 65), reusing existing definitions
  • The implementation doesn't require any changes to the core logic since the format string is passed directly to the frontend

One Minor Observation:

  • The implementation signature at line 432 uses format: str | None while the overloads use the more specific types. This is correct behavior (implementation accepts the union of all overload types) but worth noting for clarity.

Frontend Changes

Well-Implemented:

  • Clean refactoring: formatMoment was extracted from utils.ts into its own utility file formatMoment.ts with proper exports
  • Added MomentKind type export for better type safety
  • Slider.tsx (line 52) properly imports and uses the new utility
  • DateTimeColumn.ts updated to import from the new location
  • Tests moved appropriately from utils.test.ts to formatMoment.test.ts
  • The formatMoment function now accepts a momentKind parameter to properly handle date/time/datetime formatting for iso8601 and localized formats

Code Organization

  • The refactoring follows good separation of concerns - formatting utilities are now in dedicated files
  • No duplicate code introduced

Test Coverage

Unit Tests (Python)

  • test_format_none - Tests default format behavior
  • test_format_numeric_values - Parameterized test covering all 11 predefined numeric formats plus 3 printf-style formats
  • test_format_datetime_values - Parameterized test covering all 4 predefined datetime formats plus 2 momentJS formats
  • Tests follow best practices with @parameterized.expand for good coverage with minimal code duplication

Typing Tests (Python)

  • ✅ Added comprehensive typing tests in slider_types.py (lines 346-373)
  • Tests all predefined NumberFormat literals with int values
  • Tests all predefined DateTimeFormat literals with datetime/date/time values
  • Ensures type inference works correctly with format parameters

Frontend Tests

  • ✅ Tests for formatMoment moved to dedicated formatMoment.test.ts
  • Tests cover localized, distance, calendar, and iso8601 formats with various timezones
  • Tests properly use fake timers for consistent results

E2E Tests

  • ✅ Added 2 new sliders to st_slider.py demonstrating predefined formats:
    • Numeric slider with "compact" format
    • Datetime slider with "localized" format
  • ✅ Added snapshot tests for both new sliders in st_slider_test.py
  • ✅ Updated NUM_SLIDER_WIDGETS constant from 25 to 27
  • Snapshot naming follows convention: st_slider-compact_format, st_slider-localized_date_format

E2E tests follow best practices:

  • Uses themed_app fixture for light/dark mode coverage
  • Uses label-based locator via get_slider(themed_app, "Slider with compact format")
  • Snapshots specific elements rather than full page

Backwards Compatibility

Fully Backwards Compatible

  • Existing printf-style format strings (e.g., "%d", "%.2f") continue to work unchanged
  • Existing momentJS format strings (e.g., "YYYY-MM-DD", "ddd ha") continue to work unchanged
  • The format parameter remains optional with the same defaults
  • No changes to return values or behavior
  • No changes to the protobuf schema - format is still passed as a string

Security & Risk

Low Risk:

  • No new external dependencies introduced
  • Format strings are handled by existing, well-tested libraries (sprintf.js, numbro, moment.js, Intl.NumberFormat)
  • No user input validation changes that could introduce security issues
  • The predefined format names are just string literals that map to existing formatting logic

No Regression Risk:

  • The frontend formatting utilities (formatNumber, formatMoment) are existing, battle-tested code
  • The only change is that the slider now uses these utilities for formatting
  • Comprehensive test coverage ensures existing functionality is preserved

Recommendations

No blocking issues found. The implementation is clean, well-tested, and follows existing patterns in the codebase.

Optional improvements for future consideration (not blocking):

  1. Additional E2E coverage: While the current E2E tests cover representative formats (compact and localized), the team could consider adding more E2E tests for other formats (e.g., percent, dollar, distance) in a follow-up PR if visual regression testing for all formats is desired.

  2. Documentation: Consider adding examples to the Streamlit documentation showing the visual output of different predefined formats.

Verdict

APPROVED: This PR cleanly adds pre-defined format support to st.slider, is fully backwards compatible, has comprehensive test coverage, and follows existing code patterns. The code refactoring improves the overall codebase organization by extracting shared utilities.


This is an automated AI review. Please verify the feedback and use your judgment.

@lukasmasuch lukasmasuch merged commit e3023b3 into develop Dec 23, 2025
50 of 51 checks passed
@lukasmasuch lukasmasuch deleted the feature/pre-defined-formats-for-slider branch December 23, 2025 12:54
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support pre-defined formats in format parameter of st.slider

3 participants