Observable: Basic API¶
Creating an Observable¶
import nuiitivet.material as nv
name = nv.Observable("Alice")
age = nv.Observable(20)
items = nv.Observable([])
Getting and Setting Values¶
Writing from a lambda¶
age.value = 21 is a statement, and a Python lambda can only hold an
expression — so it cannot be used in a callback prop or a subscribe lambda.
set() is the same write in expression form:
Use .value = wherever a statement fits; reach for set() only where one does
not. Both go through the identical write path, so equality de-duping, compare,
batching and thread dispatch behave the same either way.
Subscribing and Unsubscribing¶
In most UI cases, cleanup is handled automatically by the framework lifecycle.
Custom Comparison Function¶
By default, value equality uses ==.
You can customize comparison behavior when needed.
always_notify = nv.Observable(0, compare=lambda a, b: False)
def compare_users(a, b):
if a is None or b is None:
return a is b
return a.uid == b.uid
user = nv.Observable(None, compare=compare_users)