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

Update traitlet values before initial render of widget #4956

Merged
merged 6 commits into from
Jan 9, 2025

Conversation

marthacryan
Copy link
Collaborator

Fixes #4933.

Source of the problem

The issue was that there were updates happening (for example, add_layout_image) before the initial call to _repr_mimebundle_, which were triggering the traitlet events like _py2js_relayout or _py2js_addTraces. These events were designed with the idea of a queue that would exist through traitlets, but anywidget (and ipywidgets in general, although for some reason this was working before the anywidget changes) expects the traitlet to be more of a single source of truth for a value, not something that has ordered changes. So by the time that the widget is initialized on the frontend, it loses all of the events that were triggered before the call to fig.show().

Solution

This updates the _repr_mimebundle_ method to set the value of the layout and data traitlets before the initial render, so that the changes that were lost in the events queue are still included.

This also changes the name of the traitlets for layout and data to _widget_layout and _widget_data because changes were being made to those fields along the way that prevented traitlets from registering a change when they're set in _repr_mimebundle.

More info on changing the traitlet names Some parts of the plotly code change specific properties of the layout traitlets rather than reassigning the entire traitlet. This meant that when we _did_ reassign the value of the traitlets, ipywidgets went to check if there were any changes and ended up deciding that there weren't because it still pointed to the object that had specific fields changed. This seems like a bug but keeping these traitlets separate from the internals of plotly and always reassigning the full value seems like a more straightforward solution.
Context on other approaches considered We considered removing the events system and simplifying the code. See #4945 for what I tried (which did work). However, this would entail a sacrifice in performance because the entire layout or data object would be sent over for any change to the traitlets. This would be much slower than sending only the deltas as we are now.

Copy link
Contributor

@emilykl emilykl left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@marthacryan marthacryan merged commit 1fc6bdd into master Jan 9, 2025
5 checks passed
@marthacryan marthacryan deleted the update-traitlets branch January 9, 2025 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix fixes something broken P1 needed for current cycle
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Erratic behavior when working with multiple FigureWidget instances (after adoption of anywidget?)
3 participants