Widgets

One of the best new features of the IPython Notebook is its rich widget system. hy_kernel can directly use all of the IPython widgets.

NOTE: the IPython semantics of displaying a widget by having it be the last line of a cell doesn't work. Looking into this! In the meantime, use IPython.display.display!


In [1]:
(import [IPython.html [widgets]]
        [IPython.display [display]]
        [IPython.utils [traitlets]])

The simplest, albeit pointless, invocation.


In [2]:
(widgets.IntText)

Naming a widget.


In [3]:
(setv x (widgets.IntText))
(display x)

The binding is bidrectional.


In [4]:
(setv x.value 1)


Out[4]:
1

Using widgets.interact, a simple GUI generator for functions.


In [5]:
(apply widgets.interact
 [(fn [x y] (print (* x y)))]
 {"x" 1 "y" 1})


1
Out[5]:
<function __main__._hy_anon_fn_1>

Two linked widgets.


In [6]:
(setv x (widgets.IntSlider))
(setv y (widgets.IntSlider))
(traitlets.link (, x "value") (, y "value"))
(display x y)

A button.


In [7]:
(setv btn (apply widgets.Button [] {"description" "Click me"}))
(btn.on-click (fn [btn] (setv x.value (+ x.value 10))))
(display btn)