Show More
@@ -160,7 +160,23 b' def _widgets_from_abbreviations(seq):' | |||||
160 | return result |
|
160 | return result | |
161 |
|
161 | |||
162 | def interactive(__interact_f, **kwargs): |
|
162 | def interactive(__interact_f, **kwargs): | |
163 | """Build a group of widgets to interact with a function.""" |
|
163 | """ | |
|
164 | Builds a group of interactive widgets tied to a function and places the | |||
|
165 | group into a Box container. | |||
|
166 | ||||
|
167 | Returns | |||
|
168 | ------- | |||
|
169 | container : a Box instance containing multiple widgets | |||
|
170 | ||||
|
171 | Parameters | |||
|
172 | ---------- | |||
|
173 | __interact_f : function | |||
|
174 | The function to which the interactive widgets are tied. The **kwargs | |||
|
175 | should match the function signature. | |||
|
176 | **kwargs : various, optional | |||
|
177 | An interactive widget is created for each keyword argument that is a | |||
|
178 | valid widget abbreviation. | |||
|
179 | """ | |||
164 | f = __interact_f |
|
180 | f = __interact_f | |
165 | co = kwargs.pop('clear_output', True) |
|
181 | co = kwargs.pop('clear_output', True) | |
166 | manual = kwargs.pop('__manual', False) |
|
182 | manual = kwargs.pop('__manual', False) | |
@@ -215,7 +231,7 b' def interactive(__interact_f, **kwargs):' | |||||
215 | # Wire up the widgets |
|
231 | # Wire up the widgets | |
216 | # If we are doing manual running, the callback is only triggered by the button |
|
232 | # If we are doing manual running, the callback is only triggered by the button | |
217 | # Otherwise, it is triggered for every trait change received |
|
233 | # Otherwise, it is triggered for every trait change received | |
218 |
# On-demand running also suppresses running the fu |
|
234 | # On-demand running also suppresses running the function with the initial parameters | |
219 | if manual: |
|
235 | if manual: | |
220 | manual_button.on_click(call_f) |
|
236 | manual_button.on_click(call_f) | |
221 | else: |
|
237 | else: | |
@@ -227,12 +243,65 b' def interactive(__interact_f, **kwargs):' | |||||
227 | return container |
|
243 | return container | |
228 |
|
244 | |||
229 | def interact(__interact_f=None, **kwargs): |
|
245 | def interact(__interact_f=None, **kwargs): | |
230 | """interact(f, **kwargs) |
|
246 | """ | |
|
247 | Displays interactive widgets which are tied to a function. | |||
|
248 | Expects the first argument to be a function. Parameters to this function are | |||
|
249 | widget abbreviations passed in as keyword arguments (**kwargs). Can be used | |||
|
250 | as a decorator (see examples). | |||
|
251 | ||||
|
252 | Returns | |||
|
253 | ------- | |||
|
254 | f : __interact_f with interactive widget attached to it. | |||
231 |
|
255 | |||
232 | Interact with a function using widgets.""" |
|
256 | Parameters | |
|
257 | ---------- | |||
|
258 | __interact_f : function | |||
|
259 | The function to which the interactive widgets are tied. The **kwargs | |||
|
260 | should match the function signature. Passed to :func:`interactive()` | |||
|
261 | **kwargs : various, optional | |||
|
262 | An interactive widget is created for each keyword argument that is a | |||
|
263 | valid widget abbreviation. Passed to :func:`interactive()` | |||
|
264 | ||||
|
265 | Examples | |||
|
266 | -------- | |||
|
267 | Renders an interactive text field that shows the greeting with the passed in | |||
|
268 | text. | |||
|
269 | ||||
|
270 | 1. Invocation of interact as a function | |||
|
271 | def greeting(text="World"): | |||
|
272 | print "Hello {}".format(text) | |||
|
273 | interact(greeting, text="IPython Widgets") | |||
|
274 | ||||
|
275 | 2. Invocation of interact as a decorator | |||
|
276 | @interact | |||
|
277 | def greeting(text="World"): | |||
|
278 | print "Hello {}".format(text) | |||
|
279 | ||||
|
280 | 3. Invocation of interact as a decorator with named parameters | |||
|
281 | @interact(text="IPython Widgets") | |||
|
282 | def greeting(text="World"): | |||
|
283 | print "Hello {}".format(text) | |||
|
284 | ||||
|
285 | Renders an interactive slider widget and prints square of number. | |||
|
286 | ||||
|
287 | 1. Invocation of interact as a function | |||
|
288 | def square(num=1): | |||
|
289 | print "{} squared is {}".format(num, num*num) | |||
|
290 | interact(square, num=5) | |||
|
291 | ||||
|
292 | 2. Invocation of interact as a decorator | |||
|
293 | @interact | |||
|
294 | def square(num=2): | |||
|
295 | print "{} squared is {}".format(num, num*num) | |||
|
296 | ||||
|
297 | 3. Invocation of interact as a decorator with named parameters | |||
|
298 | @interact(num=5) | |||
|
299 | def square(num=2): | |||
|
300 | print "{} squared is {}".format(num, num*num) | |||
|
301 | """ | |||
233 | # positional arg support in: https://gist.github.com/8851331 |
|
302 | # positional arg support in: https://gist.github.com/8851331 | |
234 | if __interact_f is not None: |
|
303 | if __interact_f is not None: | |
235 |
# This branch handles the cases |
|
304 | # This branch handles the cases 1 and 2 | |
236 | # 1. interact(f, **kwargs) |
|
305 | # 1. interact(f, **kwargs) | |
237 | # 2. @interact |
|
306 | # 2. @interact | |
238 | # def f(*args, **kwargs): |
|
307 | # def f(*args, **kwargs): | |
@@ -249,7 +318,7 b' def interact(__interact_f=None, **kwargs):' | |||||
249 | display(w) |
|
318 | display(w) | |
250 | return f |
|
319 | return f | |
251 | else: |
|
320 | else: | |
252 |
# This branch handles the case |
|
321 | # This branch handles the case 3 | |
253 | # @interact(a=30, b=40) |
|
322 | # @interact(a=30, b=40) | |
254 | # def f(*args, **kwargs): |
|
323 | # def f(*args, **kwargs): | |
255 | # ... |
|
324 | # ... |
General Comments 0
You need to be logged in to leave comments.
Login now