Chandler
Chandler9mo ago

I think I'm encountering a bug where mo.

I think I'm encountering a bug where mo.ui.button on_click does not work inside a vstack. Anyone have any suggestions for what I'm missing? Details in thread
20 Replies
Chandler
ChandlerOP9mo ago
Details below
Myles Scolnick
Myles Scolnick9mo ago
It’s not quite a bug but just a slight gotcha of our static analysis You need to declare the button outside of the vstack first (Is my guess without seeing the code)
Chandler
ChandlerOP9mo ago
Chandler
ChandlerOP9mo ago
Yup, that fixed it def _on_click(value): print('in _on_click') _button = mo.ui.button(label="Test", on_click=_on_click) _element = mo.vstack([_button]) _element Thank you Ok, now that I'm applying this to my actual marimo app, I'm wondering if it's possible to create buttons programmatically within a loop Psuedocode:
vendors = query_potential_vendors()

_element_stack = []

for vendor in vendors:
_element_stack.append(mo.ui.button(...)

mo.vstack(_element_stack)
vendors = query_potential_vendors()

_element_stack = []

for vendor in vendors:
_element_stack.append(mo.ui.button(...)

mo.vstack(_element_stack)
Myles Scolnick
Myles Scolnick9mo ago
yea you can use mo.ui.array which implements the methods of an array
Chandler
ChandlerOP9mo ago
(I updated the code) I'll give mo.ui.array a go
Chandler
ChandlerOP9mo ago
mo.ui.array works! But it doesn't look great for "user-facing" product Is there a version that doesn't show the array and the collapse?
No description
Myles Scolnick
Myles Scolnick9mo ago
you can index into it like a normal array or use a fluid api to hstack or vstack it
Chandler
ChandlerOP9mo ago
I don't know what you mean by a fluid API, but it doesn't work with a vstack. In the following example, only the last button works
def _on_click(value):
print('in _on_click')

foo_arr = ["uno", "dos", "tres"]

_element_arr = []
for label in foo_arr:
_button = mo.ui.button(label=label, on_click=_on_click)
_element_arr.append(_button)

_element = mo.vstack(_element_arr)

_element
def _on_click(value):
print('in _on_click')

foo_arr = ["uno", "dos", "tres"]

_element_arr = []
for label in foo_arr:
_button = mo.ui.button(label=label, on_click=_on_click)
_element_arr.append(_button)

_element = mo.vstack(_element_arr)

_element
But it does with work with mo.ui.array
def _on_click(value):
print('in _on_click')

foo_arr = ["uno", "dos", "tres"]

_element_arr = []
for label in foo_arr:
_button = mo.ui.button(label=label, on_click=_on_click)
_element_arr.append(_button)

_element = mo.ui.array(_element_arr)

_element
def _on_click(value):
print('in _on_click')

foo_arr = ["uno", "dos", "tres"]

_element_arr = []
for label in foo_arr:
_button = mo.ui.button(label=label, on_click=_on_click)
_element_arr.append(_button)

_element = mo.ui.array(_element_arr)

_element
Myles Scolnick
Myles Scolnick9mo ago
Hmm that is likely a bug
Chandler
ChandlerOP9mo ago
Would it be useful for me to open a GH issue?
Myles Scolnick
Myles Scolnick9mo ago
Yes that would be great
Chandler
ChandlerOP9mo ago
GitHub
Button on_clicks created within a loop do not work within vstack · ...
Describe the bug When creating a dynamic number of buttons within a vstack in a loop, only the last button's on_click behavior actually works. Environment { "marimo": "0.2.8"...
Myles Scolnick
Myles Scolnick9mo ago
Jc, what happens when you inline the button instead of a variable
Chandler
ChandlerOP9mo ago
None of the buttons worked when I inlined them inside the vstack a la elements.append(mo.ui.button())`
Myles Scolnick
Myles Scolnick9mo ago
Sorry I meant the variable in the append
Chandler
ChandlerOP9mo ago
I'm sorry, I'm not sure I understand
Myles Scolnick
Myles Scolnick9mo ago
No worries, I can try it out from you but report
Akshay
Akshay9mo ago
Thanks for making the issue Chandler. This in fact isn't a bug, though I understand why it may be surprising. I have left a response that includes a 1-line fix that makes your second example work (and explains why your code wasn't working). Hope it's helpful, let me know if you have follow-up questions. https://github.com/marimo-team/marimo/issues/929#issuecomment-1989010631
GitHub
Button on_clicks created within a loop do not work within vstack · ...
Describe the bug When creating a dynamic number of buttons within a vstack in a loop, only the last button's on_click behavior actually works. Environment { "marimo": "0.2.8"...
Chandler
ChandlerOP9mo ago
Thanks @Akshay ! Responded in the github issue. Appreciate the response 🙏