JuliaPluto / PlutoUI.jl

https://featured.plutojl.org/basic/plutoui.jl
The Unlicense
299 stars 54 forks source link

Storing several Slider(s) to variables lead to very bad reactiveness #300

Closed scls19fr closed 4 months ago

scls19fr commented 4 months ago

Hello,

Following issue #299 I changed my Pluto.jl notebook to store sliders into 4 variables.

See https://gist.github.com/scls19fr/1486d2732db61d81d4b0021ca5e79481

v2 is much less reactive and nearly unusable

With v2 you always need to release left button each time you are changing value.

A fix about this will be great because the v2 code writing is much more convenient (less cells)

Kind regards

fonsp commented 4 months ago

Can you record a video that demonstrates the problem?

fonsp commented 4 months ago

Ah I see, you case is:

bond = @bind x Slider(1:10)
md"""
Bond: $bond

Value: $x
"""

This is not supported. It's not possible to place the bond (the result of @bind x Slider(1:10)) and the bound value (x) in the same cell.

This is because moving the slider changes x, which causes cells that reference x to re-run. Re-running the md""" cell will show the new value of x, but also re-render the bond, so a new slider is created. The new slider does not have mouse focus so you can't continue sliding.

A workaround for displaying a bond and a bound value in the same cell is to use one of Pluto's built-in combined displays. The main system is in PlutoUI.ExperimentalLayout (hbox, grid, etc). You can also use a DataFrame and put the bond and bound variable in different cells.

scls19fr commented 4 months ago

I posted for comparison a Marimo notebook with exactly same features

https://gist.github.com/scls19fr/1486d2732db61d81d4b0021ca5e79481#file-am_modulation-py

but that's life ... if I want to use Pluto.jl... I know now that I need to have more cells.

ederag commented 2 months ago

Late to the party (just saw this issue in the release notes), but the show_value = true argument might be of interest here, when the slider should be in the same cell as its value. Screenshot_20240423_142918 Of course the centering is a bit off, but that would be another issue.

fonsp commented 2 months ago

And like I mentioned, you can use ExperimentalLayout to but bonds and values together. Here is a silly example:

https://github.com/JuliaPluto/PlutoUI.jl/assets/6933510/e2d07618-a4d7-4e9e-bcfd-7cbde0e47d24

# ╔═╡ b4c48706-021f-11ef-1413-059aae52cfc4
using PlutoUI

# ╔═╡ d3d667e4-4e98-44d0-b1f0-cde287afe969
bond = @bind x Slider(1:100);

# ╔═╡ aed4e404-2ca3-4db2-9ac5-d9fd65dbdd98
PlutoUI.ExperimentalLayout.grid([
    bond Text("x² = $(x*x)")
    x    bond
])