Skip to content

Matplotlib plot zooms in on resize when generated via extended task  #1343

Open
@cpsievert

Description

@cpsievert

Here's a minimal example:

import time

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

from shiny import App, Inputs, Outputs, Session, reactive, render, ui

app_ui = ui.page_fluid(
    ui.input_slider(
        "n", "input_slider()", min=10, max=100, value=50, step=5, animate=True
    ),
    ui.input_task_button("do_plot", "Do plot"),
    ui.output_plot("p"),
)


def server(input: Inputs, output: Outputs, session: Session):

    def do_plot():
        time.sleep(0.5)

        # Without ax, seaborn works through side effects, so supply the ax to avoid
        # one figure from affecting another.
        fig, ax = plt.subplots(1, 1)
        sns.lineplot(x=np.arange(100), y=np.random.randn(100), ax=ax)
        return fig

    @ui.bind_task_button(button_id="do_plot")
    @reactive.extended_task
    async def plot_task():
        return do_plot()

    @reactive.effect
    @reactive.event(do_plot)
    def _():
        plot_task()

    @render.plot
    def p():
        return plot_task.result()


app = App(app_ui, server)
zoom-on-resize.mp4

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