mr_munshine
mr_munshine11mo ago

Howdy 👋. I want to do something

Howdy 👋. I want to do something seemingly simple, yet I can't seem to get it working. I want a table, where one of the columns has a button (think something like "delete"). I want a function to run when I click the button. I'm running into the issue where the buttons are not global variables, and so they don't trigger the onclick. Is there a good pattern for this?
1 Reply
mr_munshine
mr_munshineOP11mo ago
This is a simple example:
import marimo as mo

get_state, set_state = mo.state("Not called")

def f(value):
set_state(value)
return value

global_button = mo.ui.button(label="Click", value="3", on_click=f)

mo.ui.table(
data=[
{"id": "1", "button": mo.ui.button(label="Click", value="1", on_click=f)},
{"id": "2","button": mo.ui.button(label="Click", value="1", on_click=f)},
{"id": "3","button": global_button},
]
)
import marimo as mo

get_state, set_state = mo.state("Not called")

def f(value):
set_state(value)
return value

global_button = mo.ui.button(label="Click", value="3", on_click=f)

mo.ui.table(
data=[
{"id": "1", "button": mo.ui.button(label="Click", value="1", on_click=f)},
{"id": "2","button": mo.ui.button(label="Click", value="1", on_click=f)},
{"id": "3","button": global_button},
]
)
The 3rd button will work, because global var, but the other two won't. I want to allow general data management and action on the data that the table is rendering. Most of the time it's something like "delete this item", then re-render the table. I tried playing with ui.array, and ui.dictionary, but I can't get it right 😭 Ha.. I had already asked this before. It was slighty different, but the problem is the same. I'm gonna go with table value. Apologies for the duplicate 🤦‍♂️