This collection of exercises can be done in any order, or even skipped entirely; none of the rest of the tutorial depends on it. Choose the ones that are of interest to you. Sample solutions are provided for all of them.
That is why we provide solutions!
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as widgets
from ipywidgets import interact, interactive
Write a function that takes text as an input and reverses the text, and uses interact
or interactive
to generate the input field.
# %load solutions/interact-basic-list/reverse-text.py
Pick a function of interest to you that depends on a couple of parameters and use interact
to generate controls for the parameters.
If you don't have a favorite function, try graphing $f(x) = \sin(k x + p)$ over the $0 < x < 4\pi$, with $0.5 \le k \le 2$ and $0 \le p < 2\pi$.
Use either the interact
function or the @interact
decorator.
# %load solutions/interact-basic-list/plot-function.py
Some of the exercises below might be possible only with interact and interactive.
Choose any two widgets from the widget list, link their values using widgets.jslink
, and display both widgets (the display
function can take more than one argument).
Note: for all widgets except the section widgets below you should link the value
of the widgets. For the selection widgets, link the index
instead.
Selection widgets:
Dropdown
RadioButtons
Select
SelectionSlider
SelectionRangeSlider
ToggleButtons
SelectMultiple
# %load solutions/interact-basic-list/sample-linked-widgets.py
Create any three widgets, place them in a VBox
, and display it.
# %load solutions/interact-basic-list/widgets-in-a-box.py
The cell below is for you to try out antyhing you would like. Be creative!