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

Remove widget container after plotly widget view gets destroyed #178

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

cpsievert
Copy link
Collaborator

Follow up to #169.

Discovered via https://discord.com/channels/1109483223987277844/1115318728301740112/threads/1331842445071220737

Here's a more minimal reprex of the problem. Note that, to see the problem, click 999999, then go back to 123456. An unwanted empty space is above each plot because the .lm-Widget container element from the previous render doesn't get removed.

import plotly.express as px
from shiny import App, reactive, render, ui

from shinywidgets import output_widget, render_widget

app_ui = ui.page_fluid(
    ui.input_radio_buttons(
        id="station_id_rb",
        label=None,
        choices=["123456", "999999", "abcdef", "None"],
        inline=True,
    ),
    ui.output_ui("toggled_controls"),
)

def stock_plot(stock_ticker: str, location: str):
    df = px.data.stocks()
    return px.line(df, x='date', y=stock_ticker, title=f"location: {location}")

def server(input, output, session):

    stock_tickers = ['GOOG', 'AAPL']

    @render.ui
    def toggled_controls():
        location = input.station_id_rb()
        if location is None or location == "None":
            return "location_unknown"
        else:
            return [
                output_widget(f"plot_{s}_{location}") for s in stock_tickers
            ]

    @reactive.effect
    def _():
        location = input.station_id_rb()
        if location is None or location == "None":
            return
        else:
            for s in stock_tickers:
                @output(id=f"plot_{s}_{location}")
                @render_widget
                def _():
                    return stock_plot(s, location)


app = App(app_ui, server)

@cpsievert cpsievert merged commit a315cc7 into main Jan 23, 2025
5 checks passed
@cpsievert cpsievert deleted the plotly-cleanup-view-fix branch January 23, 2025 17:41
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

Successfully merging this pull request may close these issues.

1 participant