Skip to content

Commit 32c2be0

Browse files
committed
Close #170: ensure a session context is present when widgets are closed
1 parent b1d0bfe commit 32c2be0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

shinywidgets/_shinywidgets.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from shiny import Session, reactive
2222
from shiny.http_staticfiles import StaticFiles
2323
from shiny.reactive._core import get_current_context
24-
from shiny.session import get_current_session, require_active_session
24+
from shiny.session import get_current_session, require_active_session, session_context
2525

2626
from ._as_widget import as_widget
2727
from ._cdn import SHINYWIDGETS_CDN_ONLY, SHINYWIDGETS_EXTENSION_WARNING
@@ -37,6 +37,7 @@
3737

3838
if TYPE_CHECKING:
3939
from typing import TypeGuard
40+
4041
from traitlets.traitlets import Instance
4142

4243

@@ -129,7 +130,12 @@ def _cleanup_session_state():
129130
# If we're in a reactive context, close this widget when the context is invalidated
130131
if has_current_context():
131132
ctx = get_current_context()
132-
ctx.on_invalidate(lambda: w.close())
133+
134+
def on_close():
135+
with session_context(session):
136+
w.close()
137+
138+
ctx.on_invalidate(on_close)
133139

134140
# Keep track of what session this widget belongs to (so we can close it when the
135141
# session ends)

0 commit comments

Comments
 (0)