Skip to content

Commit e426a19

Browse files
denphijonmmease
authored andcommitted
Add support to plotly_deselect event (#1542)
* Add trace.on_deselect() method to register callback to be executed on deselection (double click)
1 parent 6ced2d9 commit e426a19

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

Diff for: js/src/Figure.js

+15
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,10 @@ var FigureView = widgets.DOMWidgetView.extend({
750750
function (update) {
751751
that.handle_plotly_selected(update)
752752
});
753+
that.el.on("plotly_deselect",
754+
function (update) {
755+
that.handle_plotly_deselect(update)
756+
});
753757
that.el.on("plotly_doubleclick",
754758
function (update) {
755759
that.handle_plotly_doubleclick(update)
@@ -1080,6 +1084,17 @@ var FigureView = widgets.DOMWidgetView.extend({
10801084
handle_plotly_selected: function (data) {
10811085
this._send_points_callback_message(data, "plotly_selected");
10821086
},
1087+
1088+
/**
1089+
* Handle plotly_deselect events emitted by the Plotly.js library
1090+
* @param data
1091+
*/
1092+
handle_plotly_deselect: function (data) {
1093+
data = {
1094+
points : []
1095+
}
1096+
this._send_points_callback_message(data, "plotly_deselect");
1097+
},
10831098

10841099
/**
10851100
* Build and send a points callback message to the Python side

Diff for: plotly/basedatatypes.py

+71
Original file line numberDiff line numberDiff line change
@@ -4120,6 +4120,9 @@ def __init__(self, plotly_name, **kwargs):
41204120
# ### Callbacks to be called on selection ###
41214121
self._select_callbacks = []
41224122

4123+
# ### Callbacks to be called on deselect ###
4124+
self._deselect_callbacks = []
4125+
41234126
# ### Trace index in figure ###
41244127
self._trace_ind = None
41254128

@@ -4387,6 +4390,74 @@ def _dispatch_on_selection(self,
43874390
for callback in self._select_callbacks:
43884391
callback(self, points, selector)
43894392

4393+
# deselect
4394+
# --------
4395+
def on_deselect(
4396+
self,
4397+
callback,
4398+
append=False):
4399+
"""
4400+
Register function to be called when the user deselects points
4401+
in this trace using doubleclick.
4402+
4403+
Note: Callbacks will only be triggered when the trace belongs to a
4404+
instance of plotly.graph_objs.FigureWidget and it is displayed in an
4405+
ipywidget context. Callbacks will not be triggered on figures
4406+
that are displayed using plot/iplot.
4407+
4408+
Parameters
4409+
----------
4410+
callback
4411+
Callable function that accepts 3 arguments
4412+
4413+
- this trace
4414+
- plotly.callbacks.Points object
4415+
4416+
append : bool
4417+
If False (the default), this callback replaces any previously
4418+
defined on_deselect callbacks for this trace. If True,
4419+
this callback is appended to the list of any previously defined
4420+
callbacks.
4421+
4422+
Returns
4423+
-------
4424+
None
4425+
4426+
Examples
4427+
--------
4428+
>>> from plotly.callbacks import Points
4429+
>>> points = Points()
4430+
4431+
>>> def deselect_fn(trace, points):
4432+
... inds = points.point_inds
4433+
... # Do something
4434+
4435+
>>> trace.on_deselect(deselect_fn)
4436+
4437+
Note: The creation of the `points` object is optional,
4438+
it's simply a convenience to help the text editor perform completion
4439+
on the `points` arguments inside `selection_fn`
4440+
"""
4441+
if not append:
4442+
del self._deselect_callbacks[:]
4443+
4444+
if callback:
4445+
self._deselect_callbacks.append(callback)
4446+
4447+
def _dispatch_on_deselect(self, points):
4448+
"""
4449+
Dispatch points info to deselection callbacks
4450+
"""
4451+
if 'selectedpoints' in self:
4452+
# Update the selectedpoints property, which will notify all views
4453+
# of the selection change. This is a special case because no
4454+
# restyle event is emitted by plotly.js on selection events
4455+
# even though these events update the selectedpoints property.
4456+
self.selectedpoints = None
4457+
4458+
for callback in self._deselect_callbacks:
4459+
callback(self, points)
4460+
43904461

43914462
class BaseFrameHierarchyType(BasePlotlyType):
43924463
"""

Diff for: plotly/basewidget.py

+2
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ def _handler_js2py_pointsCallback(self, change):
737737
trace._dispatch_on_unhover(points, state)
738738
elif event_type == 'plotly_selected':
739739
trace._dispatch_on_selection(points, selector)
740+
elif event_type == 'plotly_deselect':
741+
trace._dispatch_on_deselect(points)
740742

741743
self._js2py_pointsCallback = None
742744

Diff for: plotlywidget/static/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -13409,6 +13409,10 @@ var FigureView = widgets.DOMWidgetView.extend({
1340913409
function (update) {
1341013410
that.handle_plotly_selected(update)
1341113411
});
13412+
that.el.on("plotly_deselect",
13413+
function (update) {
13414+
that.handle_plotly_deselect(update)
13415+
});
1341213416
that.el.on("plotly_doubleclick",
1341313417
function (update) {
1341413418
that.handle_plotly_doubleclick(update)
@@ -13739,6 +13743,17 @@ var FigureView = widgets.DOMWidgetView.extend({
1373913743
handle_plotly_selected: function (data) {
1374013744
this._send_points_callback_message(data, "plotly_selected");
1374113745
},
13746+
13747+
/**
13748+
* Handle plotly_deselect events emitted by the Plotly.js library
13749+
* @param data
13750+
*/
13751+
handle_plotly_deselect: function (data) {
13752+
data = {
13753+
points : []
13754+
}
13755+
this._send_points_callback_message(data, "plotly_deselect");
13756+
},
1374213757

1374313758
/**
1374413759
* Build and send a points callback message to the Python side

0 commit comments

Comments
 (0)