-
First Check
Example Codefrom nicegui import ui
from pydantic import BaseModel
class Child(BaseModel):
value: int = 3
class Parent(BaseModel):
child_value: Child = Child()
other_value: int = 99
def change_value_model():
p.child_value = Child(value=p.child_value.value + 1)
p.other_value += 1
print(f"########### Proves the child is updating {p.child_value.value}")
p = Parent()
ui.label("This updates on button click")
ui.label().bind_text_from(p, 'other_value')
ui.label("None of these update on button click")
ui.label(p.child_value.value)
ui.label().bind_text_from(p.child_value, 'value')
ui.label().bind_text_from(p.child_value, 'value', backward=lambda v: v)
ui.label().bind_text(
p.child_value, 'value',
forward=lambda v: v, # when writing to pydantic model
backward=lambda v: v # when reading from pydantic model
)
ui.button("Increment", on_click=change_value_model)
ui.run(host="127.0.0.1", port=8085) DescriptionStart the app. The labels display the initialized values I can confirm the child value is getting updated through the print statements to the terminal. I tried binding a few different ways, but no love. From my reading, it looks like other control types, like Just to add to the mystery, if I add a ui.number().bind_value(
p.child_value, 'value',
forward=lambda v: v, # when writing to pydantic model
backward=lambda v: v # when reading from pydantic model
) What am I missing to understand better why the labels bound to the child don't update when I update the data object? NiceGUI Version2.20.0 Python Version3.11.9 BrowserChrome Operating SystemWindows Additional ContextFurther testing shows that if I use a nested from nicegui import ui
def change_value_model():
p2['child_value']['value'] += 1
p2['other_value'] += 1
p2 = {"other_value": 99, "child_value": {"value": 3}}
ui.label("This updates on button click")
ui.label().bind_text_from(p2, 'other_value')
ui.label("These update on button click")
#ui.label(p2['child_value']['value'])
ui.label().bind_text_from(p2['child_value'], 'value')
ui.label().bind_text_from(p2['child_value'], 'value', backward=lambda v: v)
ui.label().bind_text(
p2['child_value'], 'value',
forward=lambda v: v, # when writing to pydantic model
backward=lambda v: v # when reading from pydantic model
)
ui.button("Increment", on_click=change_value_model)
ui.run(host="127.0.0.1", port=8085) I'll also add that if I instantiate the label as a python variable, and then use a function to re-set the text, that works as expected. from nicegui import ui
from pydantic import BaseModel
class Child(BaseModel):
value: int = 3
class Parent(BaseModel):
child_value: Child = Child()
other_value: int = 99
def change_value_model():
p.child_value = Child(value=p.child_value.value + 1)
p.other_value += 1
print(f"########### Proves the child is updating {p.child_value.value}")
derived.set_text(str(p.child_value.value))
p = Parent()
ui.label("This updates on button click")
derived = ui.label().bind_text(p.child_value, 'value')
ui.button("Increment", on_click=change_value_model)
ui.run(host="127.0.0.1", port=8085) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
From how I understand it, you're creating a new child and tossing the old one. Binding does not persist after that. You may want to modify the existing child? |
Beta Was this translation helpful? Give feedback.
From how I understand it, you're creating a new child and tossing the old one. Binding does not persist after that.
You may want to modify the existing child?