john_helt
john_helt6mo ago

Possibility of modifying object to which on_change handler is bound

I'm looking for a way to modify an object if it's value changes. Specifically I want to change the label of an object, if the value changes. I can add an on_change handler to detect changes, but once the object is created, I can no longer access it's internals. I tried asking the #ask-docs-ai channel, but I think it's hallucinating: https://discord.com/channels/1059888774789730424/1228123354607648788/1255236745717485568. If the "self" object was part of on_change function, i.e the function signature always contained "self", and if "self" objects, for which the on_change functions are bindable, could have their property setters, i.e. label, value etc. exposed, then I think it should be possible.
Discord
Discord - Group Chat That’s All Fun & Games
Discord is great for playing games and chilling with friends, or even building a worldwide community. Customize your own space to talk, play, and hang out.
Solution:
That's not possible today unfortunately, UI elements are not mutable in that way. If you need a cycle like that, you'll need to use mo.state and wire things up so that the cell that creates the UI element re-runs on change. Could that work?...
Jump to solution
5 Replies
Solution
Akshay
Akshay6mo ago
That's not possible today unfortunately, UI elements are not mutable in that way. If you need a cycle like that, you'll need to use mo.state and wire things up so that the cell that creates the UI element re-runs on change. Could that work?
john_helt
john_heltOP6mo ago
Hi, thanks for the reply. I tried with something like this, where on_change calls a state setter, and the cell that creates the UI element, with this on_change bound to it, is initialized with the same state getter, but it seems I need to manually re-run the cell to get it to update
# Cell 1
get_test, set_test = mo.state(False)

# Cell 2
_test=get_test()
def _on_change(*args):
set_test(True)
_t= mo.ui.text(label="hello world" if not _test else "hello world*", on_change=_on_change)

_t
# Cell 1
get_test, set_test = mo.state(False)

# Cell 2
_test=get_test()
def _on_change(*args):
set_test(True)
_t= mo.ui.text(label="hello world" if not _test else "hello world*", on_change=_on_change)

_t
Myles Scolnick
Myles Scolnick6mo ago
you might need to pull the label itself to its own cell
text_label = "hello world" if not get_test() else "hello world*"
text_label = "hello world" if not get_test() else "hello world*"
john_helt
john_heltOP6mo ago
That seems to work! Thanks, not sure I understand why though
Myles Scolnick
Myles Scolnick6mo ago
in order to avoid circular references. when you call set_state, we don't re-run the cell with get_state. so cell 2 does not get run