Update text value when changing an Observable in GLMakie

I’m trying to update a text value to reflect a change in one or more Observable. I’ve tried to do this a couple of ways.

If the Observable is

obs = Observable(1.0)

I’ve tried putting it in a Label

Label(fig, "$(obs)")

using a few different ways of getting the value. If you use to_value(obs), this eliminates the Observable reference, I think, so the value will not update again. I’ve also tried to lift() the value.

I’ve also tried to put it directly on the plot using text!(). There was a time when I got that to work several months ago, but I can’t remember what I did and Makie has changed a lot since then.

I know that Slider has a built-in label, but having text that reflects Observable changes is more powerful. In the example below, there are two sine wave, and their parameters can change with sliders. I want to calculate the difference in the frequencies of the two sine waves and display it.

Is there a way to do this, or have I just not found it yet?

using GLMakie
GLMakie.activate!()

f = Figure()

slider1 = Slider(f[1,1], range=0.0:0.1:1.0)
slider2 = Slider(f[2,1], range=0.0:0.1:1.0)

label = lift(slider1.value, slider2.value) do s1, s2
    return "Difference = $(s1 - s2)"
end
Label(f[3,1], label)

display(f)
1 Like

Or going off of this syntax (which doesn’t work because you’re trying to interpolate an Observable into a string) you can do Label(fig, @lift "$($obs)"). The first $ is the normal string interpolation syntax, the second $ means “use an observable’s value here”.

1 Like

Thank you both @fatteneder and @jules.

This works if you add a couple more parentheses: Label(fig, @lift("$($obs)")).

By the way, I’m working on some examples of interactivity with GLMakie. There are only a couple right now, but I’ll make a separate post when I have enough that I think they will be more broadly useful.

3 Likes