Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector of Pairs for PlutoUI.Select not parsed properly? #118

Closed
kapple19 opened this issue Jul 15, 2021 · 5 comments · Fixed by #148
Closed

Vector of Pairs for PlutoUI.Select not parsed properly? #118

kapple19 opened this issue Jul 15, 2021 · 5 comments · Fixed by #148

Comments

@kapple19
Copy link

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
Copy link
Member

disberd commented Jul 16, 2021

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
Copy link
Member

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
Copy link
Author

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
Copy link
Contributor

yha commented Oct 31, 2021

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 fonsp closed this as completed Nov 2, 2021
@fonsp fonsp linked a pull request Nov 2, 2021 that will close this issue
@fonsp
Copy link
Member

fonsp commented Nov 2, 2021

See #3 , which will be fixed by #148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants