-
Widgets 2
-
Lecture1.1
-
Lecture1.2
-
-
Layouts 1
-
Lecture2.1
-
-
Interact 2
-
Lecture3.1
-
Lecture3.2
-
Widgets Part 1
In this course we are going to learn the basics of ipywidgets which is a library that allows the user to build an interactive user interface. These interfaces can be used to build dynamic analysis for different purposes. This first course may seem boring but it will lead to building extremely interesting modules in the future for other courses.
The first thing is that we are going to create a basic widget. We define it first, in this case an IntSlider, and then we use display to display the widget. An integer slider gives a UI widget that slides between integer values.
from IPython.display import display
from ipywidgets import widgets
#A widget is an interactive ipython object that allows you to control a variable with a UI
#The following will give you an integer slider for example
X = widgets.IntSlider(
)
display(X)
Giving an argument for value will set the default value, and then min and max will allow us to set the bounds.
#You can set a default value with value as well as minimum and maximum
X = widgets.IntSlider(value=50,
min=1,
max=200,
)
display(X)
Another thing you can add is the step parameter which will set the amount by which the slider increases or decreases with.
#If you want to control the jumps you could also set a step
X = widgets.IntSlider(value=50,
min=10,
max=200,
step=10,
)
display(X)
A float slider is the same as the integer slider except it returns floating point numbers.
#A float slider works the same except for floating point numbers
#Here we will have a slide with a minimum of 10 and a maximum of 200 that will go up by .1 at a time
X = widgets.FloatSlider(value=50,
min=10,
max=200,
step=.1,
)
display(X)
Using the description parameter we can set a label for the widget.
#Of course sometimes you want to add a label, which can be done with description
X = widgets.FloatSlider(value=50,
min=10,
max=200,
step=.1,
description='Value:',
)
display(X)
If we want a specific number formatting, the readout_format parameter takes a formatting representation to be used. For example, the following will make it so that we only display one decimal point.
#Or maybe you want to change the read out format
#Giving a number formatting string will achieve this, the following says to show just one decimal
X = widgets.FloatSlider(value=50,
min=10,
max=200,
step=.1,
description='Value:',
readout_format='.1f'
)
display(X)
Something to realize is that when you display a widget a second time it is still linked. If you toggle the second one you will notice the first one also moves with it.
#Something to realize is that when you display a widget a second time it is still linked
#If you toggle this one you will notice the one above also moves with it
display(X)
You can close down a widget with close().
#You can close a widget with close()
X.close()
The BoundedFloatText widget uses a text box input instead of a slider.
#If you prefer to enter the number in you can use the BoundedFloatText
X = widgets.BoundedFloatText(
value=5.5,
min=0,
max=20.0,
step=0.05,
description='Number:')
display(X)