JuliaGizmos / Interact.jl

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

How Do I Pre-Check Checkboxes? #386

Closed TheCedarPrince closed 3 years ago

TheCedarPrince commented 3 years ago

Hey there, I tried asking for help on the Julia Slack, Zulip, and Discourse, but no luck anywhere. Here is the question I had:

How do I use Interact.jl to present a list of checkboxes with some of the boxes already checked off? Tried following the documentation and couldn’t figure out how to set specific checkboxes to true (i.e. checked off).

What I have currently, is this:

image

The boxes are empty by default. What I would like to have happen is for a few boxes to already be checked off when they are first shown like this:

image

The code I have been using is:

options = ["a", "b", "c"]
wdg = checkboxes(options)
wdg[:a] = true
wdg[:c] = true
wdg

What I don't understand is why the checkbox for "a" and "c" is not already showing up checked off. Any thoughts?

Thanks!

timholy commented 3 years ago

If you don't get an answer here, consider using https://docs.julialang.org/en/v1.7-dev/stdlib/REPL/#MultiSelectMenu and https://github.com/JuliaLang/julia/blob/b0fc9bacb9244959a7c1bd34aa0897828602d220/stdlib/REPL/src/TerminalMenus/MultiSelectMenu.jl#L39-L55

TheCedarPrince commented 3 years ago

I would love to use something like that, but I am trying to create an interface in a Jupyter notebook environment. So, sadly, that wouldn't work for my particular use case as there aren't supported in Jupyter NBs at the moment. Thanks though @timholy !

piever commented 3 years ago

You can use the keyword value to set the value:

options = ["a", "b", "c"]
value = ["a", "c"]
checkboxes(options, value = value)

(both options and value can be Observables).

It is documented here, but it looks like the default value is wrong, I think it initializes with value = [].

TheCedarPrince commented 3 years ago

That did it! Thanks @piever!

Where is the error? I can make a quick docs PR to fix it.

Also, could I add this as an example for pre-checking checkboxes as you showed here? That wasn't apparent - at least to me - from the docs.

piever commented 3 years ago

Where is the error? I can make a quick docs PR to fix it.

In the docs, it says that if you don't pass a value explicitly, it defaults to first(values(options)), but actually the default is valtype(options)[]. Docs PR with this fix and a "pre-checked" example would be great!

TheCedarPrince commented 3 years ago

Sweet! @piever - could I ping you when the PR is ready? Thanks!

piever commented 3 years ago

Sure! I'll be notified either way as I'm watching the repo.

TheCedarPrince commented 3 years ago

PR opened here: piever/InteractBase.jl#159 @piever - thanks for the help! :smile: