JuliaPluto / PlutoUI.jl

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

`Vector` of `Pair`s for `PlutoUI.Select` not parsed properly? #118

Closed kapple19 closed 3 years ago

kapple19 commented 3 years ago

Or am I doing something wrong?

The options input should be of the form ["Display Value" => assigned_value] with assigned_value isa Any, right?

Select

disberd commented 3 years ago

If you see from the documentation open on the helpbox it does mention that what is returned from the @bind is the key and not the value of the pair. I do agree with you though that it does seem a bit counterintuitive as behavior

pankgeorg commented 3 years ago

Exactly. Note that the type of the argument is key::String => value::Any, which means that it's disallowed to have non-string keys. You can easily find the value you want by doing

options = ["1" => "one", "2" => "two"]
@bind selectedkey Select(options, default=options[1][1])

--- # Another cell
value = options[ findfirst(x -> first(x) === selectedkey, options)][2]
kapple19 commented 3 years ago

Thanks guys. Yes, I did notice the help doc, and I was wondering if it was just a mistaken decision, or optimal given the context of a notebook.

Is it designed this way because in Pluto notebooks, you cannot assign, say, a custom type to the @binded variable?

Also, what is the purpose of having each second in the input Pair be type Any? It just converts it to String anyway. It would definitely make more sense for even an integer to be assigned to the bound variable. It's not even possible to do something like, say,

@bind x parse(Int64, Select(["1" => "one", "5" => "five"]))

because of course Select doesn't output the integer value selected.

I'd be happy to close this issue seeing as this design seems intentional, but just wanted to check and ask if it's possible to design it the more intuitive way.

yha commented 3 years ago

FWIW, I use the following pattern with basically all Select-like widgets:

x_options = OrderedDict(["str1" => object1, "str2" => object2, ...])
x_sel = @bind x_str Select(collect(keys(x_options)))
x = x_options[x_str]

It would be nice if there was an API that achieved this directly in the @bind line.

fonsp commented 3 years ago

See #3 , which will be fixed by #148