-
Notifications
You must be signed in to change notification settings - Fork 117
Add output_args and suspend_display decorators #786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82097a0
Add output_args and suspend_display decorators
jcheng5 8cbcb74
Add unit tests for other usages of suspend_display
jcheng5 7aeac1b
Fix typing problems
jcheng5 3793ca2
Use correct type
jcheng5 75658c0
Greatly simplify passthrough arg implementation
jcheng5 786f0cc
Fix pyright under older Python versions
jcheng5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,8 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from ._output import output_args, suspend_display | ||
|
|
||
| __all__ = ( | ||
| "output_args", | ||
| "suspend_display", | ||
| ) |
This file contains hidden or 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,126 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import contextlib | ||
| import sys | ||
| from contextlib import AbstractContextManager | ||
| from typing import Callable, TypeVar, cast, overload | ||
|
|
||
| from .. import ui | ||
| from .._typing_extensions import ParamSpec | ||
| from ..render.transformer import OutputRenderer | ||
|
|
||
| __all__ = ( | ||
| "output_args", | ||
| "suspend_display", | ||
| ) | ||
|
|
||
| OT = TypeVar("OT") | ||
| P = ParamSpec("P") | ||
| R = TypeVar("R") | ||
| CallableT = TypeVar("CallableT", bound=Callable[..., object]) | ||
|
|
||
|
|
||
| def output_args( | ||
| *args: object, **kwargs: object | ||
| ) -> Callable[[OutputRenderer[OT]], OutputRenderer[OT]]: | ||
| """Sets default UI arguments for a Shiny rendering function. | ||
|
|
||
| Each Shiny render function (like :func:`~shiny.render.plot`) can display itself when | ||
| declared within a Shiny inline-style application. In the case of | ||
| :func:`~shiny.render.plot`, the :func:`~shiny.ui.output_plot` function is called | ||
| implicitly to display the plot. Use the `@output_args` decorator to specify | ||
| arguments to be passed to `output_plot` (or whatever the corresponding UI function | ||
| is) when the render function displays itself. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| *args | ||
| Positional arguments to be passed to the UI function. | ||
| **kwargs | ||
| Keyword arguments to be passed to the UI function. | ||
|
|
||
| Returns | ||
| ------- | ||
| : | ||
| A decorator that sets the default UI arguments for a Shiny rendering function. | ||
| """ | ||
|
|
||
| def wrapper(renderer: OutputRenderer[OT]) -> OutputRenderer[OT]: | ||
| renderer.default_ui_args = args | ||
| renderer.default_ui_kwargs = kwargs | ||
| return renderer | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| @overload | ||
| def suspend_display(fn: CallableT) -> CallableT: | ||
| ... | ||
|
|
||
|
|
||
| @overload | ||
| def suspend_display() -> AbstractContextManager[None]: | ||
| ... | ||
|
|
||
|
|
||
| def suspend_display( | ||
| fn: Callable[P, R] | OutputRenderer[OT] | None = None | ||
| ) -> Callable[P, R] | OutputRenderer[OT] | AbstractContextManager[None]: | ||
| """Suppresses the display of UI elements in various ways. | ||
|
|
||
| If used as a context manager (`with suspend_display():`), it suppresses the display | ||
| of all UI elements within the context block. (This is useful when you want to | ||
| temporarily suppress the display of a large number of UI elements, or when you want | ||
| to suppress the display of UI elements that are not directly under your control.) | ||
|
|
||
| If used as a decorator (without parentheses) on a Shiny rendering function, it | ||
| prevents that function from automatically outputting itself at the point of its | ||
| declaration. (This is useful when you want to define the rendering logic for an | ||
| output, but want to explicitly call a UI output function to indicate where and how | ||
| it should be displayed.) | ||
|
|
||
| If used as a decorator (without parentheses) on any other function, it turns | ||
| Python's `sys.displayhook` into a no-op for the duration of the function call. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| fn | ||
| The function to decorate. If `None`, returns a context manager that suppresses | ||
| the display of UI elements within the context block. | ||
|
|
||
| Returns | ||
| ------- | ||
| : | ||
| If `fn` is `None`, returns a context manager that suppresses the display of UI | ||
| elements within the context block. Otherwise, returns a decorated version of | ||
| `fn`. | ||
| """ | ||
|
|
||
| if fn is None: | ||
| return suspend_display_ctxmgr() | ||
|
|
||
| # Special case for OutputRenderer; when we decorate those, we just mean "don't | ||
| # display yourself" | ||
| if isinstance(fn, OutputRenderer): | ||
| fn.default_ui = null_ui | ||
| return cast(Callable[P, R], fn) | ||
|
|
||
| return suspend_display_ctxmgr()(fn) | ||
jcheng5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def suspend_display_ctxmgr(): | ||
| oldhook = sys.displayhook | ||
| sys.displayhook = null_displayhook | ||
| try: | ||
| yield | ||
| finally: | ||
| sys.displayhook = oldhook | ||
|
|
||
|
|
||
| def null_ui(id: str, *args: object, **kwargs: object) -> ui.TagList: | ||
| return ui.TagList() | ||
|
|
||
|
|
||
| def null_displayhook(x: object) -> None: | ||
| pass | ||
This file contains hidden or 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 hidden or 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,73 @@ | ||
| import sys | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from shiny import render, ui | ||
| from shiny.express import output_args, suspend_display | ||
|
|
||
|
|
||
| def test_render_output_controls(): | ||
| @render.text | ||
| def text1(): | ||
| return "text" | ||
|
|
||
| assert ( | ||
| ui.TagList(text1.tagify()).get_html_string() | ||
| == ui.output_text_verbatim("text1").get_html_string() | ||
| ) | ||
|
|
||
| @suspend_display | ||
| @render.text | ||
| def text2(): | ||
| return "text" | ||
|
|
||
| assert ui.TagList(text2.tagify()).get_html_string() == "" | ||
|
|
||
| @output_args(placeholder=True) | ||
| @render.text | ||
| def text3(): | ||
| return "text" | ||
|
|
||
| assert ( | ||
| ui.TagList(text3.tagify()).get_html_string() | ||
| == ui.output_text_verbatim("text3", placeholder=True).get_html_string() | ||
| ) | ||
|
|
||
| @output_args(width=100) | ||
| @render.text | ||
| def text4(): | ||
| return "text" | ||
|
|
||
| with pytest.raises(TypeError, match="width"): | ||
| text4.tagify() | ||
|
|
||
|
|
||
| def test_suspend_display(): | ||
| old_displayhook = sys.displayhook | ||
| try: | ||
| called = False | ||
|
|
||
| def display_hook_spy(_): | ||
| nonlocal called | ||
| called = True | ||
|
|
||
| sys.displayhook = display_hook_spy | ||
|
|
||
| with suspend_display(): | ||
| sys.displayhook("foo") | ||
| suspend_display(lambda: sys.displayhook("bar"))() | ||
|
|
||
| @suspend_display | ||
| def whatever(x: Any): | ||
| sys.displayhook(x) | ||
|
|
||
| whatever(100) | ||
|
|
||
| assert not called | ||
|
|
||
| sys.displayhook("baz") | ||
| assert called | ||
|
|
||
| finally: | ||
| sys.displayhook = old_displayhook |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.