Skip to content

Plotly layout callbacks get dropped when rendering FigureWidget #206

@simeonschwarzenberg

Description

@simeonschwarzenberg

Hi, im trying to get plotlys layout callbacks working with python shiny. Currently installing an on_change callback on the FigureWidgets layout will not persist to the rendered widget.

Now I've tracked down the problem to be coming from this assignment widget.layout = layout in line 193 of ~shinywidgets._render_widget_base.render_widget_base.set_layout_defaults. This somehow gets rid of the callbacks installed on the widgets layout (I've checked with print(widget.layout._change_callbacks)). This should just be a case of using plotlys own update_layout function if the widget is a plotly FigureWidget. For me replacing the assignment with the following snippet made the callbacks persist and work:

from plotly.basewidget import BaseFigureWidget
if isinstance(widget, BaseFigureWidget):
    widget.update_layout(layout)
else:
    widget.layout = layout

As a simple example application i used this:

from shiny import App, ui
from shinywidgets import render_plotly, output_widget
import numpy as np
import plotly.graph_objs as go

app_ui = ui.page_fluid(
    output_widget("plot")
)

def server(input, output, session):
    @render_plotly
    def plot():
        x = np.linspace(0, 10, 100)
        y = np.sin(x)
        fig = go.Figure(data=go.Scatter(x=x, y=y, mode="lines"))
        fw = go.FigureWidget(fig)
        fw.layout.on_change(_update_x_range, "xaxis.range")
        return fw

    def _update_x_range(layout, x_range):
        print(x_range)

app = App(app_ui, server)
if __name__ == "__main__":
    app.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions