Skip to content
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

Reuse style element for theme injection into custom components #7914

Merged
merged 3 commits into from Jan 10, 2024
Merged

Reuse style element for theme injection into custom components #7914

merged 3 commits into from Jan 10, 2024

Conversation

Tom-Julux
Copy link
Contributor

Describe your changes

In the current implementation, whenever a custom component receives a render event, the theme (which may have changed) is injected as a new style element, and this element is appended to the head of the iframe document.

This pull request introduces a modification to this behaviour. Instead of creating a new style element for each render event, a single style element is now created and reused for subsequent style updates.

The updated approach offers several advantages, primarily preventing the linear growth of the iframe document size with the increasing number of render events. This optimization enhances the efficiency and performance of the custom components, in particular for long lived custom components that receive a large number of or frequent render events.

Testing Plan

Since the injected css is constant in terms of its attributes and selectors, the current behaviour of appending new style elements is equal to the overwriting introduced in this PR. Thus no testing is required


Contribution License Agreement

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

Copy link
Collaborator

@vdonato vdonato 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 the contribution @Tom-Julux!

While this change doesn't change any functionality as seen by the user, I think we'll still want to add a test that verifies that the style tag only gets appended once for completeness.

This can likely be done most easily by modifying this test to send a second streamlit:render event at the current end of the test, then verifying that no additional style tag was appended.

Once that test is added, this change should be good to go.

Copy link
Collaborator

@vdonato vdonato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vdonato vdonato merged commit 020d904 into streamlit:develop Jan 10, 2024
43 checks passed
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
…mlit#7914)

* Reuse style element for theme injection into custom components

* Test for reuse of style element for theme injection into custom components

* Run precommit

---------

Co-authored-by: Vincent Donato <vincent@streamlit.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants