Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Zane-dev16
Copy link
Contributor

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:

  • Implemented _maybe_print_fragment_callback_warning() function
  • Checks if code is running inside a fragment callback
  • Raises a warning when elements are modified during fragment callbacks
  • Provides clear guidance to developers on proper callback structure

GitHub Issue Link

Issue #10475

Testing Plan

Unit Tests Added

  1. Test warning appears when modifying elements inside a fragment callback
  2. Verify no warning is raised for callbacks outside of fragments
  3. Confirm no warning when callback does not interact with elements

Contribution License Agreement

By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

…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-io
Copy link
Contributor

snyk-io bot commented Mar 27, 2025

🎉 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 sfc-gh-bnisco self-assigned this Apr 2, 2025
Copy link
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco left a 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!

@Zane-dev16
Copy link
Contributor Author

Thanks for the feedback @sfc-gh-bnisco. I've updated the warning message: 3a3847a

Co-authored-by: Debbie Matthews <[email protected]>
Copy link
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco left a 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

@Zane-dev16
Copy link
Contributor Author

Thanks for the feedback @sfc-gh-bnisco. I've updated the test for the new warning.

)


def _maybe_print_fragment_callback_warning(delta_type: str) -> None:
Copy link
Collaborator

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.

Copy link
Contributor Author

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

@lukasmasuch lukasmasuch added impact:users PR changes affect end users change:feature PR contains new feature or enhancement implementation labels May 1, 2025
@sfc-gh-bnisco sfc-gh-bnisco added the security-assessment-completed Security assessment has been completed for PR label May 6, 2025
Copy link
Collaborator

@sfc-gh-bnisco sfc-gh-bnisco left a 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 👍

@sfc-gh-bnisco sfc-gh-bnisco merged commit 5427eb1 into streamlit:develop May 6, 2025
36 of 37 checks passed
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 security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants