In this example, we put a button control on our Notebook:
- First, we reference the widgets library and the library to display the button:
import ipywidgets as widgetsfrom IPython.display import display
- We create a button. This is like calling a library function, except that the function returns a widget object:
my_button = widgets.Button(description='Click My Button')
- We need to display the button:
display(my_button)
- When the user clicks, we want to print a message in the Notebook. We create the handler for the button clicks (we could've done any number of things, but in this example, we are just adding an output message):
def my_button_clicked(b): print("You clicked on My Button")
- We have to tell the button where ...