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

Ensure a session context is present when widgets are closed #171

Merged
merged 2 commits into from
Dec 18, 2024

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Dec 18, 2024

Closes #170

The reason this didn't come up in testing is because I was using an app that used invalidate_later() for invalidation, and it that case, the session is actually present .on_invalidate(). Whereas, if a reactive effect gets invalidated in the normal way, it apparently doesn't exist at .on_invalidate() time.

This seems like a bug in Shiny, but since this is such a bad regression in shinywidgets, I'm gonna do this patch fix for now:

from shiny import reactive
from shiny.express import input, ui
from shiny.reactive import get_current_context, invalidate_later
from shiny.session import get_current_session


@reactive.effect
def _():
    invalidate_later(1)

    # Prints the session, as expected
    ctx = get_current_context()
    ctx.on_invalidate(lambda: print(get_current_session()))


ui.input_action_button("btn", "Print session")


@reactive.effect
def _():
    input.btn()

    # Prints None?!?
    ctx = get_current_context()
    ctx.on_invalidate(lambda: print("Button click: ", get_current_session()))

@cpsievert cpsievert merged commit 63e1ea1 into main Dec 18, 2024
4 checks passed
@cpsievert cpsievert deleted the fix-duplicate-views branch December 18, 2024 16:21
@schloerke
Copy link
Collaborator

Shinylive app link

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.

Old views aren't always removed when new ones enter
2 participants