-
Notifications
You must be signed in to change notification settings - Fork 4k
Support passing query params to st.switch_page
#13027
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.
Pull Request Overview
This PR adds support for passing query parameters to st.switch_page, allowing developers to programmatically navigate to pages with specific query string values. This enhances the navigation API by providing a way to pass data between pages via URL parameters.
Key Changes
- Added optional
query_paramsparameter tost.switch_pagethat accepts either a mapping or iterable of key-value pairs - Frontend now stores and applies query parameters from the server during page navigation
- Query parameters are applied before the page rerun, ensuring they're available to the target page
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| lib/streamlit/commands/execution_control.py | Added query_params parameter to switch_page(), including type definitions, validation logic, and updated documentation |
| lib/tests/streamlit/commands/execution_control_test.py | Added unit tests covering valid dict/iterable query params and invalid input rejection |
| frontend/app/src/App.tsx | Updated state management to store and apply query parameters from server during page navigation |
| e2e_playwright/multipage_apps_v2/mpa_v2_basics.py | Added test button and query params display for E2E testing |
| e2e_playwright/multipage_apps_v2/mpa_v2_basics_test.py | Added E2E test to verify query params are correctly applied when switching pages |
…rams-for-switch-page
| if isinstance(new_query_params, Mapping) or ( | ||
| isinstance(new_query_params, Iterable) | ||
| and not isinstance( | ||
| new_query_params, # type: ignore[unreachable] | ||
| (str, bytes), | ||
| ) | ||
| ): |
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 type checker marks this condition as unreachable because by the time the code checks if new_query_params is a string/bytes, it has already confirmed it's an Iterable (since strings are iterable). This could cause unexpected behavior with string inputs.
Consider restructuring the condition to check for string/bytes first:
if isinstance(new_query_params, (str, bytes)):
raise StreamlitAPIException(f"`query_params` must be a mapping or an iterable of (key, value) pairs not a string.")
elif isinstance(new_query_params, Mapping) or isinstance(new_query_params, Iterable):
query_params_state.from_dict(new_query_params)This would make the logic clearer and remove the need for the # type: ignore[unreachable] comment.
| if isinstance(new_query_params, Mapping) or ( | |
| isinstance(new_query_params, Iterable) | |
| and not isinstance( | |
| new_query_params, # type: ignore[unreachable] | |
| (str, bytes), | |
| ) | |
| ): | |
| if isinstance(new_query_params, (str, bytes)): | |
| raise StreamlitAPIException( | |
| f"`query_params` must be a mapping or an iterable of (key, value) pairs not a string." | |
| ) | |
| elif isinstance(new_query_params, Mapping) or isinstance(new_query_params, Iterable): |
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
Describe your changes
Adds support for passing query parameters to
st.switch_pageGitHub Issue Link (if applicable)
Testing 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.