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.31k stars 155 forks source link

Button click is not updating cell (but is updating others #1680

Closed peterwilli closed 4 days ago

peterwilli commented 5 days ago

Describe the bug

I just found Marimo and it looks amazing, however, I seem to have a strange bug when using buttons.

g, s = mo.state(True)
def k(a):
    s(False)
button = mo.ui.button(label="test", on_click=k) if g() else None
button

I try to hide a button when it is clicked. However, when I click test, it does not dissapear. If I put button in another cell, it does magically work. Is this normal? If so, how can I hide a button on click?

Edit: It seems alternates also do not work:

g, s = mo.state(True)
def k(a):
    s(False)
button = mo.ui.button(label="test", on_click=k, disabled=not g())
button

Yields the same behaviour

Edit 3: According to https://docs.marimo.io/guides/state.html#example-todo-list

Notice that we created the slider and number elements in different cells. When tying elements, this is necessary, because calling a setter in a cell queues all other cells reading the state to run, not including the one that just called the setter.

So apparently I have to cycle between 2 cells. I keep this issue open regardless in case someone has a cleaner solution.

Environment

{
  "marimo": "0.6.23",
  "OS": "Linux",
  "OS Version": "6.9.6-arch1-1",
  "Processor": "",
  "Python Version": "3.10.14",
  "Binaries": {
    "Browser": "--",
    "Node": "--"
  },
  "Requirements": {
    "click": "8.1.7",
    "importlib-resources": "missing",
    "jedi": "0.19.1",
    "markdown": "3.6",
    "pymdown-extensions": "10.8.1",
    "pygments": "2.18.0",
    "tomlkit": "0.12.5",
    "uvicorn": "0.30.1",
    "starlette": "0.37.2",
    "websockets": "12.0",
    "typing-extensions": "4.11.0",
    "black": "24.4.2"
  }
}

Code to reproduce

import marimo as mo
g, s = mo.state(True)
def k(a):
    s(False)
button = mo.ui.button(label="test", on_click=k) if g() else None
button
peterwilli commented 4 days ago

Closing this, after reading the todo example I really understood how it works. UI elements each go in their separate cells, and come together via arrays or stacks. For those new here facing the same issue, I recommend reading this example: https://docs.marimo.io/guides/state.html#example-todo-list