Get help from the marimo community

Updated 6 months 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.
A
j
M
6 comments
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
Plain Text
# 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
you might need to pull the label itself to its own cell
Plain Text
text_label = "hello world" if not get_test() else "hello world*"
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
Add a reply
Sign up and join the conversation on Discord