##// 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 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from IPython.html.widgets import (Widget, TextBoxWidget,
16 from IPython.html.widgets import (Widget, TextWidget,
17 FloatSliderWidget, IntSliderWidget, CheckBoxWidget, DropdownWidget,
17 FloatSliderWidget, IntSliderWidget, CheckboxWidget, DropdownWidget,
18 ContainerWidget)
18 ContainerWidget)
19 from IPython.display import display, clear_output
19 from IPython.display import display, clear_output
20 from IPython.utils.py3compat import string_types, unicode_type
20 from IPython.utils.py3compat import string_types, unicode_type
@@ -31,45 +31,62 b' def _matches(o, pattern):'
31 return all(isinstance(obj,kind) for obj,kind in comps)
31 return all(isinstance(obj,kind) for obj,kind in comps)
32
32
33
33
34 def _min_max_value(o):
34 def _get_min_max_value(min, max, value):
35 min = o[0]
35 """Return min, max, value given input values with possible None."""
36 max = o[1]
36 if value is None:
37 if not max > min:
37 if not max > min:
38 raise ValueError('max must be greater than min: (min={0}, max={1})'.format(min, max))
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
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 return min, max, value
54 return min, max, value
41
55
56
42 def _widget_abbrev(o):
57 def _widget_abbrev(o):
43 if isinstance(o, string_types):
58 if isinstance(o, string_types):
44 return TextBoxWidget(value=unicode_type(o))
59 return TextWidget(value=unicode_type(o))
45 elif isinstance(o, dict):
60 elif isinstance(o, dict):
46 values = [unicode_type(k) for k in o]
61 labels = [unicode_type(k) for k in o]
47 w = DropdownWidget(value=values[0], values=values)
62 values = o.values()
48 w.actual_values = o
63 w = DropdownWidget(value=values[0], values=values, labels=labels)
49 return w
64 return w
50 # Special case float and int == 0.0
65 # Special case float and int == 0.0
51 # get_range(value):
66 # get_range(value):
52 elif isinstance(o, bool):
67 elif isinstance(o, bool):
53 return CheckBoxWidget(value=o)
68 return CheckboxWidget(value=o)
54 elif isinstance(o, float):
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 elif isinstance(o, int):
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 if isinstance(o, (list, tuple)):
75 if isinstance(o, (list, tuple)):
59 if _matches(o, (int, int)):
76 if _matches(o, (int, int)):
60 min, max, value = _min_max_value(o)
77 min, max, value = _get_min_max_value(o[0], o[1], None)
61 return IntSliderWidget(value=int(value), min=min, max=max)
78 return IntSliderWidget(value=value, min=min, max=max)
62 elif _matches(o, (int, int, int)):
79 elif _matches(o, (int, int, int)):
63 min, max, value = _min_max_value(o)
80 min, max, value = _get_min_max_value(o[0], o[1], None)
64 return IntSliderWidget(value=int(value), min=min, max=max, step=o[2])
81 return IntSliderWidget(value=value, min=min, max=max, step=o[2])
65 elif _matches(o, (float, float)):
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 return FloatSliderWidget(value=value, min=min, max=max)
84 return FloatSliderWidget(value=value, min=min, max=max)
68 elif _matches(o, (float, float, float)):
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 return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
87 return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
71 elif _matches(o, (float, float, int)):
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 return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
90 return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
74 elif all(isinstance(x, string_types) for x in o):
91 elif all(isinstance(x, string_types) for x in o):
75 return DropdownWidget(value=unicode_type(o[0]),
92 return DropdownWidget(value=unicode_type(o[0]),
@@ -84,7 +101,7 b' def interactive(f, **kwargs):'
84 widgets = []
101 widgets = []
85 container = ContainerWidget()
102 container = ContainerWidget()
86 container.result = None
103 container.result = None
87 container.arguments = dict()
104 container.kwargs = dict()
88 for key, value in kwargs.items():
105 for key, value in kwargs.items():
89 if isinstance(value, Widget):
106 if isinstance(value, Widget):
90 widget = value
107 widget = value
@@ -92,6 +109,7 b' def interactive(f, **kwargs):'
92 widget = _widget_abbrev(value)
109 widget = _widget_abbrev(value)
93 if widget is None:
110 if widget is None:
94 raise ValueError("Object cannot be transformed to a Widget")
111 raise ValueError("Object cannot be transformed to a Widget")
112 widget.description = key
95 widgets.append((key,widget))
113 widgets.append((key,widget))
96 widgets.sort(key=lambda e: e[1].__class__.__name__)
114 widgets.sort(key=lambda e: e[1].__class__.__name__)
97 container.children = [e[1] for e in widgets]
115 container.children = [e[1] for e in widgets]
@@ -101,9 +119,7 b' def interactive(f, **kwargs):'
101 actual_kwargs = {}
119 actual_kwargs = {}
102 for key, widget in widgets:
120 for key, widget in widgets:
103 value = widget.value
121 value = widget.value
104 if hasattr(widget, 'actual_values'):
122 container.kwargs[key] = value
105 value = widget.actual_values[value]
106 container.arguments[key] = value
107 actual_kwargs[key] = value
123 actual_kwargs[key] = value
108 if co:
124 if co:
109 clear_output(wait=True)
125 clear_output(wait=True)
@@ -112,9 +128,8 b' def interactive(f, **kwargs):'
112 # Wire up the widgets
128 # Wire up the widgets
113 for key, widget in widgets:
129 for key, widget in widgets:
114 widget.on_trait_change(call_f, 'value')
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 return container
134 return container
120
135
1 NO CONTENT: modified file
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