Skip to content
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

Remove express.ui.output_* functions, add shiny.express.render #1018

Merged
merged 26 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Added `@render.download` as a replacement for `@session.download`, which is now deprecated. (#977)

* Added `ui.output_code()`, which is currently an alias for `ui.output_text_verbatim()`. (#997)

* Added `@render.code`, which is an alias for `@render.text`, but in Express mode, it displays the result using `ui.output_code()`. (#997)

### Bug fixes

* CLI command `shiny create`... (#965)
Expand Down
1 change: 1 addition & 0 deletions docs/_quartodoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ quartodoc:
- ui.output_table
- ui.output_data_frame
- ui.output_text
- ui.output_code
- ui.output_text_verbatim
- ui.output_ui
- render.plot
Expand Down
2 changes: 1 addition & 1 deletion shiny/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A package for building reactive web applications."""

__version__ = "0.6.1.9003"
__version__ = "0.6.1.9004"

from ._shinyenv import is_pyodide as _is_pyodide

Expand Down
4 changes: 2 additions & 2 deletions shiny/api-examples/Renderer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class render_capitalize(Renderer[str]):
Whether to render a placeholder value. (Defaults to `True`)
"""

def default_ui(self, id: str):
def auto_output_ui(self, id: str):
"""
Express UI for the renderer
"""
Expand Down Expand Up @@ -94,7 +94,7 @@ class render_upper(Renderer[str]):
Note: This renderer is equivalent to `render_capitalize(to="upper")`.
"""

def default_ui(self, id: str):
def auto_output_ui(self, id: str):
"""
Express UI for the renderer
"""
Expand Down
5 changes: 3 additions & 2 deletions shiny/express/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
# console.
from ..session import Inputs as _Inputs, Outputs as _Outputs, Session as _Session
from ..session import _utils as _session_utils
from .. import render
from . import ui
from ._is_express import is_express_app
from ._output import ( # noqa: F401
ui_kwargs,
suspend_display,
output_args, # pyright: ignore[reportUnusedImport]
)
from ._run import wrap_express_app
from .display_decorator import display_body


__all__ = (
"render",
"input",
"output",
"session",
"is_express_app",
"ui_kwargs",
"suspend_display",
"wrap_express_app",
"ui",
Expand Down
63 changes: 7 additions & 56 deletions shiny/express/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@
from .. import ui
from .._typing_extensions import ParamSpec
from ..render.renderer import RendererBase, RendererBaseT
from ..render.transformer import OutputRenderer
from ..render.transformer._transformer import OT

__all__ = (
"ui_kwargs",
"suspend_display",
)
__all__ = ("suspend_display",)

P = ParamSpec("P")
R = TypeVar("R")
CallableT = TypeVar("CallableT", bound=Callable[..., object])


# TODO-barret-future; quartodoc entry?
def ui_kwargs(
def output_args(
**kwargs: object,
) -> Callable[[RendererBaseT], RendererBaseT]:
"""
Expand All @@ -31,9 +26,9 @@ def ui_kwargs(
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 `@ui_kwargs` decorator to specify
arguments to be passed to `output_plot` (or whatever the corresponding UI function
is) when the render function displays itself.
implicitly to display the plot. Use the `@ui_kwargs` decorator to specify arguments
to be passed to `output_plot` (or whatever the corresponding UI function is) when
the render function displays itself.

Parameters
----------
Expand All @@ -47,51 +42,7 @@ def ui_kwargs(
"""

def wrapper(renderer: RendererBaseT) -> RendererBaseT:
# renderer._default_ui_args = args
renderer._default_ui_kwargs = kwargs
return renderer

return wrapper


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]:
if not isinstance(renderer, OutputRenderer):
raise TypeError(
f"Expected an OutputRenderer, but got {type(renderer).__name__}."
"\nIf you are trying to set default UI arguments for a `Renderer`, use"
" `@ui_kwargs` instead."
)
renderer._default_ui_args = args
renderer._default_ui_kwargs = kwargs

renderer._auto_output_ui_kwargs = kwargs
return renderer

return wrapper
Expand Down Expand Up @@ -152,7 +103,7 @@ def suspend_display(
# display yourself"
if isinstance(fn, RendererBase):
# By setting the class value, the `self` arg will be auto added.
fn.default_ui = null_ui
fn.auto_output_ui = null_ui
return fn

return suspend_display_ctxmgr()(fn)
Expand Down
29 changes: 11 additions & 18 deletions shiny/express/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
SliderStepArg,
SliderValueArg,
ValueBoxTheme,
download_button,
download_link,
brush_opts,
click_opts,
dblclick_opts,
Expand Down Expand Up @@ -95,14 +93,7 @@
notification_show,
notification_remove,
nav_spacer,
output_plot,
output_image,
output_text,
output_text_verbatim,
output_table,
output_ui,
Progress,
output_data_frame,
value_box_theme,
)

Expand Down Expand Up @@ -178,8 +169,6 @@
"SliderStepArg",
"SliderValueArg",
"ValueBoxTheme",
"download_button",
"download_link",
"brush_opts",
"click_opts",
"dblclick_opts",
Expand Down Expand Up @@ -235,14 +224,7 @@
"notification_show",
"notification_remove",
"nav_spacer",
"output_plot",
"output_image",
"output_text",
"output_text_verbatim",
"output_table",
"output_ui",
"Progress",
"output_data_frame",
"value_box_theme",
# Imports from ._cm_components
"sidebar",
Expand Down Expand Up @@ -301,6 +283,17 @@
"showcase_bottom",
"showcase_left_center",
"showcase_top_right",
# Outputs automatically placed by render functions
"download_button",
"download_link",
"output_plot",
"output_image",
"output_text",
"output_code",
"output_text_verbatim",
"output_table",
"output_ui",
"output_data_frame",
),
# Items from shiny.express.ui that don't have a counterpart in shiny.ui
"shiny.express.ui": ("page_opts",),
Expand Down
2 changes: 2 additions & 0 deletions shiny/render/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
display,
)
from ._render import (
code,
image,
plot,
table,
Expand All @@ -28,6 +29,7 @@
"data_frame",
"display",
"text",
"code",
"plot",
"image",
"table",
Expand Down
2 changes: 1 addition & 1 deletion shiny/render/_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class data_frame(Renderer[DataFrameResult]):
objects you can return from the rendering function to specify options.
"""

def default_ui(self, id: str) -> Tag:
def auto_output_ui(self, id: str) -> Tag:
return ui.output_data_frame(id=id)

async def transform(self, value: DataFrameResult) -> Jsonifiable:
Expand Down
2 changes: 1 addition & 1 deletion shiny/render/_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class display(Renderer[None]):
def default_ui(
def auto_output_ui(
self,
id: str,
*,
Expand Down
Loading
Loading