|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Optional, overload |
| 4 | + |
| 5 | +from shiny import App, Inputs, Outputs, Session, ui |
| 6 | +from shiny.render.transformer import ( |
| 7 | + TransformerMetadata, |
| 8 | + ValueFn, |
| 9 | + is_async_callable, |
| 10 | + output_transformer, |
| 11 | + resolve_value_fn, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +@output_transformer |
| 16 | +async def TestTextTransformer( |
| 17 | + _meta: TransformerMetadata, |
| 18 | + _fn: ValueFn[str | None], |
| 19 | + *, |
| 20 | + extra_txt: Optional[str] = None, |
| 21 | +) -> str | None: |
| 22 | + value = await resolve_value_fn(_fn) |
| 23 | + value = str(value) |
| 24 | + value += "; " |
| 25 | + value += "async" if is_async_callable(_fn) else "sync" |
| 26 | + if extra_txt: |
| 27 | + value = value + "; " + str(extra_txt) |
| 28 | + return value |
| 29 | + |
| 30 | + |
| 31 | +@overload |
| 32 | +def render_test_text( |
| 33 | + *, extra_txt: Optional[str] = None |
| 34 | +) -> TestTextTransformer.OutputRendererDecorator: |
| 35 | + ... |
| 36 | + |
| 37 | + |
| 38 | +@overload |
| 39 | +def render_test_text( |
| 40 | + _fn: TestTextTransformer.ValueFn, |
| 41 | +) -> TestTextTransformer.OutputRenderer: |
| 42 | + ... |
| 43 | + |
| 44 | + |
| 45 | +def render_test_text( |
| 46 | + _fn: TestTextTransformer.ValueFn | None = None, |
| 47 | + *, |
| 48 | + extra_txt: Optional[str] = None, |
| 49 | +) -> TestTextTransformer.OutputRenderer | TestTextTransformer.OutputRendererDecorator: |
| 50 | + return TestTextTransformer( |
| 51 | + _fn, |
| 52 | + TestTextTransformer.params(extra_txt=extra_txt), |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +app_ui = ui.page_fluid( |
| 57 | + ui.code("t1:"), |
| 58 | + ui.output_text_verbatim("t1"), |
| 59 | + ui.code("t2:"), |
| 60 | + ui.output_text_verbatim("t2"), |
| 61 | + ui.code("t3:"), |
| 62 | + ui.output_text_verbatim("t3"), |
| 63 | + ui.code("t4:"), |
| 64 | + ui.output_text_verbatim("t4"), |
| 65 | + ui.code("t5:"), |
| 66 | + ui.output_text_verbatim("t5"), |
| 67 | + ui.code("t6:"), |
| 68 | + ui.output_text_verbatim("t6"), |
| 69 | +) |
| 70 | + |
| 71 | + |
| 72 | +def server(input: Inputs, output: Outputs, session: Session): |
| 73 | + @output |
| 74 | + @render_test_text |
| 75 | + def t1(): |
| 76 | + return "t1; no call" |
| 77 | + # return "hello" |
| 78 | + |
| 79 | + @output |
| 80 | + @render_test_text |
| 81 | + async def t2(): |
| 82 | + return "t2; no call" |
| 83 | + |
| 84 | + @output |
| 85 | + @render_test_text() |
| 86 | + def t3(): |
| 87 | + return "t3; call" |
| 88 | + |
| 89 | + @output |
| 90 | + @render_test_text() |
| 91 | + async def t4(): |
| 92 | + return "t4; call" |
| 93 | + |
| 94 | + @output |
| 95 | + @render_test_text(extra_txt="w/ extra_txt") |
| 96 | + def t5(): |
| 97 | + return "t5; call" |
| 98 | + |
| 99 | + @output |
| 100 | + @render_test_text(extra_txt="w/ extra_txt") |
| 101 | + async def t6(): |
| 102 | + return "t6; call" |
| 103 | + |
| 104 | + |
| 105 | +app = App(app_ui, server) |
0 commit comments