Get help from the marimo community

Updated 4 weeks ago

Interrupt execution when changing UI element

At a glance
With a slider, I'm setting a parameter for a long-running function. I'd like to stop and restart the execution of that function when I change the slider position (without having to press the cell interrupt button before). Is this possible?
H
M
M
16 comments
Do you have on-cell-change option at the bottom panel set to autorun?
This should be happening automatically πŸ€”
Yes, I do have the autorun enabled, but that is not the issue. I might not have been clear, so let me share an example:
Plain Text
import marimo

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


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


@app.cell
def _(mo):
    value = mo.ui.slider(0, 10)
    value
    return (value,)


@app.cell
async def _(sleep, value):
    for i in range(10):
        print(value.value, end=" ")
        await sleep(0.5)
    return (i,)


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


This prints the value of the slider 10 times. If I move the slider, it will re-run printing the new value after it finished printing 10 times the old value.
I would like that the execution of the for-loop stops when I move the slider, and it restarts with the new value of the slider.
Ah ok, you might find mo.md useful in this case?
Something along the lines of:

Plain Text
import marimo
__generated_with = "0.10.14"
app = marimo.App(width="medium")

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

@app.cell
def _(mo):
    value = mo.ui.slider(0, 10)
    value
    return (value,)

@app.cell
async def _(sleep, value):
    current = value.value
    
    for i in range(10):
        # Stop if slider value has changed from when we started
        mo.stop(current != value.value)
        
        print(current, end=" ")
        await sleep(0.5)
    return (i,)

if __name__ == "__main__":
    app.run()
Apologies if I'm still not understanding your query.
On clicking on the exact slider position, I don't see the old value being repeated anymore (however if you draft it along, that's a little different).
I tried using mo.stop, but it does not work. It seems that value.value does not change until after the loop finishes executing
What I want: Set slider to 0 -> starts printing 0 0 0.... When it reaches 5 zeros (0 0 0 0 0), change the slider to 1. It clears the 0 0 0 0 0 and starts printing 1 1 1...
What actually happens: ... When it reaches 5 zeros (0 0 0 0 0), change the slider to 1. It keeps on printing until it reaches 10 zeros, then it clears the output and reexecutes printing ones.
The mo.stop might not work because the python side of the slider does not change it's value until the current execution of a cell ends?
Sorry, not sure about this; (whether you can interrupt an ongoing UI element's rendering πŸ€”)
@Mauro Silberberg it’s not possible today. Maybe there is an API we could expose. This does put the program at risk of getting in a weird state by allowing an end-user to stop the execution mid-state. But nothing that is not manageable as the author.

Feel free to file a feature request on GitHub and we can look to prioritize it.
Add a reply
Sign up and join the conversation on Discord