JuliaGizmos / Interact.jl

Interactive widgets to play with your Julia code
Other
522 stars 77 forks source link

@map Syntax to twice dereference observable? #379

Open jacobadenbaum opened 3 years ago

jacobadenbaum commented 3 years ago

Suppose I have the following example.

opt1 = widget(("Option 1", "Option 2"), label = "First Layer")
opt2 = Interact.@map begin
    if &opt1 == "Option 1"
        return dropdown(["A", "B", "C"], label = "Second Layer")
    else
        return dropdown(["A", "C"], label = "Second Layer")
    end
end

Now, I want to use the value of the dropdown menu in opt2 to do something (like, generate a plot). But when I run

val = Interact.@map do_work(&opt2)

I get the ui element that correspond to the dropdown menu, instead of it's value. I would have thought that the natural way to do this would be:

val = Interact.@map do_work(&&opt2)

but this syntax is not supported and returns an error. Is it possible to support something like this? I came upon this when I was trying to dynamically generate a UI for several different types of plots, where there are specific options that are only available for certain types of plots.

For what it's worth, I'm currently, I'm getting around this by doing something like the following:

opt2_val = Observable{Any}()
opt2 = Interact.@map begin
    if &opt1 == "Option 1"
        s =  dropdown(["A", "B", "C"], label = "Second Layer")
    else
        s = dropdown(["A", "C"], label = "Second Layer")
    end
    opt2_val[] = s[]
    map!(identity, opt2_val, s)
end
val = Interact.@map do_work(&opt2_val)

This seems to work, but it's awkward to do and I'm worried that it's not best practices.