fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia

Home Page:https://plutojl.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@bind for Observables

aplavin opened this issue · comments

Observables.jl is a widely used package defining, well Observable(). It would be useful to bind Pluto UI elements to Observables, so that supporting libraries (such as Makie) update values efficiently.

I tried to make a macro @bindobs that works similar to @bind but defines an observable. I didn't manage to implement it properly, so would be happy to know if I missed something, or is such a thing not supported yet.
What I achieved is a "two-part" macro:

macro bindobs(def::Symbol, element)
	defraw = Symbol(def, :____raw)
	quote
		elt = $element
		$(esc(def)) = Observable(get(elt))
		@bind $defraw elt
	end
end

macro bindobs_(def::Symbol)
	defraw = Symbol(def, :____raw)
	:($(esc(def))[] = $(esc(defraw)))
end

then, in one cell do @bindobs myval Slider(...) and in another cell @bindobs_ myval.

Can this be done in a single cell?