Skip to content

Commit b933b1c

Browse files
committed
Use proper root_scope() API
1 parent 7e6faf8 commit b933b1c

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

shinywidgets/_render_widget_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def __init__(
7070
self._contexts: set[Context] = set()
7171

7272
async def render(self) -> Jsonifiable | None:
73-
with CurrentSessionOutput(self.output_id):
73+
with WidgetRenderContext(self.output_id):
7474
value = await self.fn()
7575

7676
# Attach value/widget attributes to user func so they can be accessed (in other reactive contexts)
@@ -216,13 +216,13 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]:
216216

217217
return (widget, fill)
218218

219+
class WidgetRenderContext:
220+
"""
221+
Let the session when a widget is currently being rendered.
219222
220-
# --------------------------------------------------------------------------------------------
221-
# Context manager to set the current output id
222-
# (this is needed since, in order to clean up widgets properly, we need to
223-
# know if they were initialized in a output context or not)
224-
# --------------------------------------------------------------------------------------------
225-
class CurrentSessionOutput:
223+
This is used to ensure that widget's that are initialized in a render_widget()
224+
context are cleaned up properly when that context is re-entered.
225+
"""
226226
def __init__(self, output_id):
227227
self.session = require_active_session(None)
228228
self.output_id = output_id
@@ -238,6 +238,6 @@ def __exit__(self, exc_type, exc_value, traceback):
238238
return False
239239

240240
@staticmethod
241-
def has_current_output(session):
241+
def is_rendering_widget(session):
242242
id = session.__dict__.get("__shinywidget_current_output_id")
243243
return id is not None

shinywidgets/_shinywidgets.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from ._cdn import SHINYWIDGETS_CDN_ONLY, SHINYWIDGETS_EXTENSION_WARNING
3030
from ._comm import BufferType, OrphanedShinyComm, ShinyComm, ShinyCommManager
3131
from ._dependencies import require_dependency
32-
from ._render_widget_base import CurrentSessionOutput, has_current_context
32+
from ._render_widget_base import WidgetRenderContext, has_current_context
3333
from ._utils import package_dir
3434

3535
__all__ = (
@@ -60,9 +60,7 @@ def init_shiny_widget(w: Widget):
6060
return
6161
# Break out of any module-specific session. Otherwise, input.shinywidgets_comm_send
6262
# will be some module-specific copy.
63-
# TODO: session = session.root_scope()
64-
while hasattr(session, "_parent"):
65-
session = cast(Session, session._parent) # pyright: ignore
63+
session = session.root_scope()
6664

6765
# If this is the first time we've seen this session, initialize some things
6866
if session not in SESSIONS:
@@ -167,7 +165,7 @@ def on_close():
167165
if id in WIDGET_INSTANCE_MAP:
168166
del WIDGET_INSTANCE_MAP[id]
169167

170-
if CurrentSessionOutput.has_current_output(session):
168+
if WidgetRenderContext.is_rendering_widget(session):
171169
ctx.on_invalidate(on_close)
172170

173171
# Keep track of what session this widget belongs to (so we can close it when the

0 commit comments

Comments
 (0)