Skip to content

Use key as main identity for st.file_uploader widget#12955

Merged
lukasmasuch merged 3 commits intodevelopfrom
stable-key-identity-for-file-uploader
Nov 6, 2025
Merged

Use key as main identity for st.file_uploader widget#12955
lukasmasuch merged 3 commits intodevelopfrom
stable-key-identity-for-file-uploader

Conversation

@lukasmasuch
Copy link
Copy Markdown
Collaborator

@lukasmasuch lukasmasuch commented Nov 6, 2025

Describe your changes

If a custom key is passed to a st.file_uploader widget 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

  • Added unit & e2e test.

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_uploader key as the main identity (except for type/accept_multiple_files), preserving state across prop changes; adds unit/e2e coverage and demo for dynamic prop updates.

  • Core behavior:
    • In lib/streamlit/elements/widgets/file_uploader.py, compute element ID with key_as_main_identity={"type", "accept_multiple_files"} so the widget key defines identity while only changes to type or accept_multiple_files alter it.
  • Unit tests:
    • Add FileUploaderStableIdTest in lib/tests/streamlit/elements/file_uploader_test.py to assert stable IDs with a key and ID changes when type/accept_multiple_files change.
  • E2E app & tests:
    • In e2e_playwright/st_file_uploader.py, add a toggle-driven dynamic uploader (key="dynamic_file_uploader_key") that updates label/help/width while keeping type and accept_multiple_files constant.
    • In e2e_playwright/st_file_uploader_test.py, add test_dynamic_file_uploader_props, helpers usage, and snapshot checks; update uploader count to NUM_FILE_UPLOADERS = 17 and adjust assertions accordingly.

Written by Cursor Bugbot for commit ebf3223. This will update automatically on new commits. Configure here.

@lukasmasuch lukasmasuch requested a review from Copilot November 6, 2025 01:24
@snyk-io
Copy link
Copy Markdown
Contributor

snyk-io bot commented Nov 6, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Licenses 0 0 0 0 0 issues
Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 6, 2025

✅ PR preview is ready!

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

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 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_identity parameter from False to 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

@lukasmasuch lukasmasuch added security-assessment-completed change:feature PR contains new feature or enhancement implementation impact:users PR changes affect end users labels Nov 6, 2025
@lukasmasuch lukasmasuch merged commit af28be9 into develop Nov 6, 2025
39 checks passed
@lukasmasuch lukasmasuch deleted the stable-key-identity-for-file-uploader branch November 6, 2025 19:20
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants