marimo icon indicating copy to clipboard operation
marimo copied to clipboard

Shortcut-execution sometimes repeated for "cloned" buttons

Open MrHenning opened this issue 7 months ago • 3 comments

Describe the bug

I create a mo.ui.button with a defined shortcut, having multiple copies of if in the notebook. If I press the shortcut, the button may get executed twice. The more copies I have, the higher the chance that it is executed twice:

Image

Here I pressed alt+z twice.

Same behaviour in app-mode.

Expected behaviour:

Each button should be executed the same number of time, showing 2 as the highest value when pressing alt+z twice.

Environment

{
  "marimo": "0.12.8",
  "OS": "Linux",
  "OS Version": "6.11.0-21-generic",
  "Processor": "x86_64",
  "Python Version": "3.12.10",
  "Binaries": {
    "Browser": "135.0.7049.84",
    "Node": "v22.13.0"
  },
  "Dependencies": {
    "click": "8.1.8",
    "docutils": "0.21.2",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.2",
    "markdown": "3.6",
    "narwhals": "1.34.1",
    "packaging": "24.2",
    "psutil": "7.0.0",
    "pygments": "2.19.1",
    "pymdown-extensions": "10.14.3",
    "pyyaml": "6.0.2",
    "ruff": "0.11.5",
    "starlette": "0.46.1",
    "tomlkit": "0.13.2",
    "typing-extensions": "4.13.2",
    "uvicorn": "0.34.0",
    "websockets": "15.0.1"
  },
  "Optional Dependencies": {
    "pandas": "2.2.3",
    "polars": "1.27.1"
  },
  "Experimental Flags": {
    "rtc": false,
    "tracing": true
  }
}

Code to reproduce

import marimo

__generated_with = "0.12.8"
app = marimo.App(width="medium")


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


@app.cell
def inc():
    def inc(val):
        val += 1
        print(val)
        return val
    return (inc,)


@app.cell
def _():
    val_init = 0
    return (val_init,)


@app.cell
def _(inc, mo, val_init):
    button_single = mo.ui.button(value=val_init, on_click=inc, keyboard_shortcut="alt+z", label="single")
    return (button_single,)


@app.cell
def _(button_single):
    button_single
    return


@app.cell
def _(inc, mo, val_init):
    button_multi = mo.ui.button(value=val_init, on_click=inc, keyboard_shortcut="alt+z", label="double")
    return (button_multi,)


@app.cell
def _(button_multi):
    button_multi
    return


@app.cell
def _(button_multi):
    button_multi
    return


@app.cell
def _():
    return


@app.cell
def _():
    return


@app.cell
def _():
    return


@app.cell
def _():
    return


@app.cell
def _():
    return


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


MrHenning avatar Apr 15 '25 15:04 MrHenning

@MrHenning what is your use-case for buttons with the same shortcut? this seems like a code-smell.

mscolnick avatar Apr 15 '25 17:04 mscolnick

It's just to demonstrate that if I press the shortcut N number of times, the two buttons are called different number of times (I didn't know how to demonstrate it differently).

The issue is not when there are 2 different buttons, but multiple copies of the same button (button_multi). The behaviour is the same if I remove the button_single.

MrHenning avatar Apr 15 '25 19:04 MrHenning

Oh, got it, that makes more sense. We should dedupe by the UI element ID, and actually should just stop the event propagation

mscolnick avatar Apr 16 '25 15:04 mscolnick