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

Refactor image embedding logic #406

Merged
merged 1 commit into from
Dec 15, 2021

Conversation

martinRenou
Copy link
Member

@martinRenou martinRenou commented Dec 14, 2021

Refactor the image embedding logic.

This fixes an issue in the case of nbconvert --to html that was preventing the plot image to render.

@github-actions
Copy link
Contributor

Binder 👈 Launch a binder notebook on branch martinRenou/ipympl/image_embedding_refactor

@martinRenou martinRenou force-pushed the image_embedding_refactor branch 5 times, most recently from d149e3e to 7c997b4 Compare December 14, 2021 12:11
@martinRenou martinRenou force-pushed the image_embedding_refactor branch from 7c997b4 to 9aad6fc Compare December 14, 2021 12:27
@martinRenou martinRenou marked this pull request as ready for review December 14, 2021 12:29
</div>
""".format(
self._figure_label, self._data_url, width
)

# Update the widget model properly for HTML embedding
self._size = (width, height)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is one important change here.

We need to make the plot size part of the widget model in the case of nbconvert --to html otherwise there is no registered size and the canvas would be 0 sized.

@@ -275,17 +287,49 @@ export class MPLCanvasModel extends DOMWidgetModel {
this.image = new Image();

this.image.onload = () => {
// In case of an embedded widget, the initial size is not correct
// and we are not receiving any resize event from the server
if (this.disabled) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the second important change in this PR.

Here, we need a special case for when the widget is disabled (no counterpart in the kernel, or no kernel at all), where we display the image that is part of the model.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Am I right that this will come up in many situations, not just nbconvert, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. You can trigger this situation by saving the widget model in the page, shutdown the kernel, then refresh the page.

@@ -184,8 +185,7 @@ class Canvas(DOMWidget, FigureCanvasWebAggCore):
# This will still be used by ipywidgets in the case of embedding.
_data_url = Any(None).tag(sync=True)

_width = CInt().tag(sync=True)
_height = CInt().tag(sync=True)
_size = Tuple([0, 0]).tag(sync=True)
Copy link
Member Author

Choose a reason for hiding this comment

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

Replacing the two private traits _width and _height into _size so that the resize happens in one comm message

Copy link
Collaborator

Choose a reason for hiding this comment

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

I like it

@ianhi
Copy link
Collaborator

ianhi commented Dec 14, 2021

Haven't tried it out, but this all looks pretty sensible to me.

@martinRenou
Copy link
Member Author

Thanks for looking into it!

I will merge and make a patch release with this soonish.

@martinRenou martinRenou merged commit 480a236 into matplotlib:master Dec 15, 2021
@martinRenou martinRenou deleted the image_embedding_refactor branch December 15, 2021 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants