-
Widgets 2
-
Lecture1.1
-
Lecture1.2
-
-
Layouts 1
-
Lecture2.1
-
-
Interact 2
-
Lecture3.1
-
Lecture3.2
-
Interact Part 2
What if we had a function where we did not actually want to change one the values? In this case we can use fixed which will then not produce a widget.
from ipywidgets import fixed
def multiply_values(x,y):
print(x * y)
display(interact(multiply_values, x=10, y=fixed(10)))
Now, we might want to use widgets that we have created instead of setting values like we have. In this case we just define the variables to be equal to the widgets. For example, to use two defined widgets….
value1 = widgets.FloatSlider(value=50,
min=10,
max=200,
step=.1,
description='Value:',
readout_format='.1f'
)
value2 = widgets.FloatSlider(value=50,
min=10,
max=200,
step=.1,
description='Value:',
readout_format='.1f'
)
display(interact(multiply_values, x=value1, y=value2))
In case you don’t recall, numpy’s linspace will make N points equally spaced between a lower an upper bound. Check out what the following produces.
import numpy as np
print(np.linspace(0,10,100))
I define this function because we will be using it to produce a graph for compound interest. Don’t worry about the exact formula if you aren’t familiar, but this graph represents what money would be worth at each point in time given an interest rate.
import matplotlib.pyplot as plt
def compound_growth(principal, r, t):
X = np.linspace(0,t,100)
Y = principal * (1 + r) ** (X)
plt.plot(X, Y)
plt.show()
compound_growth(100, .08, 10)
With this function we could define widgets to toggle the graph based on different values. You will notice the formatting for r (the growth rate) is set to a percent. If you play with this graph the line will toggle to represent the different assumptions.
principal = widgets.IntSlider(value=100,
min=10,
max=1000,
step = 10,
description='Principal:'
)
#Note the read out format to make it a percent
r = widgets.FloatSlider(value=.08,
min=.01,
max=.40,
step=.01,
description='Rate of Return:',
readout_format='.0%'
)
t = widgets.IntSlider(value=10,
min=1,
max=50,
step = 1,
description='Years:'
)
display(interact(compound_growth, principal=principal, r=r, t=t))
If you use interactive instead of interact you can get an object returned so that you can later call it.
from ipywidgets import interactive
graph = interactive(compound_growth, principal=principal, r=r, t=t)
display(graph)
If you don’t want it to update every time you can use interact_manual which makes it so the update only happens when you load changes.
from ipywidgets import interact_manual
interact_manual(compound_growth, principal=principal, r=r, t=t)
If you prefer to use layouts, you can use interactive_output with a function and then a dictionary mapping arguments to the widget controlling them. Then you can display them together or separately. The important part here is that you are able to use the widgets in a layout mixed with an output.
ui = widgets.HBox([principal, r, t])
graph = widgets.interactive_output(compound_growth, {'principal': principal,
'r': r,
't': t})
display(ui, graph)