To be able to edit code and run cells, you need to run the notebook yourself. Where would you like to run the notebook?

This notebook takes about 20 seconds to run.

In the cloud (experimental)

Binder is a free, open source service that runs scientific notebooks in the cloud! It will take a while, usually 2-7 minutes to get a session.

On your computer

(Recommended if you want to store your changes.)

  1. Copy the notebook URL:
  2. Run Pluto

    (Also see: How to install Julia and Pluto)

  3. Paste URL in the Open box

Frontmatter

If you are publishing this notebook on the web, you can set the parameters below to provide HTML metadata. This is useful for search engines and social media.

Author 1
import AbstractPlutoDingetjes.Bonds
9.0 ms
using HypertextLiteral
25.6 ms
using Random: randstring
140 μs
Radio
begin
struct Radio
options::Vector{Pair{<:AbstractString,<:Any}}
default::Union{Nothing, AbstractString}
orientation::Symbol
end
Radio(options::AbstractVector{<:AbstractString}; default=nothing, orientation=:vertical) = Radio([o => o for o in options], default, orientation)
Radio(options::AbstractVector{<:Pair{<:AbstractString,<:Any}}; default=nothing, orientation=:vertical) = Radio(options, default, orientation)
end
6.2 ms
function Base.show(io::IO, m::MIME"text/html", radio::Radio)
groupname = randstring('a':'z')
h = @htl(
"""<form style="$(radio.orientation == :horizontal ? "display: flex" : nothing)">$(
map(radio.options) do o
@htl(
"""<div><input $((
type="radio",
id=(groupname * o.first),
name=groupname,
value=o.first,
checked=(radio.default === o.first),
))><label for=$(groupname * o.first)>$(
o.second
)</label></div>"""
)
end
)<script>
const form = currentScript.parentElement
const groupname = $(groupname)
const selected_radio = form.querySelector('input[checked]')
let val = selected_radio?.value
Object.defineProperty(form, "value", {
get: () => val,
set: (newval) => {
val = newval
const i = document.getElementById(groupname + newval)
if(i != null){
i.checked = true
}
},
})
form.oninput = (e) => {
val = e.target.value
// and bubble upwards
}
</script></form>""")
show(io, m, h)
end
525 ms
Base.get(radio::Radio) = radio.default
361 μs
Bonds.initial_value(select::Radio) = select.default
314 μs
Bonds.possible_values(select::Radio) =
first.(select.options)
384 μs
function Bonds.validate_value(select::Radio, val)
val ∈ (first(p) for p in select.options)
end
749 μs
Radio(["a", "b", "c"], default="b")
16.9 μs
Radio(["a", "b", "c"], orientation=:vertical, default="b")
19.2 μs
Radio(["a", "b", "c"], orientation=:horizontal, default="b")
19.5 μs