diff --git a/panel/pane/plotly.py b/panel/pane/plotly.py index 82121b6e0d..c9a9a41ca4 100644 --- a/panel/pane/plotly.py +++ b/panel/pane/plotly.py @@ -156,13 +156,11 @@ def _get_sources_for_trace(json, data, parent_path=''): def _update_figure(self): import plotly.graph_objs as go - if (self.object is None or type(self.object) is not go.Figure or + if (self.object is None or type(self.object) not in (go.Figure, go.FigureWidget) or self.object is self._figure or not self.link_figure): return # Monkey patch the message stubs used by FigureWidget. - # We only patch `Figure` objects (not subclasses like FigureWidget) so - # we don't interfere with subclasses that override these methods. fig = self.object fig._send_addTraces_msg = lambda *_, **__: self._update_from_figure('add') fig._send_deleteTraces_msg = lambda *_, **__: self._update_from_figure('delete') @@ -181,20 +179,26 @@ def _send_restyle_msg(self, restyle_data, trace_indexes=None, source_view_id=Non @param.depends('restyle_data', watch=True) def _update_figure_style(self): - if self._figure is None or self.restyle_data is None: + if self.restyle_data is None: return - self._figure.plotly_restyle(*self.restyle_data) + if hasattr(self.object, '_handler_js2py_layoutDelta'): + self.object.plotly_update(restyle_data=self.restyle_data) + elif self._figure is not None: + self._figure.plotly_restyle(self.restyle_data) @param.depends('relayout_data', watch=True) def _update_figure_layout(self): - if self._figure is None or self.relayout_data is None: + if self.relayout_data is None: return relayout_data = self._clean_relayout_data(self.relayout_data) - # The _compound_array_props are sometimes not correctly reset - # which means that they are desynchronized with _props causing - # incorrect lookups and potential errors when updating a property - self._figure.layout._compound_array_props.clear() - self._figure.plotly_relayout(relayout_data) + if hasattr(self.object, '_handler_js2py_layoutDelta'): + self.object.plotly_update(relayout_data=relayout_data) + elif self._figure is not None: + # The _compound_array_props are sometimes not correctly reset + # which means that they are desynchronized with _props causing + # incorrect lookups and potential errors when updating a property + self._figure.layout._compound_array_props.clear() + self._figure.plotly_relayout(relayout_data) @staticmethod def _clean_relayout_data(relayout_data): @@ -335,6 +339,7 @@ def _process_event(self, event): self.param.trigger(pname) else: self.param.update(**{pname: data}) + if data is None or not hasattr(self.object, '_handler_js2py_pointsCallback'): return @@ -392,7 +397,7 @@ def _process_event(self, event): if has_z and 'z' in point_obj: points_object['zs'].append(point_obj['z']) - self.object._handler_js2py_pointsCallback( + self._figure._handler_js2py_pointsCallback( { "new": dict( event_type=f'plotly_{etype}',