Bind widgets to query params - support empty values for clearable widgets#13815
Bind widgets to query params - support empty values for clearable widgets#13815mayagbarnes wants to merge 4 commits intodevelopfrom
Conversation
✅ PR preview is ready!
|
✅ 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. |
There was a problem hiding this comment.
Pull request overview
This PR extends query-parameter bindings so that widgets can encode a cleared/empty state via empty URL values like ?foo=, with explicit backend/ frontend flags controlling when empties are treated as valid versus invalid. It also tightens the parsing and normalization of empty and partially-empty URL values across Python and TypeScript, and adds targeted unit tests to lock in the new behavior.
Changes:
- Add
allow_emptymetadata on widgets (Python) andclearablebindings (TypeScript) to control whether an empty URL value is a valid state or should be cleared. - Introduce
is_empty_url_valueand augmentparse_url_paramto treat all-empty values ("",[""],["", ""], etc.) as a special “cleared” representation, with type-specific handling for arrays vs scalars. - Update backend seeding and frontend URL sync logic (including URL-writing for empty arrays and nulls) plus comprehensive tests to ensure consistent behavior when widgets clear, reset to defaults, or differ from defaults.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
lib/tests/streamlit/runtime/state/session_state_test.py |
Adds tests around _seed_widget_from_url to confirm that empty URL values seed or clear widgets correctly based on the new allow_empty flag and clear the underlying query param when empties are not allowed. |
lib/tests/streamlit/runtime/state/query_params_test.py |
Extends tests for parse_url_param to cover empty values for array and scalar types and adds coverage for the new is_empty_url_value helper. |
lib/streamlit/runtime/state/widgets.py |
Extends register_widget to accept and document an allow_empty flag, passing it through into WidgetMetadata for later use in URL seeding. |
lib/streamlit/runtime/state/session_state.py |
Imports is_empty_url_value and uses metadata.allow_empty in _seed_widget_from_url so that empty URL values are either accepted (and seeded) or treated as invalid and cleared. |
lib/streamlit/runtime/state/query_params.py |
Adds is_empty_url_value and updates parse_url_param to treat all-empty values as a special cleared state, filter out empty strings from array types, and reserve "" as a non-option sentinel. |
lib/streamlit/runtime/state/common.py |
Extends WidgetMetadata with an allow_empty flag, with documentation tying it to the widget’s UI clearing semantics. |
frontend/lib/src/WidgetStateManager.ts |
Extends query param bindings with an optional clearable flag, adds isEmptyValueValid and enhanced shouldClearUrlParam logic, and tweaks URL update behavior to encode empty arrays as ?key= while still clearing when at default or non-clearable. |
frontend/lib/src/WidgetStateManager.test.ts |
Adds a suite of tests validating URL sync for empty arrays, clearable vs non-clearable widgets, default-based “hide at default” behavior, and the new clearable / fallback semantics. |
6f6ad5d to
c425739
Compare
SummaryThis PR adds support for empty query parameter values (
Code QualityThe code is well-structured with clear function names and follows the Streamlit codebase conventions. Frontend (
Backend (
Minor Observations:
Test CoverageThe test coverage is comprehensive and follows best practices: Python Tests:
TypeScript Tests:
E2E Tests:
Backwards CompatibilityThe changes maintain backwards compatibility:
Note: Existing widget code using Security & RiskNo security concerns identified:
Low regression risk:
AccessibilityNot applicable - this PR focuses on URL state synchronization infrastructure and does not introduce any UI changes or frontend components that would affect accessibility. Recommendations
VerdictAPPROVED: This is a well-implemented infrastructure change with comprehensive test coverage. The design decision to require explicit This is an automated AI review using |
e2363a6 to
7d4486d
Compare
Describe your changes
Adds support for empty query parameter values (
?foo=) in widget URL binding, allowing clearable widgets (multiselect, pills, etc.) to represent their cleared state in the URL.Changes:
Note: We reserve the empty string (
"") to represent the cleared state in query params, so a literal empty‑string option isn’t supported. This avoids ambiguity between?foo=(cleared) and an empty option valueTesting Plan
(Will be able to more explicitly test this behavior in widget implementations to come)