Updating interact to new APIs.

This commit is contained in:
Brian E. Granger 2014-01-22 12:12:58 -08:00 committed by MinRK
parent 0cf8002ebf
commit faebb43e01
2 changed files with 149 additions and 22 deletions

View File

@ -13,8 +13,8 @@
# Imports
#-----------------------------------------------------------------------------
from IPython.html.widgets import (Widget, StringWidget,
FloatRangeWidget, IntRangeWidget, BoolWidget, SelectionWidget,
from IPython.html.widgets import (Widget, TextBoxWidget,
FloatSliderWidget, IntSliderWidget, CheckBoxWidget, DropdownWidget,
ContainerWidget)
from IPython.display import display, clear_output
from IPython.utils.py3compat import string_types, unicode_type
@ -41,38 +41,38 @@ def _min_max_value(o):
def _widget_abbrev(o):
if isinstance(o, string_types):
return StringWidget(value=unicode_type(o))
return TextBoxWidget(value=unicode_type(o))
elif isinstance(o, dict):
values = [unicode_type(k) for k in o]
w = SelectionWidget(value=values[0], values=values)
w = DropdownWidget(value=values[0], values=values)
w.actual_values = o
return w
# Special case float and int == 0.0
# get_range(value):
elif isinstance(o, bool):
return BoolWidget(value=o)
return CheckBoxWidget(value=o)
elif isinstance(o, float):
return FloatRangeWidget(value=o, min=-o, max=3.0*o)
return FloatSliderWidget(value=o, min=-o, max=3.0*o)
elif isinstance(o, int):
return IntRangeWidget(value=o, min=-o, max=3*o)
return IntSliderWidget(value=o, min=-o, max=3*o)
if isinstance(o, (list, tuple)):
if _matches(o, (int, int)):
min, max, value = _min_max_value(o)
return IntRangeWidget(value=int(value), min=min, max=max)
return IntSliderWidget(value=int(value), min=min, max=max)
elif _matches(o, (int, int, int)):
min, max, value = _min_max_value(o)
return IntRangeWidget(value=int(value), min=min, max=max, step=o[2])
return IntSliderWidget(value=int(value), min=min, max=max, step=o[2])
elif _matches(o, (float, float)):
min, max, value = _min_max_value(o)
return FloatRangeWidget(value=value, min=min, max=max)
return FloatSliderWidget(value=value, min=min, max=max)
elif _matches(o, (float, float, float)):
min, max, value = _min_max_value(o)
return FloatRangeWidget(value=value, min=min, max=max, step=o[2])
return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
elif _matches(o, (float, float, int)):
min, max, value = _min_max_value(o)
return FloatRangeWidget(value=value, min=min, max=max, step=float(o[2]))
return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
elif all(isinstance(x, string_types) for x in o):
return SelectionWidget(value=unicode_type(o[0]),
return DropdownWidget(value=unicode_type(o[0]),
values=[unicode_type(k) for k in o])
@ -93,9 +93,9 @@ def interactive(f, **kwargs):
if widget is None:
raise ValueError("Object cannot be transformed to a Widget")
widgets.append((key,widget))
widget.parent = container
widgets.sort(key=lambda e: e[1].__class__.__name__)
container.children = [e[1] for e in widgets]
# Build the callback
def call_f(name, old, new):
actual_kwargs = {}

View File

@ -30,7 +30,16 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
@ -42,7 +51,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
@ -72,7 +82,8 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
@ -82,7 +93,23 @@
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [
{
"html": [
"<h3>Arguments:</h3><table>\n",
"<tr><td>a</td><td>10</td></tr>\n",
"<tr><td>c</td><td>True</td></tr>\n",
"<tr><td>b</td><td>Hi There</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x108142490>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
@ -98,15 +125,115 @@
"interact(show_args,\n",
" Temp=(0,10),\n",
" Current=(0.,10.,0.01),\n",
" z=(True,False),\n",
" z=True,\n",
" Text=u'Type here!',\n",
" Algorithm=['This','That','Other'],\n",
" a=widgets.FloatRangeWidget(min=-10.0, max=10.0, step=0.1, value=5.0)\n",
" a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0)\n",
" )"
],
"language": "python",
"metadata": {},
"outputs": []
"outputs": [
{
"html": [
"<h3>Arguments:</h3><table>\n",
"<tr><td>a</td><td>5.0</td></tr>\n",
"<tr><td>Algorithm</td><td>This</td></tr>\n",
"<tr><td>Temp</td><td>5</td></tr>\n",
"<tr><td>Text</td><td>Type here!</td></tr>\n",
"<tr><td>Current</td><td>5.0</td></tr>\n",
"<tr><td>z</td><td>True</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x10814e610>"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%debug"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"> \u001b[0;32m/Users/bgranger/Documents/Computing/IPython/code/ipython/IPython/html/widgets/interact.py\u001b[0m(38)\u001b[0;36m_min_max_value\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32m 37 \u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mmax\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mmin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0m\u001b[0;32m---> 38 \u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'max must be greater than min: (min={0}, max={1})'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0m\u001b[0;32m 39 \u001b[0;31m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmin\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"ipdb> print o\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(True, False)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"ipdb> print min, max\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True False\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"ipdb> q\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"isinstance(10, bool)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"False"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",