-
Notifications
You must be signed in to change notification settings - Fork 60
/
_modules.py
195 lines (157 loc) · 5.99 KB
/
_modules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
__all__ = ("Module",)
from typing import Any, Callable, Optional, Union
from htmltools import TagChildArg
from ._docstring import add_example
from .reactive import Value
from .render import RenderFunction
from .session import Inputs, Outputs, Session, require_active_session, session_context
class ModuleInputs(Inputs):
"""
A class representing the inputs of a module.
Warning
-------
An instance of this class is created for each request and passed as an argument to
the :class:`shiny.modules.Module`'s ``server`` function. For this reason, you
shouldn't need to create instances of this class yourself. Furthermore, you
probably shouldn't need this class for type checking either since it has the same
signature as :class:`shiny.session.Session`.
"""
def __init__(self, ns: str, values: Inputs):
self._ns: str = ns
self._values: Inputs = values
def _ns_key(self, key: str) -> str:
return self._ns + "-" + key
def __setitem__(self, key: str, value: Value[Any]) -> None:
self._values[self._ns_key(key)].set(value)
def __getitem__(self, key: str) -> Value[Any]:
return self._values[self._ns_key(key)]
def __delitem__(self, key: str) -> None:
del self._values[self._ns_key(key)]
# Allow access of values as attributes.
def __setattr__(self, attr: str, value: Value[Any]) -> None:
if attr in ("_values", "_ns", "_ns_key"):
object.__setattr__(self, attr, value)
return
else:
self.__setitem__(attr, value)
def __getattr__(self, attr: str) -> Value[Any]:
if attr in ("_values", "_ns", "_ns_key"):
return object.__getattribute__(self, attr)
else:
return self.__getitem__(attr)
def __delattr__(self, key: str) -> None:
self.__delitem__(key)
class ModuleOutputs(Outputs):
"""
A class representing the outputs of a module.
Warning
-------
An instance of this class is created for each request and passed as an argument to
the :class:`shiny.modules.Module`'s ``server`` function. For this reason, you
shouldn't need to create instances of this class yourself. Furthermore, you
probably shouldn't need this class for type checking either since it has the same
signature as :class:`shiny.session.Session`.
"""
def __init__(self, ns: str, outputs: Outputs):
self._ns: str = ns
self._outputs: Outputs = outputs
def _ns_key(self, key: str) -> str:
return self._ns + "-" + key
def __call__(
self,
fn: Optional[RenderFunction] = None,
*,
id: Optional[str] = None,
suspend_when_hidden: bool = True,
priority: int = 0,
name: Optional[str] = None,
) -> Union[None, Callable[[RenderFunction], None]]:
if name is not None:
from . import _deprecated
_deprecated.warn_deprecated(
"`@output(name=...)` is deprecated. Use `@output(id=...)` instead."
)
id = name
def set_fn(fn: RenderFunction) -> None:
output_id = id or fn.__name__
output_id = self._ns_key(output_id)
out_fn = self._outputs(
id=output_id,
suspend_when_hidden=suspend_when_hidden,
priority=priority,
)
return out_fn(fn)
if fn is None:
return set_fn
else:
return set_fn(fn)
class ModuleSession(Session):
"""
A class representing the session of a module.
Warning
-------
An instance of this class is created for each request and passed as an argument to
the :class:`shiny.modules.Module`'s ``server`` function. For this reason, you
shouldn't need to create instances of this class yourself. Furthermore, you
probably shouldn't need this class for type checking either since it has the same
signature as :class:`shiny.session.Session`.
"""
def __init__(self, ns: str, parent_session: Session) -> None:
self._ns: str = ns
self._parent: Session = parent_session
self.input: ModuleInputs = ModuleInputs(ns, parent_session.input)
self.output: ModuleOutputs = ModuleOutputs(ns, parent_session.output)
def __getattr__(self, attr: str) -> Any:
return getattr(self._parent, attr)
@add_example()
class Module:
"""
Modularize UI and server-side logic.
Parameters
----------
ui
The module's UI definition.
server
The module's server-side logic.
"""
def __init__(
self,
ui: Callable[..., TagChildArg],
server: Callable[[ModuleInputs, ModuleOutputs, ModuleSession], None],
) -> None:
self._ui: Callable[..., TagChildArg] = ui
self._server: Callable[
[ModuleInputs, ModuleOutputs, ModuleSession], None
] = server
def ui(self, ns: str, *args: Any) -> TagChildArg:
"""
Render the module's UI.
Parameters
----------
namespace
A namespace for the module.
args
Additional arguments to pass to the module's UI definition.
"""
return self._ui(Module._make_ns_fn(ns), *args)
def server(self, ns: str, *, session: Optional[Session] = None) -> None:
"""
Invoke the module's server-side logic.
Parameters
----------
ns
A namespace for the module.
session
A :class:`~shiny.Session` instance. If not provided, it is inferred via
:func:`~shiny.session.get_current_session`.
"""
self.ns: str = ns
session = require_active_session(session)
session_proxy = ModuleSession(ns, session)
with session_context(session_proxy):
self._server(session_proxy.input, session_proxy.output, session_proxy)
@staticmethod
def _make_ns_fn(namespace: str) -> Callable[[str], str]:
def ns_fn(id: str) -> str:
return namespace + "-" + id
return ns_fn