Skip to content

Commit d9de12b

Browse files
committed
Reorganize session utils
1 parent ccd2f92 commit d9de12b

File tree

9 files changed

+42
-41
lines changed

9 files changed

+42
-41
lines changed

shiny/modules.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
from .reactive import Value
1313
from .render import RenderFunction
14-
from .session import Inputs, Outputs, Session
15-
from .session._utils import require_active_session
14+
from .session import Inputs, Outputs, Session, require_active_session
1615

1716

1817
class ModuleInputs(Inputs):

shiny/session/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
"""
44

55
from ._session import *
6+
from ._utils import *
67

7-
__all__ = ("Session", "Inputs", "Outputs", "get_current_session")
8+
__all__ = (
9+
"Session",
10+
"Inputs",
11+
"Outputs",
12+
"get_current_session",
13+
"require_active_session",
14+
)

shiny/session/_session.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__all__ = ("Session", "Inputs", "Outputs", "get_current_session", "session_context")
1+
__all__ = ("Session", "Inputs", "Outputs")
22

33
import functools
44
import os
@@ -12,8 +12,6 @@
1212
import mimetypes
1313
import dataclasses
1414
import urllib.parse
15-
from contextvars import ContextVar, Token
16-
from contextlib import contextmanager
1715
from typing import (
1816
TYPE_CHECKING,
1917
AsyncIterable,
@@ -56,7 +54,7 @@
5654
from .._fileupload import FileInfo, FileUploadManager
5755
from ..input_handler import input_handlers
5856
from ..types import SafeException, SilentCancelOutputException, SilentException
59-
from ._utils import *
57+
from ._utils import RenderedDeps, read_thunk_opt, session_context
6058

6159
# This cast is necessary because if the type checker thinks that if
6260
# "tag" isn't in `message`, then it's not a ClientMessage object.
@@ -688,24 +686,3 @@ def _is_hidden(self, name: str) -> bool:
688686
return True
689687
else:
690688
return hidden
691-
692-
693-
# ==============================================================================
694-
# Context manager for current session (AKA current reactive domain)
695-
# ==============================================================================
696-
_current_session: ContextVar[Optional[Session]] = ContextVar(
697-
"current_session", default=None
698-
)
699-
700-
701-
def get_current_session() -> Optional[Session]:
702-
return _current_session.get()
703-
704-
705-
@contextmanager
706-
def session_context(session: Optional[Session]):
707-
token: Token[Union[Session, None]] = _current_session.set(session)
708-
try:
709-
yield
710-
finally:
711-
_current_session.reset(token)

shiny/session/_utils.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
__all__ = ("get_current_session", "session_context", "require_active_session")
2+
13
import sys
2-
from typing import TYPE_CHECKING, List, Dict, Any, Optional, TypeVar, Callable, Union
4+
from contextlib import contextmanager
5+
from contextvars import ContextVar, Token
6+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, TypeVar, Union
37

48
if TYPE_CHECKING:
59
from ._session import Session
@@ -16,10 +20,29 @@ class RenderedDeps(TypedDict):
1620
html: str
1721

1822

23+
# ==============================================================================
24+
# Context manager for current session (AKA current reactive domain)
25+
# ==============================================================================
26+
_current_session: ContextVar[Optional["Session"]] = ContextVar(
27+
"current_session", default=None
28+
)
29+
30+
31+
def get_current_session() -> Optional["Session"]:
32+
return _current_session.get()
33+
34+
35+
@contextmanager
36+
def session_context(session: Optional["Session"]):
37+
token: Token[Union[Session, None]] = _current_session.set(session)
38+
try:
39+
yield
40+
finally:
41+
_current_session.reset(token)
42+
43+
1944
def require_active_session(session: Optional["Session"]) -> "Session":
2045
if session is None:
21-
from ._session import get_current_session
22-
2346
session = get_current_session()
2447
if session is None:
2548
import inspect

shiny/ui/_input_update.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
from ._input_select import SelectChoicesArg, _normalize_choices, _render_choices
3232
from ._input_slider import SliderValueArg, SliderStepArg, _slider_type, _as_numeric
3333
from .._utils import drop_none
34-
from ..session import Session
35-
from ..session._utils import require_active_session
34+
from ..session import Session, require_active_session
3635

3736
# -----------------------------------------------------------------------------
3837
# input_action_button.py

shiny/ui/_insert.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
from htmltools import TagChildArg
1212

13-
from ..session import Session
14-
from ..session._utils import require_active_session
13+
from ..session import Session, require_active_session
1514

1615

1716
def insert_ui(

shiny/ui/_modal.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
from htmltools import tags, Tag, div, HTML, TagChildArg, TagAttrArg
1717

1818
from .._utils import run_coro_sync
19-
from ..session import Session
20-
from ..session._utils import require_active_session
19+
from ..session import Session, require_active_session
2120

2221

2322
def modal_button(label: str, icon: TagChildArg = None, **kwargs: TagChildArg) -> Tag:

shiny/ui/_notification.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
from htmltools import TagList, TagChildArg
1212

1313
from .._utils import run_coro_sync, rand_hex
14-
from ..session import Session
15-
from ..session._utils import require_active_session
14+
from ..session import Session, require_active_session
1615

1716

1817
def notification_show(

shiny/ui/_progress.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from typing import Optional, Dict, Any
44
from warnings import warn
55
from .._utils import run_coro_sync, rand_hex
6-
from ..session import Session
7-
from ..session._utils import require_active_session
6+
from ..session import Session, require_active_session
87

98

109
class Progress:

0 commit comments

Comments
 (0)