The following is a run through of the widgets you can create with Interact. All widgets allow for the following keyword arguments:
label
: Label to be shown next to the widgetvalue
: The value the widget should be set to when createdsignal
: A signal object of type Reactive.Input
which gets the value of the widget as user enters input.Many of the widgets have keyword arguments specific to them. See below for more.
In [1]:
using Interact
using Reactive
Sliders are arguably the most useful of the widgets. A slider can be created with the slider{T <: Number}(range::Range{T})
function. The value of the slider defaults to the median of the range, and can be set using the value::T
keyword argument. The type of signal a slider depends on the type of the range. E.g. A floating point range like 0:π/4:2π
gives a signal of floating point values, while a range like 1:10
gives a signal of integers.
In [2]:
float_slider = slider(0:π/4:2π)
Out[2]:
In [3]:
int_slider = slider(1:10)
Out[3]:
In [4]:
signal(int_slider)
Out[4]:
checkbox
takes an optional first argument which defaults to false
and creates a checkbox.
In [5]:
display(checkbox())
checkbox(true)
Out[5]:
You can create a toggle button with togglebutton
which takes as an optional argument its label.
In [6]:
status = togglebutton("Mary called", value=true)
Out[6]:
In [7]:
lift(s -> s ? "Mary called" : "Mary didn't call", status)
Out[7]:
A button gives out a signal of a constant signal which is nothing
by default. You can set this using the value
keyword argument. The signal updates when the button is clicked.
In [8]:
b = button("Click Me")
Out[8]:
Here is how you can count the number of clicks made on a button using foldl
on the signal:
In [9]:
foldl((acc, value) -> acc + 1, 0, signal(b))
Out[9]:
There are 3 options widgets: dropdown
, togglebuttons
, radiobuttons
. There are two types allowed as an argument while invoking these:
AbstractArray
(e.g. Vector
, Tuple
)Associative
(e.g. Dict
, OrderedDict
)
The default value is the first element (or undefined in case of an undordered Associative
like Dict
), but this can be set using the value
keyword argument.
In [10]:
a = dropdown(["one", "two", "three"])
Out[10]:
In [11]:
signal(a)
Out[11]:
In [12]:
f = radiobuttons(["Add" => +, "Sub" => -, "Exp" => ^])
Out[12]:
In [13]:
@lift f(e, π*im)
Out[13]:
Notice that the order "Add", "Sub", "Exp" was not retained in the above example, because a Dict
does not save the ordering. To overcome this, we can use OrderedDict
from DataStructures.jl package.
In [14]:
using DataStructures
f_ = togglebuttons([("Add", +), ("Sub", -), ("Exp", ^)])
Out[14]:
In [15]:
@lift f_(e, π*im)
Out[15]:
A textbox can be of a Number
or String
type. textbox
takes one argument: its default value.
In [16]:
string_box = textbox("Change me")
Out[16]:
In [17]:
signal(string_box)
Out[17]:
A textbox can be of a Number
type as well. Just set a default number value, or use textbox(typ=T)
where T is a Number
type.
In [18]:
int_box = textbox(0)
Out[18]:
In [19]:
signal(int_box)
Out[19]:
If creating a number typed textbox, you can also pass along an optional range
field to set a bound on the possible values one can input. If an entered value exceeds the range, it is replaced by its nearest bounding number.
In [20]:
bounded_float_box = textbox(2pi, range=-10.0:10)
Out[20]:
In [21]:
signal(bounded_float_box)
Out[21]:
textarea
takes an optional default value and creates a textarea. Its signal changes when you type.
In [22]:
tex = textarea("Your very own \$\\LaTeX\$ editor")
Out[22]:
In [23]:
@lift latex(tex)
Out[23]:
widget
tries to coerce a value into a widget.
In [24]:
map(display, [
widget(1:10), # Slider
widget(false), # Checkbox
widget("text"), # Textbox
widget(1.1), # Number Textbox
widget([:on, :off]), # Toggle Buttons
widget([:π => float(π), :τ => 2π])
]);