<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 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.
Step1:  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.
Step2:  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.
Step3:  When you move the slider, the function is called, which prints the current value of x.
If you pass True or False, interact will generate a checkbox
Step4:  If you pass a string, interact will generate a text area.
Step5:  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.
Step6:  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.
Step7:  When we call interact, we pass fixed(20) for q to hold it fixed at a value of 20.
Step8:  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
Step9:  This examples clarifies how interact proceses its keyword arguments
Step10:  If a 3-tuple of integers is passed (min,max,step), the step size can also be set.
Step11:  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).
Step12:  The step size can be changed by passing a third element in the tuple.
Step13:  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.
Step14:  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.
Step15:  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.
Step16:  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.
Step17:  Unlike interact, interactive returns a Widget instance rather than immediately displaying the widget.
Step18:  The widget is an interactive, a subclass of VBox, which is a container for other widgets.
Step19:  The children of the interactive are two integer-valued sliders and an output widget, produced by the widget abbreviations above.
Step20:  To actually display the widgets, you can use IPython's display function.
Step21:  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. Note that unlike interact, the return value of the function will not be displayed automatically, but you can display a value inside the function with IPython.display.display.
Here are the current keyword arguments. If you rerun this cell after manipulating the sliders, the values will have changed.
Step22:  Here is the current return value of the function.
Step23:  Disabling continuous updates
When interacting with long running functions, realtime feedback is a burden instead of being helpful.  See the following example
Step24:  Notice that the output is updated even while dragging the mouse on the slider. This is not useful for long running functions due to lagging
Step25:  There are two ways to mitigate this.  You can either only execute on demand, or restrict execution to mouse release events.
interact_manual
The interact_manual function provides a variant of interaction that allows you to restrict execution so it is only done on demand.  A button is added to the interact controls that allows you to trigger an execute event.
Step26:  continuous_update
If you are using slider widgets, you can set the continuous_update kwarg to False.  continuous_update is a kwarg of slider widgets that restricts executions to mouse release events.
Step27:  Arguments that are dependent of each other
Arguments that are dependent of each other can be expressed manually using observe.  See the following example, where one variable is used to describe the bounds of another.  For more information, please see the widget events example notebook.
<ASSISTANT_TASK:>
Python Code:
from __future__ import print_function
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
Explanation: 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.
End of explanation
def f(x):
    return x
Explanation: 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.
End of explanation
interact(f, x=10);
Explanation: 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.
End of explanation
interact(f, x=True);
Explanation: When you move the slider, the function is called, which prints the current value of x.
If you pass True or False, interact will generate a checkbox:
End of explanation
interact(f, x='Hi there!');
Explanation: If you pass a string, interact will generate a text area.
End of explanation
@interact(x=True, y=1.0)
def g(x, y):
    return (x, y)
Explanation: 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.
End of explanation
def h(p, q):
    return (p, q)
Explanation: 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.
End of explanation
interact(h, p=5, q=fixed(20));
Explanation: When we call interact, we pass fixed(20) for q to hold it fixed at a value of 20.
End of explanation
interact(f, x=widgets.IntSlider(min=-10,max=30,step=1,value=10));
Explanation: 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:
End of explanation
interact(f, x=(0,4));
Explanation: This examples clarifies how interact proceses its keyword arguments:
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.
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:
<table class="table table-condensed table-bordered">
  <tr><td><strong>Keyword argument</strong></td><td><strong>Widget</strong></td></tr>  
  <tr><td>`True` or `False`</td><td>Checkbox</td></tr>  
  <tr><td>`'Hi there'`</td><td>Text</td></tr>
  <tr><td>`value` or `(min,max)` or `(min,max,step)` if integers are passed</td><td>IntSlider</td></tr>
  <tr><td>`value` or `(min,max)` or `(min,max,step)` if floats are passed</td><td>FloatSlider</td></tr>
  <tr><td>`['orange','apple']` or `{'one':1,'two':2}`</td><td>Dropdown</td></tr>
</table>
Note that a dropdown is used if a list or a dict 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 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), an integer-valued slider is produced with those minimum and maximum values (inclusively). In this case, the default step size of 1 is used.
End of explanation
interact(f, x=(0,8,2));
Explanation: If a 3-tuple of integers is passed (min,max,step), the step size can also be set.
End of explanation
interact(f, x=(0.0,10.0));
Explanation: 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).
End of explanation
interact(f, x=(0.0,10.0,0.01));
Explanation: The step size can be changed by passing a third element in the tuple.
End of explanation
@interact(x=(0.0,20.0,0.5))
def h(x=5.5):
    return x
Explanation: 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.
End of explanation
interact(f, x=['apples','oranges']);
Explanation: 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.
End of explanation
interact(f, x={'one': 10, 'two': 20});
Explanation: 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.
End of explanation
def f(a, b):
    return a+b
Explanation: 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.
End of explanation
w = interactive(f, a=10, b=20)
Explanation: Unlike interact, interactive returns a Widget instance rather than immediately displaying the widget.
End of explanation
type(w)
Explanation: The widget is an interactive, a subclass of VBox, which is a container for other widgets.
End of explanation
w.children
Explanation: The children of the interactive are two integer-valued sliders and an output widget, produced by the widget abbreviations above.
End of explanation
from IPython.display import display
display(w)
Explanation: To actually display the widgets, you can use IPython's display function.
End of explanation
w.kwargs
Explanation: 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. Note that unlike interact, the return value of the function will not be displayed automatically, but you can display a value inside the function with IPython.display.display.
Here are the current keyword arguments. If you rerun this cell after manipulating the sliders, the values will have changed.
End of explanation
w.result
Explanation: Here is the current return value of the function.
End of explanation
def slow_function(i):
    print(int(i),list(x for x in range(int(i)) if 
                str(x)==str(x)[::-1] and 
                str(x**2)==str(x**2)[::-1]))
    return
%%time
slow_function(1e6)
Explanation: Disabling continuous updates
When interacting with long running functions, realtime feedback is a burden instead of being helpful.  See the following example:
End of explanation
from ipywidgets import FloatSlider
interact(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5));
Explanation: Notice that the output is updated even while dragging the mouse on the slider. This is not useful for long running functions due to lagging:
End of explanation
interact_manual(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5));
Explanation: There are two ways to mitigate this.  You can either only execute on demand, or restrict execution to mouse release events.
interact_manual
The interact_manual function provides a variant of interaction that allows you to restrict execution so it is only done on demand.  A button is added to the interact controls that allows you to trigger an execute event.
End of explanation
interact(slow_function,i=FloatSlider(min=1e5, max=1e7, step=1e5, continuous_update=False));
Explanation: continuous_update
If you are using slider widgets, you can set the continuous_update kwarg to False.  continuous_update is a kwarg of slider widgets that restricts executions to mouse release events.
End of explanation
x_widget = FloatSlider(min=0.0, max=10.0, step=0.05)
y_widget = FloatSlider(min=0.5, max=10.0, step=0.05, value=5.0)
def update_x_range(*args):
    x_widget.max = 2.0 * y_widget.value
y_widget.observe(update_x_range, 'value')
def printer(x, y):
    print(x, y)
interact(printer,x=x_widget, y=y_widget);
Explanation: Arguments that are dependent of each other
Arguments that are dependent of each other can be expressed manually using observe.  See the following example, where one variable is used to describe the bounds of another.  For more information, please see the widget events example notebook.
End of explanation
<END_TASK>