Show More
@@ -227,12 +227,65 b' def interactive(__interact_f, **kwargs):' | |||||
227 | return container |
|
227 | return container | |
228 |
|
228 | |||
229 | def interact(__interact_f=None, **kwargs): |
|
229 | def interact(__interact_f=None, **kwargs): | |
230 | """interact(f, **kwargs) |
|
230 | """ | |
|
231 | Displays interactive widgets which are tied to a function. | |||
|
232 | Expects the first argument to be a function. Parameters to this function are | |||
|
233 | passed in as keyword arguments (**kwargs). Can be used as a decorator (see | |||
|
234 | examples). | |||
|
235 | ||||
|
236 | Returns | |||
|
237 | ------- | |||
|
238 | __interact_f with interactive widget attached to it. | |||
|
239 | ||||
|
240 | Parameters | |||
|
241 | ---------- | |||
|
242 | __interact_f : function | |||
|
243 | The function to which the interactive widgets are tied. The **kwargs | |||
|
244 | should match the function signature. Passed to :func:`interactive()` | |||
|
245 | **kwargs : various, optional | |||
|
246 | An interactive widget will be created for each keyword argument. Passed | |||
|
247 | to :func:`interactive()` | |||
|
248 | ||||
|
249 | Examples | |||
|
250 | -------- | |||
|
251 | Renders an interactive text field that shows the greeting with the passed in | |||
|
252 | text. | |||
231 |
|
253 | |||
232 | Interact with a function using widgets.""" |
|
254 | 1. Invocation of interact as a function | |
|
255 | def greeting(text="World"): | |||
|
256 | print "Hello {}".format(text) | |||
|
257 | interact(greeting, text="IPython Widgets") | |||
|
258 | ||||
|
259 | 2. Invocation of interact as a decorator | |||
|
260 | @interact | |||
|
261 | def greeting(text="World"): | |||
|
262 | print "Hello {}".format(text) | |||
|
263 | ||||
|
264 | 3. Invocation of interact as a decorator with named parameters | |||
|
265 | @interact(text="IPython Widgets") | |||
|
266 | def greeting(text="World"): | |||
|
267 | print "Hello {}".format(text) | |||
|
268 | ||||
|
269 | Renders an interactive slider widget and prints square of number. | |||
|
270 | ||||
|
271 | 1. Invocation of interact as a function | |||
|
272 | def square(num=1): | |||
|
273 | print "{} squared is {}".format(num, num*num) | |||
|
274 | interact(square, num=5) | |||
|
275 | ||||
|
276 | 2. Invocation of interact as a decorator | |||
|
277 | @interact | |||
|
278 | def square(num=2): | |||
|
279 | print "{} squared is {}".format(num, num*num) | |||
|
280 | ||||
|
281 | 3. Invocation of interact as a decorator with named parameters | |||
|
282 | @interact(num=5) | |||
|
283 | def square(num=2): | |||
|
284 | print "{} squared is {}".format(num, num*num) | |||
|
285 | """ | |||
233 | # positional arg support in: https://gist.github.com/8851331 |
|
286 | # positional arg support in: https://gist.github.com/8851331 | |
234 | if __interact_f is not None: |
|
287 | if __interact_f is not None: | |
235 |
# This branch handles the cases |
|
288 | # This branch handles the cases 1 and 2 | |
236 | # 1. interact(f, **kwargs) |
|
289 | # 1. interact(f, **kwargs) | |
237 | # 2. @interact |
|
290 | # 2. @interact | |
238 | # def f(*args, **kwargs): |
|
291 | # def f(*args, **kwargs): | |
@@ -249,7 +302,7 b' def interact(__interact_f=None, **kwargs):' | |||||
249 | display(w) |
|
302 | display(w) | |
250 | return f |
|
303 | return f | |
251 | else: |
|
304 | else: | |
252 |
# This branch handles the case |
|
305 | # This branch handles the case 3 | |
253 | # @interact(a=30, b=40) |
|
306 | # @interact(a=30, b=40) | |
254 | # def f(*args, **kwargs): |
|
307 | # def f(*args, **kwargs): | |
255 | # ... |
|
308 | # ... |
General Comments 0
You need to be logged in to leave comments.
Login now