Callbacks#

callbacks.py#

Lightweight two-class event system used by every plot object and widget.

CallbackRegistry

Per-object store of named callbacks. Every plot object and widget exposes on_changed, on_release, on_click, on_key, on_line_hover, and on_line_click decorator methods that connect handlers through this registry.

Event

Immutable data-carrier passed to every callback. All keys in the raw JS payload are accessible as attributes (event.zoom, event.cx, etc.) in addition to the typed event_type, source, and data fields.

Example

fig, ax = apl.subplots(1, 1)
plot = ax.imshow(data)

@plot.on_release
def on_settle(event):
    print(f"zoom={event.zoom:.2f}  center=({event.center_x:.3f}, {event.center_y:.3f})")

Classes

CallbackRegistry

Per-object registry for on_click / on_changed / on_release / on_key / on_line_hover / on_line_click callbacks.

Event

A single interactive event.

Full Reference

class anyplotlib.callbacks.CallbackRegistry[source]

Bases: object

Per-object registry for on_click / on_changed / on_release / on_key / on_line_hover / on_line_click callbacks.

connect(event_type, fn)[source]

Register fn for event_type. Returns integer CID.

Parameters:
Return type:

int

disconnect(cid)[source]

Remove handler for cid. Silent if not found.

Parameters:

cid (int)

Return type:

None

fire(event)[source]

Dispatch event to all handlers matching event.event_type.

Return type:

None

class anyplotlib.callbacks.Event(event_type, source, data=<factory>)[source]

Bases: object

A single interactive event.

Event_type:

one of on_click / on_changed / on_release / on_key / on_line_hover / on_line_click

Source:

the originating Python object (Widget, Plot, or None)

Data:

full state dict; all keys also accessible as event.x

Parameters:

For on_line_hover and on_line_click events the data dict contains:

  • line_idNone for the primary line, or the 8-char ID string assigned by Plot1D.add_line().

  • x – data-space x coordinate of the nearest point on the line.

  • y – data-space y coordinate of the nearest point on the line.

event_type: str
source: Any
data: dict