|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Literal |
| 4 | + |
| 5 | +from shiny import App, Inputs, Outputs, Session, ui |
| 6 | +from shiny.render.renderer import Renderer, ValueFn |
| 7 | + |
| 8 | +####### |
| 9 | +# Start of package author code |
| 10 | +####### |
| 11 | + |
| 12 | + |
| 13 | +class render_capitalize(Renderer[str]): |
| 14 | + # The documentation for the class will be displayed when the user hovers over the |
| 15 | + # decorator when **no** parenthesis are used. Ex: `@render_capitalize` |
| 16 | + # If no documentation is supplied to the `__init__()` method, then this |
| 17 | + # documentation will be displayed when parenthesis are used on the decorator. |
| 18 | + """ |
| 19 | + Render capitalize class documentation goes here. |
| 20 | + """ |
| 21 | + |
| 22 | + to_case: Literal["upper", "lower", "ignore"] |
| 23 | + """ |
| 24 | + The case to render the value in. |
| 25 | + """ |
| 26 | + placeholder: bool |
| 27 | + """ |
| 28 | + Whether to render a placeholder value. (Defaults to `True`) |
| 29 | + """ |
| 30 | + |
| 31 | + def default_ui(self, id: str): |
| 32 | + """ |
| 33 | + Express UI for the renderer |
| 34 | + """ |
| 35 | + return ui.output_text_verbatim(id, placeholder=self.placeholder) |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + _fn: ValueFn[str | None] | None = None, |
| 40 | + *, |
| 41 | + to_case: Literal["upper", "lower", "ignore"] = "upper", |
| 42 | + placeholder: bool = True, |
| 43 | + ) -> None: |
| 44 | + # If a different set of documentation is supplied to the `__init__` method, |
| 45 | + # then this documentation will be displayed when parenthesis are used on the decorator. |
| 46 | + # Ex: `@render_capitalize()` |
| 47 | + """ |
| 48 | + Render capitalize documentation goes here. |
| 49 | +
|
| 50 | + It is a good idea to talk about parameters here! |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + to_case |
| 55 | + The case to render the value. (`"upper"`) |
| 56 | +
|
| 57 | + Options: |
| 58 | + - `"upper"`: Render the value in upper case. |
| 59 | + - `"lower"`: Render the value in lower case. |
| 60 | + - `"ignore"`: Do not alter the case of the value. |
| 61 | +
|
| 62 | + placeholder |
| 63 | + Whether to render a placeholder value. (`True`) |
| 64 | + """ |
| 65 | + # Do not pass params |
| 66 | + super().__init__(_fn) |
| 67 | + self.widget = None |
| 68 | + self.to_case = to_case |
| 69 | + |
| 70 | + async def render(self) -> str | None: |
| 71 | + value = await self.value_fn() |
| 72 | + if value is None: |
| 73 | + # If `None` is returned, then do not render anything. |
| 74 | + return None |
| 75 | + |
| 76 | + ret = str(value) |
| 77 | + if self.to_case == "upper": |
| 78 | + return ret.upper() |
| 79 | + if self.to_case == "lower": |
| 80 | + return ret.lower() |
| 81 | + if self.to_case == "ignore": |
| 82 | + return ret |
| 83 | + raise ValueError(f"Invalid value for `to_case`: {self.to_case}") |
| 84 | + |
| 85 | + |
| 86 | +class render_upper(Renderer[str]): |
| 87 | + """ |
| 88 | + Minimal capitalize string transformation renderer. |
| 89 | +
|
| 90 | + No parameters are supplied to this renderer. This allows us to skip the `__init__()` |
| 91 | + method and `__init__()` documentation. If you hover over this decorator with and |
| 92 | + without parenthesis, you will see this documentation in both situations. |
| 93 | +
|
| 94 | + Note: This renderer is equivalent to `render_capitalize(to="upper")`. |
| 95 | + """ |
| 96 | + |
| 97 | + def default_ui(self, id: str): |
| 98 | + """ |
| 99 | + Express UI for the renderer |
| 100 | + """ |
| 101 | + return ui.output_text_verbatim(id, placeholder=True) |
| 102 | + |
| 103 | + async def transform(self, value: str) -> str: |
| 104 | + """ |
| 105 | + Transform the value to upper case. |
| 106 | +
|
| 107 | + This method is shorthand for the default `render()` method. It is useful to |
| 108 | + transform non-`None` values. (Any `None` value returned by the app author will |
| 109 | + be forwarded to the browser.) |
| 110 | +
|
| 111 | + Parameters |
| 112 | + ---------- |
| 113 | + value |
| 114 | + The a non-`None` value to transform. |
| 115 | +
|
| 116 | + Returns |
| 117 | + ------- |
| 118 | + str |
| 119 | + The transformed value. (Must be a subset of `Jsonifiable`.) |
| 120 | + """ |
| 121 | + |
| 122 | + return str(value).upper() |
| 123 | + |
| 124 | + |
| 125 | +####### |
| 126 | +# End of package author code |
| 127 | +####### |
| 128 | + |
| 129 | + |
| 130 | +####### |
| 131 | +# Start of app author code |
| 132 | +####### |
| 133 | + |
| 134 | + |
| 135 | +def text_row(id: str, label: str): |
| 136 | + return ui.tags.tr( |
| 137 | + ui.tags.td(f"{label}:"), |
| 138 | + ui.tags.td(ui.output_text_verbatim(id, placeholder=True)), |
| 139 | + ) |
| 140 | + return ui.row( |
| 141 | + ui.column(6, f"{id}:"), |
| 142 | + ui.column(6, ui.output_text_verbatim(id, placeholder=True)), |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +app_ui = ui.page_fluid( |
| 147 | + ui.h1("Capitalization renderer"), |
| 148 | + ui.input_text("caption", "Caption:", "Data summary"), |
| 149 | + ui.tags.table( |
| 150 | + text_row("upper", "@render_upper"), |
| 151 | + text_row("upper_with_paren", "@render_upper()"), |
| 152 | + # |
| 153 | + text_row("cap_upper", "@render_capitalize"), |
| 154 | + text_row("cap_lower", "@render_capitalize(to='lower')"), |
| 155 | + ), |
| 156 | +) |
| 157 | + |
| 158 | + |
| 159 | +def server(input: Inputs, output: Outputs, session: Session): |
| 160 | + # Hovering over `@render_upper` will display the class documentation |
| 161 | + @render_upper |
| 162 | + def upper(): |
| 163 | + return input.caption() |
| 164 | + |
| 165 | + # Hovering over `@render_upper` will display the class documentation as there is no |
| 166 | + # `__init__()` documentation |
| 167 | + @render_upper() |
| 168 | + def upper_with_paren(): |
| 169 | + return input.caption() |
| 170 | + |
| 171 | + # Hovering over `@render_capitalize` will display the class documentation |
| 172 | + @render_capitalize |
| 173 | + def cap_upper(): |
| 174 | + return input.caption() |
| 175 | + |
| 176 | + # Hovering over `@render_capitalize` will display the `__init__()` documentation |
| 177 | + @render_capitalize(to_case="lower") |
| 178 | + def cap_lower(): |
| 179 | + return input.caption() |
| 180 | + |
| 181 | + |
| 182 | +app = App(app_ui, server) |
0 commit comments