-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathhooks.py
More file actions
28 lines (20 loc) · 840 Bytes
/
hooks.py
File metadata and controls
28 lines (20 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from typing import Any
from reactpy import use_effect, use_state
from reactpy.types import State
from traitlets import HasTraits
def use_trait(obj: HasTraits, name: str) -> State[Any]:
"""Hook to use the attribute of a HasTraits object as a state variable
This works on Jupyter Widgets, for example.
"""
value, set_value = use_state(lambda: getattr(obj, name))
@use_effect
def register_observer():
def handle_change(change):
set_value(change["new"])
# observe the slider's value
obj.observe(handle_change, "value")
# unobserve the slider's value if this component is no longer displayed
return lambda: obj.unobserve(handle_change, "value")
def set_trait(new_value: Any) -> None:
setattr(obj, name, new_value)
return State(value, set_trait)