From e07b25c8194c2f07ef3184fb188b5c26e9f5bc95 Mon Sep 17 00:00:00 2001 From: Chris P Date: Wed, 21 Jan 2015 13:54:55 -0500 Subject: [PATCH] de-nest messages for callbacks --- plotly/widgets/graph_widget.py | 53 ++++++++++++------------------- specs/GraphWidgetSpec.md | 57 +++++++++++----------------------- 2 files changed, 37 insertions(+), 73 deletions(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index b4063a10cd7..0a06755088f 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -82,7 +82,13 @@ def _handle_msg(self, message): self._message = _message if content.get('event', '') in ['click', 'hover', 'zoom']: - self._event_handlers[content['event']](self, content) + # De-nest the message + if content['event'] == 'click' or content['event'] == 'hover': + message = content['message']['points'] + elif content['event'] == 'zoom': + message = content['message']['ranges'] + + self._event_handlers[content['event']](self, message) def _handle_registration(self, event_type, callback, remove): self._event_handlers[event_type].register_callback(callback, @@ -118,21 +124,14 @@ def on_click(self, callback, remove=False): which point(s) were clicked on. click_obj example: - { - 'event': 'hover', - 'graphId': '2e66d4da-0523-4b6b-a3d7-f3b8ced416b1', - 'message': { - 'points': [ - { - 'curveNumber': 1, - 'pointNumber': 2, - 'x': 4, - 'y': 14 - } - ], - 'type': 'hover' + [ + { + 'curveNumber': 1, + 'pointNumber': 2, + 'x': 4, + 'y': 14 } - } + ] remove (bool, optional): If False, attach the callback. If True, remove the callback. Defaults to False. @@ -173,21 +172,14 @@ def on_hover(self, callback, remove=False): which point(s) was hovered over. hover_obj example: - { - 'event': 'hover', - 'graphId': '2e66d4da-0523-4b6b-a3d7-f3b8ced416b1', - 'message': { - 'points': [ - { + [ + { 'curveNumber': 1, 'pointNumber': 2, 'x': 4, 'y': 14 - } - ], - 'type': 'hover' } - } + ] remove (bool, optional): If False, attach the callback. If True, remove the callback. Defaults to False. @@ -230,15 +222,8 @@ def on_zoom(self, callback, remove=False): zoom_obj example: { - 'event': 'zoom', - 'graphId': '2e66d4da-0523-4b6b-a3d7-f3b8ced416b1', - 'message': { - 'ranges': { - 'x': [1.8399058038561549, 2.16443359662], - 'y': [4.640902872777017, 7.855677154582] - }, - 'type': 'zoom' - } + 'x': [1.8399058038561549, 2.16443359662], + 'y': [4.640902872777017, 7.855677154582] } remove (bool, optional): If False, attach the callback. diff --git a/specs/GraphWidgetSpec.md b/specs/GraphWidgetSpec.md index 790bffd206f..8f261994c15 100644 --- a/specs/GraphWidgetSpec.md +++ b/specs/GraphWidgetSpec.md @@ -22,21 +22,14 @@ g.on_click(callback, remove=False) which point(s) were clicked on. click_obj example: - { - 'event': 'hover', - 'graphId': '2e66d4da-0523-4b6b-a3d7-f3b8ced416b1', - 'message': { - 'points': [ - { - 'curveNumber': 1, - 'pointNumber': 2, - 'x': 4, - 'y': 14 - } - ], - 'type': 'hover' - } - } + [ + { + 'curveNumber': 1, + 'pointNumber': 2, + 'x': 4, + 'y': 14 + } + ] remove (bool, optional): If False, attach the callback. If True, remove the callback. Defaults to False. @@ -79,21 +72,14 @@ g.on_hover(callback, remove=False) points belong to. hover_obj example: - { - 'event': 'hover', - 'graphId': '2e66d4da-0523-4b6b-a3d7-f3b8ced416b1', - 'message': { - 'points': [ - { - 'curveNumber': 1, - 'pointNumber': 2, - 'x': 4, - 'y': 14 - } - ], - 'type': 'hover' - } - } + [ + { + 'curveNumber': 1, + 'pointNumber': 2, + 'x': 4, + 'y': 14 + } + ] remove (bool, optional): If False, attach the callback. If True, remove the callback. Defaults to False. @@ -136,15 +122,8 @@ g.on_zoom(callback, remove=False) zoom_obj example: { - 'event': 'zoom', - 'graphId': '2e66d4da-0523-4b6b-a3d7-f3b8ced416b1', - 'message': { - 'ranges': { - 'x': [1.8399058038561549, 2.1644335966246384], - 'y': [4.640902872777017, 7.8556771545827635] - }, - 'type': 'zoom' - } + 'x': [1.8399058038561549, 2.1644335966246384], + 'y': [4.640902872777017, 7.8556771545827635] } remove (bool, optional): If False, attach the callback.