Skip to content

bqplot ImageWidget: built-in _mouse_click references uninitialized click_center/is_marking, so any click raises AttributeError and blocks other on_msg callbacks #206

Description

@mwcraig

Description

In astrowidgets 0.5.0, any mouse click on the bqplot ImageWidget with an image loaded raises AttributeError, because the built-in _mouse_click handler references two attributes that are never initialized.

ImageWidget._init_mouse_callbacks registers on_mouse_message, which dispatches click events to _mouse_click (bqplot.py lines 441–455 at the 0.5.0 tag):

def _mouse_click(self, event_data):
    if self._data is None:
        # Nothing to display, so exit
        return

    xc = event_data['domain']['x']
    yc = event_data['domain']['y']

    if self.click_center:
        self.center_on((xc, yc))

    if self.is_marking:
        print('marky marking')
        # Just hand off to the method that actually does the work
        self._add_new_single_marker(xc, yc)

Neither click_center nor is_marking is set anywhere — not in ImageWidget.__init__ and not in ImageViewerLogic (they were part of the interactive-feature set dropped from the display API; see #201). So the first if raises:

AttributeError: 'ImageWidget' object has no attribute 'click_center'

Reproducer

import numpy as np
from astrowidgets.bqplot import ImageWidget

iw = ImageWidget()
iw.load_image(np.zeros((10, 10)))
iw._mouse_click({"domain": {"x": 3, "y": 3}})   # AttributeError

In a live notebook the same thing happens on any real mouse click once an image is loaded.

Knock-on effect: downstream click handlers are blocked

This is worse than a traceback in the log: ipywidgets runs on_msg callbacks in registration order without exception isolation, and the built-in callback is registered in __init__, i.e. always first. Its AttributeError therefore prevents any user-registered on_msg click callback from running at all — clicks are effectively dead for downstream code.

Workaround

Setting the attributes as plain instance attributes after construction makes _mouse_click a no-op and unblocks later callbacks:

iw.click_center = False
iw.is_marking = False

(stellarphot does this while migrating to 0.5.0: feder-observatory/stellarphot#584.)

Suggested fix

Initialize both to False in ImageWidget.__init__ (or remove the dead branches from _mouse_click until the interactive features from #201 are rebuilt).

Happy to send a PR either way.


This issue was written by Claude (AI assistant) at the direction of @mwcraig.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions