Skip to content

Try using async widgets #21

@aazuspan

Description

@aazuspan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions