Skip to content

Commit

Permalink
Handle relayout and restyle callbacks on Plotly pane
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Jan 23, 2025
1 parent 9f703f3 commit ffe3739
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions panel/pane/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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}',
Expand Down

0 comments on commit ffe3739

Please sign in to comment.