Skip to content

Commit 872a58e

Browse files
committed
@render_widget decorator no longer needs to be used with (). Inpsired by posit-dev/py-shiny#189
1 parent fd1bc8f commit 872a58e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

ipyshiny/_ipyshiny.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import inspect
88
import json
99
import os
10-
from typing import Callable, Awaitable, Sequence, Union, cast, Any
10+
from typing import Callable, Awaitable, Sequence, Union, cast, Any, overload
1111
from uuid import uuid4
1212
from weakref import WeakSet
1313

@@ -173,7 +173,26 @@ async def __call__(self) -> object:
173173
return await self.run()
174174

175175

176-
def render_widget():
176+
@overload
177+
def render_widget(
178+
fn: Union[IPyWidgetRenderFunc, IPyWidgetRenderFuncAsync]
179+
) -> IPyWidget:
180+
...
181+
182+
183+
@overload
184+
def render_widget() -> Callable[
185+
[Union[IPyWidgetRenderFunc, IPyWidgetRenderFuncAsync]], IPyWidget
186+
]:
187+
...
188+
189+
190+
def render_widget(
191+
fn: Optional[Union[IPyWidgetRenderFunc, IPyWidgetRenderFuncAsync]] = None
192+
) -> Union[
193+
IPyWidget,
194+
Callable[[Union[IPyWidgetRenderFunc, IPyWidgetRenderFuncAsync]], IPyWidget],
195+
]:
177196
def wrapper(fn: Union[IPyWidgetRenderFunc, IPyWidgetRenderFuncAsync]) -> IPyWidget:
178197
if inspect.iscoroutinefunction(fn):
179198
fn = cast(IPyWidgetRenderFuncAsync, fn)
@@ -182,7 +201,10 @@ def wrapper(fn: Union[IPyWidgetRenderFunc, IPyWidgetRenderFuncAsync]) -> IPyWidg
182201
fn = cast(IPyWidgetRenderFunc, fn)
183202
return IPyWidget(fn)
184203

185-
return wrapper
204+
if fn is None:
205+
return wrapper
206+
else:
207+
return wrapper(fn)
186208

187209

188210
# altair/pydeck/bokeh objects aren't directly renderable as an ipywidget,

0 commit comments

Comments
 (0)