ipywidgets supports async widgets via threading. Rather than waiting for data, turning it into HTML, and directly displaying the HTML, I could return a loading HTML widget and use threading to update the widget contents once data is retrieved from the server and formatted. This would make the experience more similar to the code editor by not blocking the entire kernel.
The main downside would be adding a dependency on ipywidgets, but if most users are using this alongside geemap then that's not a big issue. Other considerations would be:
Can I throw all the JS and CSS into the HTML widget like I currently do, or do I need to handle that differently?
- Would reprs inside an
HTML widget display correctly when rendered statically like they currently do? That's not a dealbreaker, but it would be nice.
- Are there performance drawbacks in rendering time?
- What will the
ipywidgets compatibility be? I've had issues in the past with ipywidgets>7, especially in Jupyter Lab, so ideally this would work in version 7 or 8.
- Calling
_repr_html_ should return an HTML string, not a widget, so I think I would need to use _ipython_display_ or _repr_mimebundle_ instead and return the corresponding method from the associated widget.
Here's a rough implementation idea:
def _ipython_display_(obj: ee.Element):
"""Display an Earth Engine object in an async HTML widget"""
html = ipywidgets.HTML("<span>Loading...</span>")
threading.Thread().start(build_repr, args=(obj, html))
return html._ipython_display_()
def _build_repr(obj: ee.Element, html: ipywidgets.HTML) -> None:
"""Format an HTML repr string and add it to an HTML widget"""
info = obj.getInfo()
rep = _format_repr(info)
html.value = rep
ipywidgetssupports async widgets via threading. Rather than waiting for data, turning it into HTML, and directly displaying the HTML, I could return a loadingHTMLwidget and use threading to update the widget contents once data is retrieved from the server and formatted. This would make the experience more similar to the code editor by not blocking the entire kernel.The main downside would be adding a dependency on
ipywidgets, but if most users are using this alongsidegeemapthen that's not a big issue. Other considerations would be:Can I throw all the JS and CSS into theHTMLwidget like I currently do, or do I need to handle that differently?HTMLwidgets do not run scripts.HTMLwidget display correctly when rendered statically like they currently do? That's not a dealbreaker, but it would be nice.ipywidgetscompatibility be? I've had issues in the past withipywidgets>7, especially in Jupyter Lab, so ideally this would work in version 7 or 8._repr_html_should return an HTML string, not a widget, so I think I would need to use_ipython_display_or_repr_mimebundle_instead and return the corresponding method from the associated widget.Here's a rough implementation idea: