Skip to content

Commit 9dc5d5c

Browse files
authored
Merge pull request #58 from bcdev/forman-x-fix_py_api_and_docs
Updated API + Docs
2 parents 0c5b03b + 1527a76 commit 9dc5d5c

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

chartlets.py/chartlets/callback.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def from_decorator(
2222
decorator_name: str,
2323
decorator_args: tuple[Any, ...],
2424
function: Any,
25-
outputs_allowed: bool = False,
25+
states_only: bool = False,
2626
) -> "Callback":
2727
try:
2828
signature = inspect.signature(function)
@@ -45,22 +45,22 @@ def from_decorator(
4545
inputs: list[Input | State] = []
4646
outputs: list[Output] = []
4747
for arg in decorator_args:
48-
if isinstance(arg, (Input, State)):
49-
inputs.append(arg)
50-
elif outputs_allowed and isinstance(arg, Output):
51-
outputs.append(arg)
52-
elif outputs_allowed:
48+
if states_only and not isinstance(arg, State):
5349
raise TypeError(
5450
f"arguments for decorator {decorator_name!r}"
55-
f" must be of type Input, State, Output, AppInput,"
56-
f" or AppOutput, but got {arg.__class__.__name__!r}"
51+
f" must be of type State,"
52+
f" but got {arg.__class__.__name__!r}"
5753
)
58-
else:
54+
if not isinstance(arg, (Input, State, Output)):
5955
raise TypeError(
6056
f"arguments for decorator {decorator_name!r}"
61-
f" must be of type Input,"
57+
f" must be of type Input, State, or Output,"
6258
f" but got {arg.__class__.__name__!r}"
6359
)
60+
if isinstance(arg, Output):
61+
outputs.append(arg)
62+
else:
63+
inputs.append(arg)
6464

6565
num_params = len(signature.parameters) - 1
6666
num_inputs = len(inputs)

chartlets.py/chartlets/contribution.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from abc import ABC
33

44
from .callback import Callback
5-
from .channel import Channel
5+
from .channel import Input, State, Output
66

77

88
class Contribution(ABC):
@@ -20,7 +20,7 @@ class Contribution(ABC):
2020
"""
2121

2222
# noinspection PyShadowingBuiltins
23-
def __init__(self, name: str, **initial_state):
23+
def __init__(self, name: str, **initial_state: Any):
2424
self.name = name
2525
self.initial_state = initial_state
2626
self.extension: str | None = None
@@ -47,7 +47,7 @@ def to_dict(self) -> dict[str, Any]:
4747
d.update(callbacks=[cb.to_dict() for cb in self.callbacks])
4848
return d
4949

50-
def layout(self, *args) -> Callable[[Callable], Callable]:
50+
def layout(self, *args: State) -> Callable[[Callable], Callable]:
5151
"""Provides a decorator for a user-provided function that
5252
returns the initial user interface layout.
5353
@@ -65,13 +65,12 @@ def layout(self, *args) -> Callable[[Callable], Callable]:
6565
called `ctx`.
6666
6767
Other parameters of the decorated function are user-defined
68-
and must have a corresponding `chartlets.Input` or
69-
`chartlets.State` arguments in the `layout` decorator in the
70-
same order.
68+
and must have a corresponding `chartlets.State` arguments
69+
in the `layout` decorator in the same order.
7170
7271
Args:
7372
args:
74-
`chartlets.Input` or `chartlets.State` objects that
73+
`chartlets.State` objects that
7574
define the source of the value for the corresponding
7675
parameter of the decorated function. Optional.
7776
@@ -81,13 +80,13 @@ def layout(self, *args) -> Callable[[Callable], Callable]:
8180

8281
def decorator(function: Callable) -> Callable:
8382
self.layout_callback = Callback.from_decorator(
84-
"layout", args, function, outputs_allowed=False
83+
"layout", args, function, states_only=True
8584
)
8685
return function
8786

8887
return decorator
8988

90-
def callback(self, *args: Channel) -> Callable[[Callable], Callable]:
89+
def callback(self, *args: Input | State | Output) -> Callable[[Callable], Callable]:
9190
"""Provide a decorator for a user-provided callback function.
9291
9392
Callback functions are event handlers that react
@@ -125,7 +124,7 @@ def callback(self, *args: Channel) -> Callable[[Callable], Callable]:
125124
def decorator(function: Callable) -> Callable:
126125
self.callbacks.append(
127126
Callback.from_decorator(
128-
"callback", args, function, outputs_allowed=True
127+
"callback", args, function, states_only=False
129128
)
130129
)
131130
return function

chartlets.py/my_extension/my_panel_2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import altair as alt
22
import pandas as pd
33

4-
from chartlets import Component, Input, Output
4+
from chartlets import Component, Input, State, Output
55
from chartlets.components import Plot, Box, Select
66
from chartlets.demo.contribs import Panel
77
from chartlets.demo.context import Context
@@ -10,7 +10,7 @@
1010
panel = Panel(__name__, title="Panel B")
1111

1212

13-
@panel.layout(Input("@app", "selectedDatasetId"))
13+
@panel.layout(State("@app", "selectedDatasetId"))
1414
def render_panel(
1515
ctx: Context,
1616
selected_dataset_id: str = "",

chartlets.py/my_extension/my_panel_3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
@panel.layout(
14-
Input("@app", "selectedDatasetId"),
14+
State("@app", "selectedDatasetId"),
1515
)
1616
def render_panel(
1717
ctx: Context,

chartlets.py/tests/callback_test.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,17 @@ def test_decorator_target(self):
158158
def test_decorator_args(self):
159159
with pytest.raises(
160160
TypeError,
161-
match="arguments for decorator 'test' must be of"
162-
" type Input, but got 'int'",
161+
match=(
162+
"arguments for decorator 'test' must be of type"
163+
" Input, State, or Output, but got 'int'"
164+
),
163165
):
164166
Callback.from_decorator("test", (13,), my_callback)
165167

166168
with pytest.raises(
167169
TypeError,
168170
match=(
169-
"arguments for decorator 'test' must be of type Input,"
170-
" State, Output, AppInput, or AppOutput, but got 'int'"
171+
"arguments for decorator 'test' must be of type State, but got 'int'"
171172
),
172173
):
173-
Callback.from_decorator("test", (13,), my_callback, outputs_allowed=True)
174+
Callback.from_decorator("test", (13,), my_callback, states_only=True)

docs/api/components.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ and providers:
1818

1919
::: chartlets.components.Checkbox
2020

21-
::: chartlets.components.Dropdown
21+
::: chartlets.components.IconButton
2222

2323
::: chartlets.components.Plot
2424

25+
::: chartlets.components.Select
26+
2527
::: chartlets.components.Typography
2628

2729
## Base classes

0 commit comments

Comments
 (0)