-
I tried to update a dropdown's options by the following code, but it does not work. Is this currently supported? Thanks. get_generic_name, set_generic_name = mo.state("")
get_specifications, set_specifications = mo.state([])
get_specification, set_specification = mo.state("")
def get_distinct_specifications(generic_name: str, *, uri: str) -> list[str]:
if not generic_name:
return []
query = f"""select distinct(specification)
from drug_dosage_admin
where generic_name REGEXP '^{re.escape(generic_name)}';"""
df = pl.read_database_uri(query, uri=uri)
return sorted(set(df["specification"].to_list()))
def on_generic_name_change(generic_name: str):
set_generic_name(generic_name)
set_specifications(
lambda _: get_distinct_specifications(get_generic_name(), uri=database_url)
)
specification.options = get_specifications()
generic_name = mo.ui.text(
get_generic_name(),
placeholder="e.g. 布洛芬缓释片",
label="通用名",
on_change=on_generic_name_change,
)
specification = mo.ui.dropdown(
get_specifications(),
value=None,
label="规格",
on_change=set_specification,
)
mo.hstack([generic_name, specification], justify="start") |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Don't use You need to use marimo's built-in reactivity. Run Notebook link: https://marimo.app/l/vfwyu0 Out of curiosity, how and why did you decide to use |
Beta Was this translation helpful? Give feedback.
Don't use
mo.state()
, and don't useon_change
handlers. They should be used exceedingly rarely, and almost always lead to trouble.You need to use marimo's built-in reactivity. Run
marimo tutorial ui
to understand how this works or read our docs.Here is an example:
Notebook link: https://marimo.app/l/vfwyu0
Out of curiosity, how and why did you decide to use
mo.state
andon_change
? I would really strongly like to discourage users from doing this.