@@ -70,7 +70,7 @@ def __init__(
70
70
self ._contexts : set [Context ] = set ()
71
71
72
72
async def render (self ) -> Jsonifiable | None :
73
- with CurrentSessionOutput (self .output_id ):
73
+ with WidgetRenderContext (self .output_id ):
74
74
value = await self .fn ()
75
75
76
76
# Attach value/widget attributes to user func so they can be accessed (in other reactive contexts)
@@ -216,20 +216,19 @@ def set_layout_defaults(widget: Widget) -> Tuple[Widget, bool]:
216
216
217
217
return (widget , fill )
218
218
219
+ class WidgetRenderContext :
220
+ """
221
+ Let the session when a widget is currently being rendered.
219
222
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
+ """
226
226
def __init__ (self , output_id ):
227
227
self .session = require_active_session (None )
228
228
self .output_id = output_id
229
- self ._old_id = None
229
+ self ._old_id = self . session . __dict__ . get ( "__shinywidget_current_output_id" )
230
230
231
231
def __enter__ (self ):
232
- self ._old_id = self .session .__dict__ .get ("__shinywidget_current_output_id" )
233
232
self .session .__dict__ ["__shinywidget_current_output_id" ] = self .output_id
234
233
return self
235
234
@@ -238,6 +237,6 @@ def __exit__(self, exc_type, exc_value, traceback):
238
237
return False
239
238
240
239
@staticmethod
241
- def has_current_output (session ):
240
+ def is_rendering_widget (session ):
242
241
id = session .__dict__ .get ("__shinywidget_current_output_id" )
243
242
return id is not None
0 commit comments