anyplotlib.Widget#

class anyplotlib.Widget(wtype, push_fn, **kwargs)[source]#

Bases: _EventMixin

Base class for all overlay widgets.

Provides attribute-based state access, callbacks for interaction events, and automatic synchronization with the JavaScript renderer.

Parameters:
  • wtype (str) – Widget type (e.g., ‘rectangle’, ‘circle’, ‘crosshair’).

  • push_fn (Callable) – Zero-arg callback to send position updates to the JavaScript renderer.

  • **kwargs (dict) – Initial widget state (position, size, color, etc.).

callbacks#

Event callback registry. Register handlers via widget.add_event_handler(fn, "pointer_move") or as a decorator: @widget.add_event_handler("pointer_move").

Common event types:

  • "pointer_move" — fires on every drag frame

  • "pointer_up" — fires once when drag settles

  • "pointer_down" — fires on click/press event

Type:

CallbackRegistry

set(_push=True, **kwargs)[source]#

Update properties and send targeted update to JavaScript.

Parameters:
  • _push (bool, optional) – Whether to push update to renderer. Default True. Set to False internally to avoid echo loops.

  • **kwargs (dict) – Properties to update (e.g., x=100, y=50, radius=20).

Return type:

None

Notes

Updates are sent as targeted widget updates, not full panel re-renders. This is more efficient for frequent updates during dragging.

get(key, default=None)[source]#

Get a widget property by name.

Parameters:
  • key (str) – Property name.

  • default (optional) – Default value if property not found.

Returns:

The property value.

Return type:

object

to_dict()[source]#

Return a dict copy of the widget state.

Returns:

All widget properties including id and type.

Return type:

dict

property visible: bool#

True if the widget is rendered; False if hidden.

show()[source]#

Show the widget. Does not fire pointer_move callbacks.

Return type:

None

hide()[source]#

Hide the widget without removing it or its callbacks.

Call show() to make it visible again. Does not fire pointer_move callbacks.

Return type:

None

property id: str#

Return the widget’s unique identifier.