Lab: Interactive Functions#
This lab has some simple user interfaces that will be completed by a function you write. Run the next cell before you begin:
[ ]:
import ipywidgets
from p4e.widgets import bind
Part 1: Interactive Functions#
Each of the following questions has code for a user interface (UI) and asks you to write a specified funtion.
1. Add Integers#
Using the UI in the next cell:
[ ]:
a_widget = ipywidgets.IntText(description="a:")
b_widget = ipywidgets.IntText(description="b:")
display(
a_widget,
b_widget,
bind('add_ints', {'a': a_widget, 'b': b_widget})
)
Write a function add_ints
that takes two inputs, a
and b
and returns their sum.
Function name:
add_ints
Arguments:
a
(int
): The first intb
(int
): The second int
Returns: The sum of
a
andb
[ ]:
2. Hyperlink#
Using the UI in the next cell:
[ ]:
url_widget = ipywidgets.Text(description="URL:", value="https://www.lifealgorithmic.com/")
text_widget = ipywidgets.Text(description="Text:", value="Class Home")
display(
url_widget,
text_widget,
bind('hyper_link', {'url': url_widget, 'text': text_widget})
)
Write a function named hyper_link
that takes two inputs, url
and text
and returns an HTML
containing a hyperlink that takes the browser to url
and displays text
.
Function name:
hyper_link
Arguments:
url
(str
): The link targettext
(str
): The link text
Returns: An
HTML
with the hyperlink (it should be clickable).
[ ]:
Part 2: Global Widgets#
In this part your interactive functions will have to use global variables.
1. Minimum and Maximum#
The interface in the next cell has three widgets:
A slider that selects an integer
An IntText that sets the minimum for the slider
An IntText that sets the maximum for the slider
Run the UI in the next cell:
[ ]:
int_widget = ipywidgets.IntSlider(description='Number:', min=0, max=10)
min_widget = ipywidgets.IntText(description='Min:', value=0)
max_widget = ipywidgets.IntText(description='Max:', value=10)
bind('set_minmax', {'min': min_widget, 'max': max_widget,})
display(
int_widget, min_widget, max_widget,
)
Write a funcion called set_minmax
that sets the values int_widget.min
and int_widget.max
.
Function name:
set_minmax
Arguments:
min
(int
) - The minimummax
(int
) - The maximum
Returns
None
[ ]: