#!/usr/bin/env python # coding: utf-8 # # < [Overview](01.00-overview.ipynb) | [Contents](00.00-index.ipynb) | [OPTIONAL - More about interact](02.01-OPTIONAL-More-About-Interact.ipynb) > # # Widgets without writing widgets: 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 ipywidgets import interact, interactive, fixed, interact_manual import ipywidgets as widgets # ## 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 triples its argument, `x`. # In[ ]: def f(x): return 3 * x # 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 parameter. # In[ ]: interact(f, x=10); # When you move the slider, the function is called, and the return value 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` field. # 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[ ]: @widgets.interact(x=True, y=1.0) def g(x, y): return (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): return (p, q) # 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` as the value of `q` is fixed. # ## Widget abbreviations # When you pass an integer-valued keyword argument of `10` (`x=10`) to `interact`, it generates an integer-valued slider control with a range of `[-10, +3*10]`. In this case, `10` is an *abbreviation* for an actual slider widget: # # ```python # IntSlider(min=-10, max=30, step=1, value=10) # ``` # # In fact, we can get the same result if we pass this `IntSlider` as the keyword argument for `x`: # 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, that widget is used. Any widget with a `value` attribute can be used, even custom ones. # 2. Otherwise, the value is treated as a *widget abbreviation* that is converted to a widget before it is used. # # The following table gives an overview of different widget abbreviations: # # # # # # # # #
Keyword argumentWidget
`True` or `False`Checkbox
`'Hi there'`Text
`value` or `(min,max)` or `(min,max,step)` if integers are passedIntSlider
`value` or `(min,max)` or `(min,max,step)` if floats are passedFloatSlider
`['orange','apple']` or `[('one', 1), ('two', 2)]`Dropdown
# Note that a dropdown is used if a list or a list of tuples is given (signifying discrete choices), and a slider is used if a tuple is given (signifying a range). # You have seen how the checkbox and text 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)`, an integer-valued slider is produced with those minimum and maximum values (inclusively). In this case, the default step size of `1` is used. # In[ ]: interact(f, x=(0, 4)); # A `FloatSlider` is generated if any of the values are floating point. The step size can be changed by passing a third element in the tuple. # In[ ]: interact(f, x=(0, 10, 0.01)); # ### Exercise: Reverse some text # # Here is a function that takes text as an input and returns the text backwards. # In[ ]: def reverse(x): return x[::-1] reverse('I am printed backwards.') # Use `interact` to make interactive controls for this function. # In[ ]: # In[ ]: # %load solutions/interact-basic-list/reverse-text.py # For both integer and float-valued sliders, you can pick the initial value of the widget by passing a default keyword argument to the underlying Python function. 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): return x # Dropdown menus are constructed by passing a list of strings. In this case, the strings are both used 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 list of tuples of the form `('label', value)`. The first items are the names in the dropdown menu UI and the second items are values that are the arguments passed to the underlying Python function. # In[ ]: interact(f, x=[('one', 10), ('two', 20)]); # ## Basic interactive plot # # Though the examples so far in this notebook had very basic output, more interesting possibilities are straightforward. # # The function below plots a straight line whose slope and intercept are given by its arguments. # In[ ]: get_ipython().run_line_magic('matplotlib', 'widget') import matplotlib.pyplot as plt import numpy as np def f(m, b): plt.figure(2) plt.clf() plt.grid() x = np.linspace(-10, 10, num=1000) plt.plot(x, m * x + b) plt.ylim(-5, 5) plt.show() # The interactive below displays a line whose slope and intercept is set by the sliders. Note that if the variable containing the widget, `interactive_plot`, is the last thing in the cell it is displayed. # In[ ]: interact(f, m=(-2.0, 2.0), b=(-3, 3, 0.5)) # ### Exercise: Make a plot # # Here is a python function that, given $k$ and $p$, plots $f(x) = \sin(k x - p)$. # # In[ ]: def plot_f(k, p): plt.figure(5) plt.clf() plt.grid() x = np.linspace(0, 4 * np.pi) y = np.sin(k*x - p) plt.plot(x, y) plt.show() # Copy the above function definition and make it interactive using `interact`, so that there are sliders for the parameters $k$ and $p$, where $0.5\leq k \leq 2$ and $0 \leq p \leq 2\pi$ (hint: use `np.pi` for $\pi$). # In[ ]: # %load solutions/interact-basic-list/plot-function.py interact(plot_f, k=(0.5, 2), p=(0, 2 * np.pi)) # # For more information # # See the notebook [02.01-More About interact](02.01-OPTIONAL-More-About-Interact.ipynb) for more information about other ways of generating interactive controls for functions and for details about how to control when sliders are updated. # # For more extended examples of `interact` and `interactive`, see [the example in the ipywidgets source repository](https://github.com/jupyter-widgets/ipywidgets/blob/master/docs/source/examples/Index.ipynb). # # < [Overview](01.00-overview.ipynb) | [Contents](00.00-index.ipynb) | [OPTIONAL - More about interact](02.01-OPTIONAL-More-About-Interact.ipynb) >