Giter Club home page Giter Club logo

Comments (2)

akshayka avatar akshayka commented on May 28, 2024

Hey @jcgsville , this is a limitation of our static analysis. Even though you created 3 buttons, at the end of the for loop, you only have one variable bound to a button, namely _button, and it is assigned to the last button created in the for loop.

In general, when creating a dynamic number of UI elements, you need to use mo.ui.array or mo.ui.dictionary. If you are concerned about formatting, the elements can be pulled out and displayed however you like. I will link an example.

First, here is the corrected code for your second example:

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)

# You need to wrap your Python list in mo.ui.array and assign it to a variable
_element_arr = mo.ui.array(_element_arr)
_element = mo.vstack(_element_arr)

_element

Next, here is an example of how to have custom formatting with mo.ui.array (view in our playground):

import marimo

__generated_with = "0.3.1"
app = marimo.App()


@app.cell
def __():
    import marimo as mo
    return mo,


@app.cell
def __(mo):
    def _on_click(value):
        print("in _on_click")


    foo_arr = ["uno", "dos", "tres"]
    element_arr = mo.ui.array(
        [mo.ui.button(label=label, on_click=_on_click) for label in foo_arr]
    )
    return element_arr, foo_arr


@app.cell
def __(element_arr, mo):
    # custom formatting #1: vstack
    mo.vstack(element_arr)
    return


@app.cell
def __(element_arr, mo):
    # custom formatting #2: via element access

    mo.md(
        f"""
        Here are my buttons!

        - {element_arr[0]}
        - {element_arr[1]}
        - {element_arr[2]}
        """
    )
    return


if __name__ == "__main__":
    app.run()

If you haven't already, I do recommend reading our documentation guide on interactivity: https://docs.marimo.io/guides/interactivity.html#interactivity

There you will see the following text:

For interactions on a UI element to have any effect, the element must be assigned to a global variable.

Hope this helps!

from marimo.

jcgsville avatar jcgsville commented on May 28, 2024

Hey @akshayka. Thank you for the thorough explanation. That makes sense. I read the docs but it didn't click with me that this was an "interactivity" problem. I associated "interactivity" with state management in my brain.

Hopefully google will surface this response for the next person who similarly stumbles as I did.

Cheers, and thanks again!

from marimo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.