marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
5.39k stars 158 forks source link

mo.ui.table, add option selection="always_one" #1715

Open Eloitor opened 4 days ago

Eloitor commented 4 days ago

Description

I have a table with possible parameters for a calculation. Selecting a row sets the parameters for the rest of the notebook. The problem is that if no row is selected I get the error: IndexError: list index out of range.

Suggested solution

Add an option for the rows of a table to behave as radio buttons instead of check boxes.

Alternative

No response

Additional context

No response

mscolnick commented 4 days ago

You have two options that come to mind:

  1. Use mo.stop to validate that there has been selection
table = mo.ui.table(...)

# in another cell
mo.stop(not table.value, mo.callout("Must select one value"))
  1. create a "controlled" ui element
get_state, set_state = mo.state()
def handle_selection_with_no_deselect():
   ...

# in another cell
table = mo.ui.table(value=get_state(), on_change=handle_selection_with_no_deselect)
mscolnick commented 4 days ago

We could also add a min/max selection to the table as well.