Interactive Widgets#
widgets.py — Interactive overlay widget classes.
Each widget has a .callbacks (CallbackRegistry). Register handlers via:
@rect.on_changed # every drag frame
def live(event): ...
@rect.on_release # once on mouseup
def done(event): ...
@rect.on_click
def clicked(event): ...
rect.x = 40 # moves widget, sends targeted update to JS
rect.x # always reflects current JS position
Base Class
Base class for all overlay widgets. |
Concrete Widgets
Draggable rectangle overlay widget for 2-D plots. |
|
Draggable circle overlay widget for 2-D plots. |
|
Draggable annular (ring) overlay widget for 2-D plots. |
|
Draggable crosshair overlay widget for 2-D plots. |
|
Draggable polygon overlay widget for 2-D plots. |
|
Text label overlay widget for 2-D plots. |
|
Draggable vertical line overlay widget for 1-D plots. |
|
Draggable horizontal line overlay widget for bar charts. |
|
Draggable range selection widget. |
Full Reference
- class anyplotlib.widgets.Widget(wtype, push_fn, **kwargs)[source]
Bases:
objectBase class for all overlay widgets.
Provides attribute-based state access, callbacks for interaction events, and automatic synchronization with the JavaScript renderer.
- Parameters:
- callbacks
Event callback registry. Register handlers via: -
@widget.on_changed— fires on every drag frame -@widget.on_release— fires once when drag settles -@widget.on_click— fires on click event- Type:
- set(_push=True, **kwargs)[source]
Update properties and send targeted update to JavaScript.
- Parameters:
- 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.
- to_dict()[source]
Return a dict copy of the widget state.
- Returns:
All widget properties including id and type.
- Return type:
- on_changed(fn)[source]
Decorator: register fn to fire on every drag frame.
Use this for high-frequency updates (keep handler fast).
- Parameters:
fn (Callable) – Handler function receiving an Event.
- Returns:
The decorated function.
- Return type:
Callable
- on_release(fn)[source]
Decorator: register fn to fire once when drag settles.
Use this for expensive operations triggered after user stops dragging.
- Parameters:
fn (Callable) – Handler function receiving an Event.
- Returns:
The decorated function.
- Return type:
Callable
- on_click(fn)[source]
Decorator: register fn to fire on widget click.
- Parameters:
fn (Callable) – Handler function receiving an Event.
- Returns:
The decorated function.
- Return type:
Callable
- disconnect(cid)[source]
Remove the callback registered under cid.
- Parameters:
cid (int or Callable) – Either the integer CID returned by
callbacks.connect(), or the decorated function itself (carries a._cidattribute).- Return type:
None
- property visible: bool
Trueif the widget is rendered;Falseif hidden.
- show()[source]
Show the widget. Does not fire
on_changedcallbacks.- Return type:
None
- hide()[source]
Hide the widget without removing it or its callbacks.
Call
show()to make it visible again. Does not fireon_changedcallbacks.- Return type:
None
- property id: str
Return the widget’s unique identifier.
- class anyplotlib.widgets.RectangleWidget(push_fn, *, x, y, w, h, color='#00e5ff')[source]
Bases:
WidgetDraggable rectangle overlay widget for 2-D plots.
- Parameters:
push_fn (Callable) – Update callback.
x (float) – Top-left corner position in pixel/data coordinates.
y (float) – Top-left corner position in pixel/data coordinates.
w (float) – Width and height in pixel/data coordinates.
h (float) – Width and height in pixel/data coordinates.
color (str, optional) – CSS colour for the rectangle outline. Default
"#00e5ff".
- class anyplotlib.widgets.CircleWidget(push_fn, *, cx, cy, r, color='#00e5ff')[source]
Bases:
WidgetDraggable circle overlay widget for 2-D plots.
- class anyplotlib.widgets.AnnularWidget(push_fn, *, cx, cy, r_outer, r_inner, color='#00e5ff')[source]
Bases:
WidgetDraggable annular (ring) overlay widget for 2-D plots.
- Parameters:
push_fn (Callable) – Update callback.
cx (float) – Center position in pixel/data coordinates.
cy (float) – Center position in pixel/data coordinates.
r_outer (float) – Outer and inner radii in pixel/data coordinates. Inner radius must be less than outer radius.
r_inner (float) – Outer and inner radii in pixel/data coordinates. Inner radius must be less than outer radius.
color (str, optional) – CSS colour for the ring outline. Default
"#00e5ff".
- Raises:
ValueError – If r_inner >= r_outer.
- class anyplotlib.widgets.CrosshairWidget(push_fn, *, cx, cy, color='#00e5ff')[source]
Bases:
WidgetDraggable crosshair overlay widget for 2-D plots.
- class anyplotlib.widgets.PolygonWidget(push_fn, *, vertices, color='#00e5ff')[source]
Bases:
WidgetDraggable polygon overlay widget for 2-D plots.
- Parameters:
- Raises:
ValueError – If fewer than 3 vertices provided.
- class anyplotlib.widgets.LabelWidget(push_fn, *, x, y, text='Label', fontsize=14, color='#00e5ff')[source]
Bases:
WidgetText label overlay widget for 2-D plots.
- Parameters:
push_fn (Callable) – Update callback.
x (float) – Label position in pixel/data coordinates.
y (float) – Label position in pixel/data coordinates.
text (str, optional) – Label text. Default
"Label".fontsize (int, optional) – Font size in points. Default 14.
color (str, optional) – CSS colour for the text. Default
"#00e5ff".
- class anyplotlib.widgets.VLineWidget(push_fn, *, x, color='#00e5ff')[source]
Bases:
WidgetDraggable vertical line overlay widget for 1-D plots.
Allows interactive selection of a single x-axis value. The line can be dragged left/right to change the selected position.
- class anyplotlib.widgets.HLineWidget(push_fn, *, y, color='#00e5ff')[source]
Bases:
WidgetDraggable horizontal line overlay widget for bar charts.
Allows interactive selection of a single y-axis value. The line can be dragged up/down to change the selected value.
- class anyplotlib.widgets.RangeWidget(push_fn, *, x0, x1, color='#00e5ff', style='band', y=0.0)[source]
Bases:
WidgetDraggable range selection widget.
Two display styles are available:
style='band'(default)Two connected vertical lines with a translucent fill band. Either line can be dragged independently; the whole band can be dragged by clicking inside it.
style='fwhm'Two circular handles joined by a dashed horizontal line drawn at height y (the half-maximum level). Only the x-positions of the handles are draggable. Use this to show/edit a FWHM interval on a peak.
- Parameters:
push_fn (Callable) – Update callback.
x0 (float) – Initial left and right positions in data coordinates.
x1 (float) – Initial left and right positions in data coordinates.
color (str, optional) – CSS colour. Default
"#00e5ff".style ({'band', 'fwhm'}, optional) – Visual style. Default
"band".y (float, optional) – Y-position (data coordinates) for the connecting line when
style='fwhm'. Ignored forstyle='band'. Default0.0.