|
4 | 4 |
|
5 | 5 | from typing import TYPE_CHECKING, Callable, TypeVar
|
6 | 6 |
|
7 |
| -from ._docstring import no_example |
| 7 | +from ._docstring import add_example |
8 | 8 | from ._namespaces import (
|
9 | 9 | Id,
|
10 | 10 | ResolvedId,
|
|
24 | 24 | _: Id # type: ignore
|
25 | 25 |
|
26 | 26 |
|
27 |
| -@no_example() |
| 27 | +@add_example(ex_dir="api-examples/Module") |
28 | 28 | def ui(fn: Callable[P, R]) -> Callable[Concatenate[str, P], R]:
|
| 29 | + """ |
| 30 | + Decorator for defining a Shiny module UI function. |
| 31 | +
|
| 32 | + This decorator allows you to write the UI portion of a Shiny module. |
| 33 | + When your decorated `ui` function is called with an `id`, |
| 34 | + the UI elements defined within will automatically be namespaced using that `id`. |
| 35 | + This enables reuse of UI components and consistent input/output handling |
| 36 | + when paired with a :func:`shiny.module.server` function. |
| 37 | +
|
| 38 | + Parameters |
| 39 | + ---------- |
| 40 | + fn |
| 41 | + A function that returns a Shiny UI element or layout (e.g., a `ui.panel_*` component). |
| 42 | + This function should **not** accept an `id` parameter itself; the decorator injects it. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + : |
| 47 | + A function that takes a `str` `id` as its first argument, followed by any additional |
| 48 | + parameters accepted by `fn`. When called, it returns UI elements with input/output |
| 49 | + IDs automatically namespaced using the provided module `id`. |
| 50 | +
|
| 51 | + See Also |
| 52 | + -------- |
| 53 | + * Shiny Modules documentation: <https://shiny.posit.co/py/docs/modules.html> |
| 54 | + * :func:`shiny.module.server` |
| 55 | + """ |
| 56 | + |
29 | 57 | def wrapper(id: Id, *args: P.args, **kwargs: P.kwargs) -> R:
|
30 | 58 | with namespace_context(id):
|
31 | 59 | return fn(*args, **kwargs)
|
32 | 60 |
|
33 | 61 | return wrapper
|
34 | 62 |
|
35 | 63 |
|
36 |
| -@no_example() |
| 64 | +@add_example(ex_dir="api-examples/Module") |
37 | 65 | def server(
|
38 | 66 | fn: Callable[Concatenate[Inputs, Outputs, Session, P], R],
|
39 | 67 | ) -> Callable[Concatenate[str, P], R]:
|
| 68 | + """ |
| 69 | + Decorator for defining a Shiny module server function. |
| 70 | +
|
| 71 | + This decorator is used to encapsulate the server logic for a Shiny module. |
| 72 | + It automatically creates a namespaced child `Session` using the provided module `id`, |
| 73 | + and passes the appropriate `input`, `output`, and `session` objects to your server function. |
| 74 | +
|
| 75 | + This ensures that the server logic is scoped correctly for each module instance and |
| 76 | + allows for reuse of logic across multiple instances of the same module. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + fn |
| 81 | + A server function that takes `input`, `output`, and `session` as its first |
| 82 | + three arguments, followed by any additional arguments defined by the user. |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + : |
| 87 | + A function that takes a module `id` (as a string) as its first argument, |
| 88 | + followed by any arguments expected by `fn`. When called, it will register |
| 89 | + the module's server logic in a namespaced context. |
| 90 | +
|
| 91 | + See Also |
| 92 | + -------- |
| 93 | + * Shiny Modules documentation: <https://shiny.posit.co/py/docs/modules.html> |
| 94 | + * :func:`shiny.module.ui` |
| 95 | + """ |
40 | 96 | from .session import require_active_session, session_context
|
41 | 97 |
|
42 | 98 | def wrapper(id: Id, *args: P.args, **kwargs: P.kwargs) -> R:
|
43 | 99 | sess = require_active_session(None)
|
44 | 100 | child_sess = sess.make_scope(id)
|
45 | 101 | with session_context(child_sess):
|
46 |
| - return fn(child_sess.input, child_sess.output, child_sess, *args, **kwargs) |
| 102 | + return fn( |
| 103 | + child_sess.input, |
| 104 | + child_sess.output, |
| 105 | + child_sess, |
| 106 | + *args, |
| 107 | + **kwargs, |
| 108 | + ) |
47 | 109 |
|
48 | 110 | return wrapper
|
0 commit comments