Skip to content

Commit 63e1ea1

Browse files
authored
Ensure a session context is present when widgets are closed (#171)
1 parent b1d0bfe commit 63e1ea1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to shinywidgets will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [UNRELEASED]
9+
10+
* Fixed an issue where `@render_widget` would sometimes incorrectly render a new widget without removing the old one. (#167)
11+
812
## [0.4.1] - 2024-12-17
913

1014
* Fixed a Python 3.9 compatibility issue.

shinywidgets/_shinywidgets.py

+8-2
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)