Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Mouse hover over a FigureWidget to trigger the update of another figure is very unstable

Here http://nbviewer.jupyter.org/gist/empet/65d6bb07352993e1f8b5cddce815616a
is a reproducible example.

Sometimes it works, but most often when it retrieves the scatter
point index it display the error message
list index out of range:

 . . .
 <ipython-input-20-ca1abaf933a6> in
trigger_update(trace,points, state)
        4
        5     def trigger_update(trace, points, state):
---->   6         ind = points.point_inds[0]#get the index of the scatter point  that is hovered on
        7
        8         with fwd.batch_update():#update data and layout in the bar chart

IndexError: list index out of range

What is odd is that it continues to update the bar chart on hover, even after displaying this error.

This behaviour manifests in any other pair consisting in a scatter plot (not only scatermapbox) and other FigureWidget to be updated.

Hi @empet, This is a really cool notebook dashboard!

It looks like the trouble you’re running into is that you have two traces (county boundary lines and country center points) and the on_hover interaction function is called whenever a hover interaction occurs on either trace. So when a hover is registered on a county boundary, the on_hover function for the county centers trace is being called with an empty array of points.

Here are two options for getting around this:

  1. Add a check to your trigger_update to do nothing if the point_inds are empty.
def trigger_update(trace, points, state):
        if not points.point_inds:
            return
        
        ind = points.point_inds[0]#get the index of the scatter point  that is hovered on
        
        with fwd.batch_update():#update data and layout in the bar chart
            fwd.data[0].y = votes_Clinton_Trump[ind]
            fwd.layout.title = county_name[ind]
  1. Set the hoverinfo property of the country lines trace to skip instead of none (as you have it now). When the hoverinfo is skip, hover events will not be fired.

The reason these empty hover events are fired at the moment is because the on_hover and on_selection logic is nearly identical. For on_selection I wanted empty selection events to fire so that it was possible to respond to the situation where someone selects a region that contains no points. But maybe that should be a special case for on_selection, so that on_hover, on_unhover, and on_click don’t fire with empty point_inds

What do you think?
-Jon

1 Like

@jmmease Thank you very much for your suggestions!!
The first one works but the second leads to the same error.