Skip to content

Commit

Permalink
Change Renderer.value_fn to Renderer.fn (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Jan 10, 2024
1 parent e3591ec commit 5e2b364
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
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.9001"
__version__ = "0.6.1.9002"

from ._shinyenv import is_pyodide as _is_pyodide

Expand Down
2 changes: 1 addition & 1 deletion shiny/api-examples/Renderer/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(
self.to_case = to_case

async def render(self) -> str | None:
value = await self.value_fn()
value = await self.fn()
if value is None:
# If `None` is returned, then do not render anything.
return None
Expand Down
4 changes: 2 additions & 2 deletions shiny/render/_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ async def render(self) -> JsonifiableDict | None:
orig_displayhook = sys.displayhook
sys.displayhook = wrap_displayhook_handler(results.append)

if self.value_fn.is_async():
if self.fn.is_async():
raise TypeError(
"@render.display does not support async functions. Use @render.ui instead."
)

try:
# Run synchronously
sync_value_fn = self.value_fn.get_sync_fn()
sync_value_fn = self.fn.get_sync_fn()
ret = sync_value_fn()
if ret is not None:
raise RuntimeError(
Expand Down
16 changes: 8 additions & 8 deletions shiny/render/_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,21 @@ def default_ui(

def __init__(
self,
fn: Optional[ValueFn[object]] = None,
_fn: Optional[ValueFn[object]] = None,
*,
alt: Optional[str] = None,
width: float | None | MISSING_TYPE = MISSING,
height: float | None | MISSING_TYPE = MISSING,
**kwargs: object,
) -> None:
super().__init__(fn)
super().__init__(_fn)
self.alt = alt
self.width = width
self.height = height
self.kwargs = kwargs

async def render(self) -> dict[str, Jsonifiable] | Jsonifiable | None:
is_userfn_async = self.value_fn.is_async()
is_userfn_async = self.fn.is_async()
name = self.output_id
session = require_active_session(None)
width = self.width
Expand Down Expand Up @@ -220,7 +220,7 @@ def container_size(dimension: Literal["width", "height"]) -> float:
)

# Call the user function to get the plot object.
x = await self.value_fn()
x = await self.fn()

# Note that x might be None; it could be a matplotlib.pyplot

Expand Down Expand Up @@ -325,11 +325,11 @@ def default_ui(self, id: str, **kwargs: object):

def __init__(
self,
fn: Optional[ValueFn[ImgData]] = None,
_fn: Optional[ValueFn[ImgData]] = None,
*,
delete_file: bool = False,
) -> None:
super().__init__(fn)
super().__init__(_fn)
self.delete_file: bool = delete_file

async def transform(self, value: ImgData) -> dict[str, Jsonifiable] | None:
Expand Down Expand Up @@ -413,14 +413,14 @@ def default_ui(self, id: str, **kwargs: TagAttrValue) -> Tag:

def __init__(
self,
fn: Optional[ValueFn[TableResult]] = None,
_fn: Optional[ValueFn[TableResult]] = None,
*,
index: bool = False,
classes: str = "table shiny-table w-auto",
border: int = 0,
**kwargs: object,
) -> None:
super().__init__(fn)
super().__init__(_fn)
self.index: bool = index
self.classes: str = classes
self.border: int = border
Expand Down
20 changes: 10 additions & 10 deletions shiny/render/renderer/_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,29 +293,29 @@ class Renderer(RendererBase, Generic[IT]):
TODO-barret-docs
"""

value_fn: AsyncValueFn[IT | None]
fn: AsyncValueFn[IT | None]
"""
App-supplied output value function which returns type `IT`. This function is always
asyncronous as the original app-supplied function possibly wrapped to execute
asynchonously.
"""

def __call__(self, value_fn: ValueFnApp[IT | None]) -> Self:
def __call__(self, _fn: ValueFnApp[IT | None]) -> Self:
"""
Renderer __call__ docs here; Sets app's value function
TODO-barret-docs
"""

if not callable(value_fn):
if not callable(_fn):
raise TypeError("Value function must be callable")

# Copy over function name as it is consistent with how Session and Output
# retrieve function names
self.__name__: str = value_fn.__name__
self.__name__: str = _fn.__name__

# Set value function with extra meta information
self.value_fn: AsyncValueFn[IT | None] = AsyncValueFn(value_fn)
self.fn: AsyncValueFn[IT | None] = AsyncValueFn(_fn)

# Allow for App authors to not require `@output`
self._auto_register()
Expand All @@ -324,17 +324,17 @@ def __call__(self, value_fn: ValueFnApp[IT | None]) -> Self:

def __init__(
self,
value_fn: ValueFn[IT | None] = None,
_fn: ValueFn[IT | None] = None,
):
# Do not display docs here. If docs are present, it could highjack the docs of
# the subclass's `__init__` method.
# """
# Renderer - init docs here
# """
super().__init__()
if callable(value_fn):
if callable(_fn):
# Register the value function
self(value_fn)
self(_fn)

async def transform(self, value: IT) -> Jsonifiable:
"""
Expand All @@ -345,7 +345,7 @@ async def transform(self, value: IT) -> Jsonifiable:
raise NotImplementedError(
"Please implement either the `transform(self, value: IT)` or `render(self)` method.\n"
"* `transform(self, value: IT)` should transform the `value` (of type `IT`) into Jsonifiable object. Ex: `dict`, `None`, `str`. (standard)\n"
"* `render(self)` method has full control of how an App author's value is retrieved (`self.value_fn()`) and utilized. (rare)\n"
"* `render(self)` method has full control of how an App author's value is retrieved (`self._fn()`) and utilized. (rare)\n"
"By default, the `render` retrieves the value and then calls `transform` method on non-`None` values."
)

Expand All @@ -355,7 +355,7 @@ async def render(self) -> Jsonifiable:
TODO-barret-docs
"""
value = await self.value_fn()
value = await self.fn()
if value is None:
return None

Expand Down
6 changes: 3 additions & 3 deletions shiny/render/transformer/_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ def __init__(
# Checking if a function is async has a 180+ns overhead (barret's machine)
# -> It is faster to always call an async function than to always check if it is async
# Always being async simplifies the execution
self._value_fn = AsyncValueFn(value_fn)
self._value_fn_is_async = self._value_fn.is_async() # legacy key
self._fn = AsyncValueFn(value_fn)
self._value_fn_is_async = self._fn.is_async() # legacy key
self.__name__ = value_fn.__name__

self._transformer = transform_fn
Expand Down Expand Up @@ -304,7 +304,7 @@ async def _run(self) -> OT:
# TransformerMetadata
self._meta(),
# Callable[[], Awaitable[IT]]
self._value_fn,
self._fn,
# P
*self._params.args,
**self._params.kwargs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def default_ui(self, id: str) -> Tag:

# The init method is used to set up the renderer's parameters.
# If no parameters are needed, then the `__init__()` method can be omitted.
def __init__(self, _value_fn: ValueFn[int] = None, *, height: str = "200px"):
super().__init__(_value_fn)
def __init__(self, _fn: ValueFn[int] = None, *, height: str = "200px"):
super().__init__(_fn)
self.height: str = height

# Transforms non-`None` values into a `Jsonifiable` object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def default_ui(self, id: str) -> Tag:

# # There are no parameters being supplied to the `output_custom_component` rendering function.
# # Therefore, we can omit the `__init__()` method.
# def __init__(self, _value_fn: ValueFn[int] = None, *, extra_arg: str = "bar"):
# super().__init__(_value_fn)
# def __init__(self, _fn: ValueFn[int] = None, *, extra_arg: str = "bar"):
# super().__init__(_fn)
# self.extra_arg: str = extra_arg

# Transforms non-`None` values into a `Jsonifiable` object.
Expand Down

0 comments on commit 5e2b364

Please sign in to comment.