-
Notifications
You must be signed in to change notification settings - Fork 4k
Fix #10475: Warn when modifying elements inside fragment callback #10942
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
Fix #10475: Warn when modifying elements inside fragment callback #10942
Conversation
…lback Raise a warning when a developer modifies elements (such as st.markdown, st.write, etc.) inside a fragment widget's callback. Modifying elements during a fragment callback can lead to unexpected behavior, such as elements disappearing or causing inconsistent UI updates. Furthermore, it simply doesn't make sense, as the purpose of a fragment is isolation and preventing full app reruns when a widget callback interacts with components outside the fragment. The new function _maybe_print_fragment_callback_warning checks if the context indicates that the code is running inside a fragment callback (in_fragment_callback is True) and raises a warning if any element is modified. The warning includes helpful guidance for developers on how to structure their callbacks to avoid these issues. Three unit tests were written to test the appearance of the warning in a fragment callback, and no appearance of the warning when running a callback without a fragment or when running a callback that doesn't interact with elements.
🎉 Snyk checks have passed. No issues have been found so far.✅ security/snyk check is complete. No issues have been found. (View Details) ✅ license/snyk check is complete. No issues have been found. (View Details) |
sfc-gh-bnisco
left a comment
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.
Thank you @Zane-dev16 for this contribution! Overall, the direction here looks good! I left a comment inline around the content of the warn statement. Please take a look and let me know if you have any questions, I'd be happy to help answer them. Thanks!
|
Thanks for the feedback @sfc-gh-bnisco. I've updated the warning message: 3a3847a |
Co-authored-by: Debbie Matthews <[email protected]>
sfc-gh-bnisco
left a comment
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.
Thanks for updating @Zane-dev16 - The tests will need to be updated since the wording has changed. The failing tests are flagged inline on the lib/tests/streamlit/runtime/state/session_state_test.py file
|
Thanks for the feedback @sfc-gh-bnisco. I've updated the test for the new warning. |
lib/streamlit/delta_generator.py
Outdated
| ) | ||
|
|
||
|
|
||
| def _maybe_print_fragment_callback_warning(delta_type: str) -> None: |
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.
suggestion: Hey @Zane-dev16 - one last request for you, sorry I didn't catch this earlier. We can remove the delta_type argument now since it's not being used now with the modified warning copy.
We've been working on adding more automated linting. If you merge the latest upstream develop branch into this branch, I believe this will become a lint error.
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.
Hi @sfc-gh-bnisco, I didn't catch that either. I've made the change
sfc-gh-bnisco
left a comment
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.
Thank you for your contribution and iterations! LGTM 👍
Describe your changes
Added a new warning mechanism to detect and alert developers when modifying Streamlit elements inside a fragment widget's callback. This addresses potential unexpected UI behaviors and improves developer guidance for proper fragment usage.
Key modifications:
_maybe_print_fragment_callback_warning()functionGitHub Issue Link
Issue #10475
Testing Plan
Unit Tests Added
Contribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.