SolaraS
Solara2y ago
5 replies
pikaa

Plotly FigureWidget axes do not reset ranges within solara's render context

Upon applying an effect to set autorange=True to a figure with autorange='reversed', the Figure does not return to the original axes orientation.

I'm wondering what happens behind the scenes here for the render when the use_effect is triggered. I can upload a ipynb with its expected behaviour within a jupyter context vs its behaviour in solara.

The partial code (can give complete if needed). plotstate contains 2 reactives for flipx and flipy.
def imshowfig()    
    def create_fig():
        z, cmin, cmax = perform_binning()
        fig = px.imshow(z.T,
                        zmin=cmin,
                        zmax=cmax,
                        origin="lower",
                        color_continuous_scale="inferno")
        return fig

    # only instantiate the figure once
    figure = sl.use_memo(create_fig, dependencies=[])
    
    def add_effects(fig_element: sl.Element):

        def set_xflip():
            print("setting xflip", plotstate.flipx.value)
            fig_widget: FigureWidget = sl.get_widget(fig_element)
            if plotstate.flipx.value:
                fig_widget.update_xaxes(autorange="reversed")
            else:
                fig_widget.update_xaxes(autorange=True)
            print(fig_widget.layout.xaxis)

        def set_yflip():
            print("setting yflip", plotstate.flipy.value)
            fig_widget: FigureWidget = sl.get_widget(fig_element)
            if plotstate.flipy.value:
                fig_widget.update_yaxes(autorange="reversed")
            else:
                fig_widget.update_yaxes(autorange=True)
            print(fig_widget.layout.yaxis)

        sl.use_effect(set_xflip, dependencies=[plotstate.flipx.value])
        sl.use_effect(set_yflip, dependencies=[plotstate.flipy.value])

    fig_el = sl.FigurePlotly(figure)
    add_effects(fig_el)
Was this page helpful?