##// END OF EJS Templates
Updating interact to work with latest state of widgets.
Brian E. Granger -
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -13,8 +13,8 b''
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 from IPython.html.widgets import (Widget, TextBoxWidget,
17 FloatSliderWidget, IntSliderWidget, CheckBoxWidget, DropdownWidget,
16 from IPython.html.widgets import (Widget, TextWidget,
17 FloatSliderWidget, IntSliderWidget, CheckboxWidget, DropdownWidget,
18 18 ContainerWidget)
19 19 from IPython.display import display, clear_output
20 20 from IPython.utils.py3compat import string_types, unicode_type
@@ -31,45 +31,62 b' def _matches(o, pattern):'
31 31 return all(isinstance(obj,kind) for obj,kind in comps)
32 32
33 33
34 def _min_max_value(o):
35 min = o[0]
36 max = o[1]
37 if not max > min:
38 raise ValueError('max must be greater than min: (min={0}, max={1})'.format(min, max))
39 value = min + abs(o[0]-o[1])/2
34 def _get_min_max_value(min, max, value):
35 """Return min, max, value given input values with possible None."""
36 if value is None:
37 if not max > min:
38 raise ValueError('max must be greater than min: (min={0}, max={1})'.format(min, max))
39 value = min + abs(min-max)/2
40 value = type(min)(value)
41 elif min is None and max is None:
42 if value == 0.0:
43 min, max, value = 0.0, 1.0, 0.5
44 elif value == 0:
45 min, max, value = 0, 1, 0
46 elif isinstance(value, float):
47 min, max = -value, 3.0*value
48 elif isinstance(value, int):
49 min, max = -value, 3*value
50 else:
51 raise TypeError('expected a number, got: %r' % number)
52 else:
53 raise ValueError('unable to infer range, value from: ({0}, {1}, {2})'.format(min, max, value))
40 54 return min, max, value
41 55
56
42 57 def _widget_abbrev(o):
43 58 if isinstance(o, string_types):
44 return TextBoxWidget(value=unicode_type(o))
59 return TextWidget(value=unicode_type(o))
45 60 elif isinstance(o, dict):
46 values = [unicode_type(k) for k in o]
47 w = DropdownWidget(value=values[0], values=values)
48 w.actual_values = o
61 labels = [unicode_type(k) for k in o]
62 values = o.values()
63 w = DropdownWidget(value=values[0], values=values, labels=labels)
49 64 return w
50 65 # Special case float and int == 0.0
51 66 # get_range(value):
52 67 elif isinstance(o, bool):
53 return CheckBoxWidget(value=o)
68 return CheckboxWidget(value=o)
54 69 elif isinstance(o, float):
55 return FloatSliderWidget(value=o, min=-o, max=3.0*o)
70 min, max, value = _get_min_max_value(None, None, o)
71 return FloatSliderWidget(value=o, min=min, max=max)
56 72 elif isinstance(o, int):
57 return IntSliderWidget(value=o, min=-o, max=3*o)
73 min, max, value = _get_min_max_value(None, None, o)
74 return IntSliderWidget(value=o, min=min, max=max)
58 75 if isinstance(o, (list, tuple)):
59 76 if _matches(o, (int, int)):
60 min, max, value = _min_max_value(o)
61 return IntSliderWidget(value=int(value), min=min, max=max)
77 min, max, value = _get_min_max_value(o[0], o[1], None)
78 return IntSliderWidget(value=value, min=min, max=max)
62 79 elif _matches(o, (int, int, int)):
63 min, max, value = _min_max_value(o)
64 return IntSliderWidget(value=int(value), min=min, max=max, step=o[2])
80 min, max, value = _get_min_max_value(o[0], o[1], None)
81 return IntSliderWidget(value=value, min=min, max=max, step=o[2])
65 82 elif _matches(o, (float, float)):
66 min, max, value = _min_max_value(o)
83 min, max, value = _get_min_max_value(o[0], o[1], None)
67 84 return FloatSliderWidget(value=value, min=min, max=max)
68 85 elif _matches(o, (float, float, float)):
69 min, max, value = _min_max_value(o)
86 min, max, value = _get_min_max_value(o[0], o[1], None)
70 87 return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
71 88 elif _matches(o, (float, float, int)):
72 min, max, value = _min_max_value(o)
89 min, max, value = _get_min_max_value(o[0], o[1], None)
73 90 return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
74 91 elif all(isinstance(x, string_types) for x in o):
75 92 return DropdownWidget(value=unicode_type(o[0]),
@@ -84,7 +101,7 b' def interactive(f, **kwargs):'
84 101 widgets = []
85 102 container = ContainerWidget()
86 103 container.result = None
87 container.arguments = dict()
104 container.kwargs = dict()
88 105 for key, value in kwargs.items():
89 106 if isinstance(value, Widget):
90 107 widget = value
@@ -92,6 +109,7 b' def interactive(f, **kwargs):'
92 109 widget = _widget_abbrev(value)
93 110 if widget is None:
94 111 raise ValueError("Object cannot be transformed to a Widget")
112 widget.description = key
95 113 widgets.append((key,widget))
96 114 widgets.sort(key=lambda e: e[1].__class__.__name__)
97 115 container.children = [e[1] for e in widgets]
@@ -101,9 +119,7 b' def interactive(f, **kwargs):'
101 119 actual_kwargs = {}
102 120 for key, widget in widgets:
103 121 value = widget.value
104 if hasattr(widget, 'actual_values'):
105 value = widget.actual_values[value]
106 container.arguments[key] = value
122 container.kwargs[key] = value
107 123 actual_kwargs[key] = value
108 124 if co:
109 125 clear_output(wait=True)
@@ -112,9 +128,8 b' def interactive(f, **kwargs):'
112 128 # Wire up the widgets
113 129 for key, widget in widgets:
114 130 widget.on_trait_change(call_f, 'value')
115 widget.description = key
116 131
117 container.on_displayed(lambda : call_f(None, None, None))
132 container.on_displayed(lambda _: call_f(None, None, None))
118 133
119 134 return container
120 135
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now