marimo icon indicating copy to clipboard operation
marimo copied to clipboard

Unnecessary cell re-runs on query param change

Open andrewhill157 opened this issue 10 months ago • 4 comments

Describe the bug

In example below, if you use set to set the value of a particular query param, other cells that access the value of a different query param seem to re-run. This is just a toy example, but in my case this behavior leads to a number of things that will not change at all being re-rendered.

In the example below just push the button and the cell with the sleep(10) that is displaying unrelated text will rerun presumably because query params changed but none of the specific params accessed by this cell have actually changed.

Environment

Using marimo 0.4.0

Code to reproduce

image

import marimo as mo
from random import randint
import time

params = mo.query_params()
time.sleep(10)
text = mo.ui.text(
    label="Enter something to display 🎉",
    value=params["text"] or "",
    full_width=True,
)
text
mo.md(text.value)
button = mo.ui.button(label="Click me to generate a new number")
button
button
my_number = params["number"] or randint(0, 1000)
params.set("number", my_number)
mo.md(f"Your number is {my_number}")

andrewhill157 avatar Apr 18 '24 21:04 andrewhill157

This makes sense in the queryparams' current form. We can add a concept of lens to be able to scope down the scope of the reactiveness: e.g get_text, set_text = query_params.lens("text") which gives you a more granular piece of state.

(you might be able to do this today with mo.state() as well)

mscolnick avatar Apr 18 '24 22:04 mscolnick

Yeah totally, is clear in the docs that this is should currently work (sorry bug probably the wrong label here!). Something like that would definitely be helpful at least in my case. I can take a closer look at mo.state and see if I could make something work with that, my quick initial attempts after looking at the docs weren't successful but also my first time looking at it. Was trying to use getter, setter = mo.state(mo.query_params()) and then interacting with it that way, which may not make sense.

andrewhill157 avatar Apr 19 '24 01:04 andrewhill157

mo.state(mo.query_params()) may be equivalent to just mo.query_params() - we can look if lensing is something we can support soon

mscolnick avatar Apr 19 '24 01:04 mscolnick

Great, thanks for considering!

andrewhill157 avatar Apr 22 '24 16:04 andrewhill157