Comments (2)
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.
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)
- ASM_CONSTS[code] is not a function HOT 1
- regression bug (0.5.2->0.6.0): batch wrapped Switch looses it's on_change handler HOT 2
- I want to render bar chart based on the marimo buttons like day,week,month and year but bar chart is not rendering HOT 4
- Resuming session (refresh) loses stale status
- infinite loop with mo.state via function calling both getter and setter HOT 4
- `mo.ui.table()` view of polars dataframe fails in HTML export HOT 2
- Change md code-block style HOT 4
- ui.table renders `False` bools as `true` HOT 1
- Regression: Clicking in a cell doesn't move cursor [go-to-definition bundle] HOT 10
- Scratchpad HOT 5
- Marimo Islands Quirks (Pyodide initialization) HOT 4
- Relax Uniqueness Constraint for Write-only Variables HOT 5
- file_browser() option to select folder paths (and all files at once)
- Vim block select mode does not work
- Can't get github copilot to work with nginx and oauth2 HOT 1
- Marimo auto-markdown view modifies original code HOT 2
- The iframe element only supports the marimo.app HOT 1
- `mo.lazy` preview code shown in documentation running incorrectly HOT 1
- Sidebar mixed with command palette, original sidebar, etc HOT 1
- batch wrapped elements don't react to changes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from marimo.