-
Widgets 2
-
Lecture1.1
-
Lecture1.2
-
-
Layouts 1
-
Lecture2.1
-
-
Interact 2
-
Lecture3.1
-
Lecture3.2
-
Interact Part 1
The interaction piece of ipywidgets is what makes this library so powerful. Try the following basic example.
from IPython.display import display
from ipywidgets import widgets, interact
def print_value(x):
print(x)
display(interact(print_value, x=10))
We define a function, and set a variable in interact and then the function will change each time the widget changes.
For example, what about printing the slider value and the squared value?
def print_value_squared(x):
print(x)
print(x**2)
display(interact(print_value_squared, x=10))
We can also use two values. For example if we want to multiply two slider values dynamically….
def multiply_values(x,y):
print(x * y)
display(interact(multiply_values, x=10, y=10))
Let’s combine the a different type of a value, a boolean which decides whether or not we print the slider value or the squared slider value.
def squared_bool(x,squared_flag):
if squared_flag:
print(x ** 2)
else:
print(x)
display(interact(squared_bool, x=10, squared_flag=True))
Another type we could use is a string, if for example we wanted to print the lower cased version of a string that comes as an input.
def lower_case(x):
print(x.lower())
display(interact(lower_case, x='ALL UPPER!'))
Prev
Layouts
Next
Interact Part 2