#!/usr/bin/env python # coding: utf-8 # # Using Interact # The `interact` function (`ipywidgets.interact`) automatically creates user interface (UI) controls for exploring code and data interactively. It is the easiest way to get started using IPython's widgets. # In[ ]: from __future__ import print_function # for python 2 from ipywidgets import interact, interactive, fixed import ipywidgets as widgets #
# The widgets in this notebook won't show up on http://nbviewer.ipython.org. To view the widgets and interact with them, you will need to download this notebook and run it with an IPython Notebook server. #
# ## Basic `interact` # At the most basic level, `interact` autogenerates UI controls for function arguments, and then calls the function with those arguments when you manipulate the controls interactively. To use `interact`, you need to define a function that you want to explore. Here is a function that prints its only argument `x`. # In[ ]: def f(x): print(x) # In[ ]: f(10) # When you pass this function as the first argument to `interact` along with an integer keyword argument (`x=10`), a slider is generated and bound to the function. # In[ ]: interact(f, x=10); # When you move the slider, the function is called and the current value of `x` is printed. # # If you pass `True` or `False`, `interact` will generate a checkbox: # In[ ]: interact(f, x=True); # If you pass a string, `interact` will generate a text area. # In[ ]: interact(f, x='Hi there!'); # `interact` can also be used as a decorator. This allows you to define a function and interact with it in a single shot. As this example shows, `interact` also works with functions that have multiple arguments. # In[ ]: @interact(x=True, y=1.0) def g(x, y): print(x, y) # ## Fixing arguments using `fixed` # There are times when you may want to explore a function using `interact`, but fix one or more of its arguments to specific values. This can be accomplished by wrapping values with the `fixed` function. # In[ ]: def h(p, q): print(p, q) # In[ ]: h(5, 10) # When we call `interact`, we pass `fixed(20)` for q to hold it fixed at a value of `20`. # In[ ]: interact(h, p=5, q=fixed(20)); # Notice that a slider is only produced for `p` since the value of `q` is fixed. # ## Widget creation # The `interact` function tries to guess which type of control you want based on the value you pass in to the `interact` function. When you pass an integer argument to `interact`, such as `interact(f, x=10)`, an integer-valued slider control is generated centered at the value: # # ```python # widgets.IntSlider(min=-10,max=30,step=1,value=10) # ``` # # We can also directly create the widget when calling `interact`, rather than having `interact` guess: # In[ ]: interact(f, x=widgets.IntSlider(min=-10,max=30,step=1,value=10)); # This examples clarifies how `interact` processes its keyword arguments: # # 1. If the keyword argument is a `Widget` instance with a `value` attribute, the widget is used. Any widget with a `value` attribute can be used, even custom ones. # 2. Otherwise, the value is used to guess a correct a widget, which is used. # # The following table gives an overview of `interact` translates arguments into widgets: # # # # # # # #
Keyword argumentWidget
`True` or `False`Checkbox
string `'Hi there'`Textarea
numeric `value` or `(min,max)` or `(min,max,step)`IntSlider or FloatSlider
`('orange','apple')` or `{'one':1,'two':2}`Dropdown
# You have seen how the checkbox and textarea widgets work above. Here, more details about the different abbreviations for sliders and dropdowns are given. # # If a 2-tuple of integers is passed, `(min,max)`, a integer-valued slider is produced with those minimum and maximum (inclusive) values. In this case, the default step size of `1` is used. # In[ ]: interact(f, x=(0,4)); # If a 3-tuple of integers is passed `(min,max,step)`, the step size is set. # In[ ]: interact(f, x=(0,8,2)); # A float-valued slider is produced if the elements of the tuples are floats. Here the minimum is `0.0`, the maximum is `10.0` and step size is `0.1` (the default). # In[ ]: interact(f, x=(0.0,10.0)); # The step size can be changed by passing a 3-element tuple. The length of the slider may necessitate skipping some steps if there are two many to fit. # In[ ]: interact(f, x=(0.0,10.0,0.01)); # For both integer and float-valued sliders, the initial value is the default value of the function parameter, if it exists. Here we set the initial value of a float slider to `5.5`. # In[ ]: @interact(x=(0.0,20.0,0.5)) def h(x=5.5): print(x) # Dropdown menus can be produced by passing a tuple of strings. In this case, the strings are used both as the names in the dropdown menu UI and passed to the underlying Python function. # In[ ]: interact(f, x=('apples','oranges')); # If you want a dropdown menu that passes non-string values to the Python function, you can pass a dictionary. The keys in the dictionary are used for the names in the dropdown menu UI and the values are the arguments that are passed to the underlying Python function. # In[ ]: from collections import OrderedDict interact(f, x={'a': 10, 'b': 20}); # ## `interactive` # In addition to `interact`, IPython provides another function, `interactive`, that is useful when you want to reuse the widgets that are produced or access the data that is bound to the UI controls. # Here is a function that returns the sum of its two arguments. # In[ ]: def f(a, b): return a+b # In[ ]: f(1,2) # Unlike `interact`, `interactive` returns a `Widget` instance rather than immediately displaying the widget. # In[ ]: w = interactive(f, a=10, b=20) # The widget is a `Box`, which is a container for other widgets. # In[ ]: type(w) # The children of the `Box` are two integer valued sliders produced by the widget abbreviations above. # In[ ]: w.children # To actually display the widgets, you can use IPython's `display` function. # In[ ]: from IPython.display import display display(w) # At this point, the UI controls work just like they would if `interact` had been used. You can manipulate them interactively and the function will be called. However, the widget instance returned by `interactive` also gives you access to the current keyword arguments and return value of the underlying Python function. # # Here are the current keyword arguments. If you rerun this cell after manipulating the sliders, the values will have changed. # In[ ]: w.kwargs # Here is the current return value of the function. # In[ ]: w.result # In[ ]: