Interact widget slidebar

We can use interact to produce a slidebar by passing in an extent. For example, we have the following script:

#imports 
from ipywidgets import interact 
 
# define a function to work with (cubes the number) 
def myfunction(arg): 
    return arg+1 
 
#take values from slidebar and apply to function provided 
interact(myfunction, arg=9); 
Note that the semicolon following the interact function call is required.

We have a script which does the following:

  •  References the package we want to use
  •  Defines a function that is called for every user input of a value
  •  Calls to interact, passing our handler and a range of values

When we run this script, we get a scrollbar that is modifiable by the user:

The user is able to slide the vertical bar over the range of values. The upper end is 27 and the lower end is -1 (assuming we could pass additional arguments to interact to set the range of values selectable). myfunction is called every time the value in the interact widget is changed and the result printed. Hence, we see 27 selected and the number 28 displayed (following the processing of myfunction - 27 + 1).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.4