Use key as main identity for st.file_uploader widget#12955
Merged
lukasmasuch merged 3 commits intodevelopfrom Nov 6, 2025
Merged
Use key as main identity for st.file_uploader widget#12955lukasmasuch merged 3 commits intodevelopfrom
st.file_uploader widget#12955lukasmasuch merged 3 commits intodevelopfrom
Conversation
Contributor
✅ 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. |
Contributor
✅ PR preview is ready!
|
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR implements stable widget IDs for st.file_uploader when a key is provided. The change ensures that the widget ID remains stable across reruns when non-critical parameters change, while still invalidating the ID when parameters that affect file compatibility (type and accept_multiple_files) are modified.
Key Changes
- Modified
key_as_main_identityparameter fromFalseto a set containing{"type", "accept_multiple_files"}for the file uploader - Added Python unit tests to verify stable ID behavior with whitelisted parameters
- Added E2E test to verify that widget state persists when dynamic properties are updated
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
lib/streamlit/elements/widgets/file_uploader.py |
Changed key_as_main_identity from False to {"type", "accept_multiple_files"} with explanatory comment |
lib/tests/streamlit/elements/file_uploader_test.py |
Added FileUploaderStableIdTest class with tests for stable ID behavior and whitelisted parameter changes |
e2e_playwright/st_file_uploader.py |
Added dynamic file uploader test case with toggle to update properties |
e2e_playwright/st_file_uploader_test.py |
Added E2E test verifying state persistence when updating file uploader properties dynamically |
sfc-gh-bnisco
approved these changes
Nov 6, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your changes
If a custom key is passed to a
st.file_uploaderwidget it will be used as the main identity. This allows for dynamically changing other parameters in between reruns without causing the widgets to be recreated in the frontend.GitHub 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.
Note
Treats
st.file_uploaderkey as the main identity (except fortype/accept_multiple_files), preserving state across prop changes; adds unit/e2e coverage and demo for dynamic prop updates.lib/streamlit/elements/widgets/file_uploader.py, compute element ID withkey_as_main_identity={"type", "accept_multiple_files"}so the widget key defines identity while only changes totypeoraccept_multiple_filesalter it.FileUploaderStableIdTestinlib/tests/streamlit/elements/file_uploader_test.pyto assert stable IDs with a key and ID changes whentype/accept_multiple_fileschange.e2e_playwright/st_file_uploader.py, add a toggle-driven dynamic uploader (key="dynamic_file_uploader_key") that updates label/help/width while keepingtypeandaccept_multiple_filesconstant.e2e_playwright/st_file_uploader_test.py, addtest_dynamic_file_uploader_props, helpers usage, and snapshot checks; update uploader count toNUM_FILE_UPLOADERS = 17and adjust assertions accordingly.Written by Cursor Bugbot for commit ebf3223. This will update automatically on new commits. Configure here.