Creating a list of buttons
Hi there! I am working with a
list
of button
s that upon clicking the button_i, it looks up a list of strings and displays the i^th string in a text area. I cannot seem to make it work after reviewing the documentation on button and state (excellent example of the task list!)
The main obstacle for me is making the on_click
argument work for a list of buttons. For example,
When I try to click on either button, the output from cell 2 or cell 3 does not change, whereas if I were to create a single button, the same initialization does work as expected when clicked upon.
How may I modify the code or address the problem otherwise? Any guidance is much appreciated!10 Replies
hi @๐ , in order for ui elements to be registered in the scope of the notebook/application, they need to be bound to a variable in the global scope.
at the moment (maybe in in the future), they cannot just be within a dict or list that is bound within the scope. for example:
my_list = [button, button]
is not sufficient. so we have built-in higher-order components to make this work. instead you will need to do:
here are some docs for array and dictionaryThanks, Myles! Dictionary worked. Thanks so much life is beautiful again ๐คฉ๐คฉ definitely missed that in the documentation and should have thought of global scope.
Hi! I have a follow up question - I want to display my_dict without showing the key and dictionary view. What I can manage is mo.vstack([my_dict.elements[k] for k in my_dict.value.keys()]). But that seems to have disabled the button on_click functionality. Is there a way to do that? I wonder if itโs scope issue again. Sorry typing on the phone so not writing the code view properly.
Oh oddโฆ it works now
There may be the odd issue with this. We are actually better support for zoom/lens/getters for a use case to add these elements, such as putting these elements into a table or nested stack like this
@๐ , to create a higher-order element with a custom layout, you can use
mo.ui.batch
instead of mo.ui.dictionary
: https://docs.marimo.io/api/inputs/batch.html#marimo.ui.batch
Hopefully that helps! Let us know if not. Here's a code example from the linked-to docs.
Hi @Akshay and @Myles Scolnick , thanks a lot for the followups; I haven't been able to fully dedicate time to this button issue. It turns out I had a hack it seems... and it's still not ideal.
I am wondering if for the following use case, what I have is the only way.
Use case: I want to create a vertical stack of N buttons, where N can vary. Each button is supposed to initiate an action according to the index of the button in the N-array. In addition, I prefer not to see "dictionary" or "array".
What I have right now:
The look I'd like is in
# cell 4
and not # cell 5
but unfortunately the reactivity in get_x()
does not work if I only have # cell 4
without cell 5
...
I have tried `mo.ui.batch
suggested but I could not get the dynamic change of the number of buttons according to N work. Is there something else I can try, or modify? Thank you!Hi! I will take a look today, and will write back later.
Hi @๐ ! I've attached an implementation using
mo.ui.batch
that works with dynamic N
. We recently fixed a bug with batch so please make sure you're using the latest version of marimo. Let me know if you have any questions!Thanks! @Akshay . Out now and will take a look soon. Thank you for looking into this so promptly!
It worked! Hallelujah thank you so much! I donโt fully understand the code thoughโฆ is this making the dictionary of buttons twice?
It's making just one dictionary of buttons.
batch
is similar conceptually to mo.ui.dictionary
, except it lets you display the UI elements in arbitrary HTML.
You can learn more at the documentation: https://docs.marimo.io/api/inputs/batch.html#marimo.ui.batch
Here's a breakdown of the code I provided.
This creates HTML with placeholders for the buttons:
If you expand the list comprehension, this looks like mo.vstack(["{button_0}", "{button_1"}, ...])
: the braces are placeholders for UI elements {}
This substitutes the buttons for the placeholders:
The keys are the placeholders and the values are the UI elements to substitute for those placeholders.
The example in the docs might be easier to understandThank you!!
(I didn't get a notification so didn't see the response till now!!)