diff --git a/CHANGELOG.md b/CHANGELOG.md index b8e2423..27f65c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [UNRELEASED] * `datetime.date()` values are properly JSON serialized. (#204) +* Fixed an issue were callbacks on a plotly `FigureWidget` object were getting dropped with recent versions of plotly. (#207, thanks @simeonschwarzenberg) ## [0.6.2] - 2025-05-21 diff --git a/shinywidgets/_render_widget_base.py b/shinywidgets/_render_widget_base.py index 5e8021a..8b07005 100644 --- a/shinywidgets/_render_widget_base.py +++ b/shinywidgets/_render_widget_base.py @@ -173,7 +173,8 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]: # Plotly provides it's own layout API (which isn't a subclass of ipywidgets.Layout) if pkg == "plotly": - from plotly.graph_objs import Layout as PlotlyLayout # pyright: ignore + from plotly.graph_objs import Layout as PlotlyLayout + from plotly.basewidget import BaseFigureWidget if isinstance(layout, PlotlyLayout): if layout.height is not None: @@ -190,7 +191,12 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]: if fill: widget._config = {"responsive": True, **widget._config} # type: ignore - widget.layout = layout + if isinstance(widget, BaseFigureWidget): + # Reassigning the layout to a FigureWidget drops installed callbacks; + # use native update_layout instead. + widget.update_layout(layout) + else: + widget.layout = layout # altair, confusingly, isn't setup to fill it's Layout() container by default. I # can't imagine a situation where you'd actually want it to _not_ fill the parent