ipywidgets
ipywidgets copied to clipboard
Question: mixing `display` and `_repr_html_`
I have a question regarding custom IPyWidgets.
We have an already made library that renders on notebooks using the _repr_html_ method understood by IPython. Those elements are custom HTML/JS/CSS components. In other contexts, they can be used as interactive objects and we used to integrate then with Jupyter Notebook, using the old deprecated API.
We know would like to integrate with IPyWidgets, version 8. I have different strategies, one of them being described as follows:
Given an existing Component class that implements _repr_html_, given a custom IPyWidget (made with the cookie cutter for example) class Widget, create a composite:
class InteractiveComponent(Component):
def __init__(self):
self.__widget = Widget()
def _repr_html_(self):
display(self.__widget)
return Component._repr_html_(self)
My biggest question is: do you believe this approach is safe (disregarding any technical detail/difficulty I might have making the two rendered objects synchronized), in the sense:
- Can there be byte mixing between the code the kernel sends to the frontend when doing
displayand the string returned by_repr_html_? - Is it always true the two rendered objects will belong under the same
windowobject?
I ask because I already had issues with point (1.) and displaying a very large object. Here it is aimed to be a very small IPyWidget, but a very large HTML representation. (Issue unable to reproduce; it looked like a byte stream mixing between two streams, but couldn't reproduce and had the flavor of typical concurrency bug, so let's not mind about the actual issue I faced but just about whether or not doing both is safe)
I could imagine situations where (2.) fails to be true ; for example, _repr_html_ objects are rendered in an iframe, but display makes the widget displayed in the parent window. Is it possible?
Sorry in advance for the questions being a bit vague. I'm at the stage of trying to understand the new API while finding solutions so even for me it's not clear where would the issues be.
Note: I also investigated the idea of creating a custom widget that reinjects the code of _repr_html_ in this.el and it was working fine too. This is also a possibility that could work in my case, but somehow it requires to create more wrappers bind on the IPyWidgets third-party, compared to the approach "by composition" described above.
I also observe there is an Output widget that can render rich content from IPython, so maybe I could actually extend Box from IPyWidgets and make it render the two in the same cell. That might be more idiomatic and less difficult to actually maintain/wrap