-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a renderer generator to create sync/async render methods with ful…
…l typing (#621) Co-authored-by: Winston Chang <[email protected]>
- Loading branch information
Showing
22 changed files
with
1,743 additions
and
617 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Optional, overload | ||
|
||
from shiny import App, Inputs, Outputs, Session, ui | ||
from shiny.render.transformer import ( | ||
TransformerMetadata, | ||
ValueFn, | ||
is_async_callable, | ||
output_transformer, | ||
resolve_value_fn, | ||
) | ||
|
||
|
||
@output_transformer | ||
async def TestTextTransformer( | ||
_meta: TransformerMetadata, | ||
_fn: ValueFn[str | None], | ||
*, | ||
extra_txt: Optional[str] = None, | ||
) -> str | None: | ||
value = await resolve_value_fn(_fn) | ||
value = str(value) | ||
value += "; " | ||
value += "async" if is_async_callable(_fn) else "sync" | ||
if extra_txt: | ||
value = value + "; " + str(extra_txt) | ||
return value | ||
|
||
|
||
@overload | ||
def render_test_text( | ||
*, extra_txt: Optional[str] = None | ||
) -> TestTextTransformer.OutputRendererDecorator: | ||
... | ||
|
||
|
||
@overload | ||
def render_test_text( | ||
_fn: TestTextTransformer.ValueFn, | ||
) -> TestTextTransformer.OutputRenderer: | ||
... | ||
|
||
|
||
def render_test_text( | ||
_fn: TestTextTransformer.ValueFn | None = None, | ||
*, | ||
extra_txt: Optional[str] = None, | ||
) -> TestTextTransformer.OutputRenderer | TestTextTransformer.OutputRendererDecorator: | ||
return TestTextTransformer( | ||
_fn, | ||
TestTextTransformer.params(extra_txt=extra_txt), | ||
) | ||
|
||
|
||
app_ui = ui.page_fluid( | ||
ui.code("t1:"), | ||
ui.output_text_verbatim("t1"), | ||
ui.code("t2:"), | ||
ui.output_text_verbatim("t2"), | ||
ui.code("t3:"), | ||
ui.output_text_verbatim("t3"), | ||
ui.code("t4:"), | ||
ui.output_text_verbatim("t4"), | ||
ui.code("t5:"), | ||
ui.output_text_verbatim("t5"), | ||
ui.code("t6:"), | ||
ui.output_text_verbatim("t6"), | ||
) | ||
|
||
|
||
def server(input: Inputs, output: Outputs, session: Session): | ||
@output | ||
@render_test_text | ||
def t1(): | ||
return "t1; no call" | ||
# return "hello" | ||
|
||
@output | ||
@render_test_text | ||
async def t2(): | ||
return "t2; no call" | ||
|
||
@output | ||
@render_test_text() | ||
def t3(): | ||
return "t3; call" | ||
|
||
@output | ||
@render_test_text() | ||
async def t4(): | ||
return "t4; call" | ||
|
||
@output | ||
@render_test_text(extra_txt="w/ extra_txt") | ||
def t5(): | ||
return "t5; call" | ||
|
||
@output | ||
@render_test_text(extra_txt="w/ extra_txt") | ||
async def t6(): | ||
return "t6; call" | ||
|
||
|
||
app = App(app_ui, server) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from conftest import ShinyAppProc | ||
from controls import OutputTextVerbatim | ||
from playwright.sync_api import Page | ||
|
||
|
||
def test_output_image_kitchen(page: Page, local_app: ShinyAppProc) -> None: | ||
page.goto(local_app.url) | ||
|
||
OutputTextVerbatim(page, "t1").expect_value("t1; no call; sync") | ||
OutputTextVerbatim(page, "t2").expect_value("t2; no call; async") | ||
OutputTextVerbatim(page, "t3").expect_value("t3; call; sync") | ||
OutputTextVerbatim(page, "t4").expect_value("t4; call; async") | ||
OutputTextVerbatim(page, "t5").expect_value("t5; call; sync; w/ extra_txt") | ||
OutputTextVerbatim(page, "t6").expect_value("t6; call; async; w/ extra_txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.