Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cpsievert opened this issue Apr 29, 2024 · 0 comments
Open

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

cpsievert opened this issue Apr 29, 2024 · 0 comments

Comments

@cpsievert
Copy link
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant