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:Jump to 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?...5 Replies
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?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
you might need to pull the label itself to its own cell
That seems to work! Thanks, not sure I understand why though
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