Skip to content

Commit ac448a9

Browse files
wchschloerke
andauthored
Store current session in Renderer.__call__() (#1329)
Co-authored-by: Barret Schloerke <barret@posit.co>
1 parent 8dea6ad commit ac448a9

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

shiny/render/_data_frame.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
from .._docstring import add_example
2323
from .._typing_extensions import TypedDict
2424
from .._utils import wrap_async
25-
from ..session._utils import (
26-
get_current_session,
27-
require_active_session,
28-
session_context,
29-
)
25+
from ..session._utils import require_active_session, session_context
3026
from ._data_frame_utils import (
3127
AbstractTabularData,
3228
CellPatch,
@@ -184,13 +180,6 @@ class data_frame(Renderer[DataFrameResult]):
184180
objects you can return from the rendering function to specify options.
185181
"""
186182

187-
_session: Session | None # Do not use. Use `_get_session()` instead
188-
"""
189-
Do not use! Call `._get_session()` instead!
190-
191-
Internal @render.data_frame session object.
192-
"""
193-
194183
_value: reactive.Value[DataFrameResult | None]
195184
"""
196185
Reactive value of the data frame's rendered object.
@@ -758,11 +747,6 @@ def auto_output_ui(self) -> Tag:
758747
return ui.output_data_frame(id=self.output_id)
759748

760749
def __init__(self, fn: ValueFn[DataFrameResult]):
761-
# MUST be done before super().__init__ is called as `_set_output_metadata` is
762-
# called in `super().__init__` during auto registration of the output
763-
session = get_current_session()
764-
self._session = session
765-
766750
super().__init__(fn)
767751

768752
# Set reactives from calculated properties

shiny/render/renderer/_renderer.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,6 @@ class Renderer(Generic[IT]):
137137
asynchonously.
138138
"""
139139

140-
# Set _session here because some subclasses of Renderer (e.g. data_frame) set
141-
# self._session before calling super().__init__(). If we were to set
142-
# self._session=None in the __init__ method here, it would overwrite the value from
143-
# the subclass. We avoid that by setting it here.
144-
_session: Session | None = None
145-
146140
def __call__(self, _fn: ValueFn[IT]) -> Self:
147141
"""
148142
Add the value function to the renderer.
@@ -163,6 +157,7 @@ def __call__(self, _fn: ValueFn[IT]) -> Self:
163157
:
164158
Original renderer instance.
165159
"""
160+
from ...session import get_current_session
166161

167162
if not callable(_fn):
168163
raise TypeError("Value function must be callable")
@@ -179,6 +174,8 @@ def __call__(self, _fn: ValueFn[IT]) -> Self:
179174
# for auto-registration to occur.
180175
self.output_id = self.__name__
181176

177+
self._session = get_current_session()
178+
182179
# Allow for App authors to not require `@output`
183180
self._auto_register()
184181

@@ -220,6 +217,7 @@ def __init__(
220217
# """
221218
# Renderer - init docs here
222219
# """
220+
self._session: Session | None = None
223221
super().__init__()
224222

225223
self._auto_registered: bool = False
@@ -331,16 +329,12 @@ def _auto_register(self) -> None:
331329
"""
332330
# If in Express mode, register the output
333331
if not self._auto_registered:
334-
from ...session import get_current_session
335-
336-
s = get_current_session()
337-
if s is not None:
332+
if self._session is not None:
338333
# Register output on reactive graph
339-
s.output(self)
334+
self._session.output(self)
340335
# We mark the fact that we're auto-registered so that, if an explicit
341336
# registration now occurs, we can undo this auto-registration.
342337
self._auto_registered = True
343-
self._session = s
344338

345339

346340
# Not inheriting from `WrapAsync[[], IT]` as python 3.8 needs typing extensions that

0 commit comments

Comments
 (0)