Widgets

Jupyter widgets are an excellent way to create controls in a notebook that allow a user to interact with their data. There are many easy-to-implement widgets available in Jupyter. They have been designed to follow a similar naming schema (where possible), so we only need to go through a few examples before we can get a reasonable feel for their scope:

  1. First, import widgets from ipywidgets, as shown in the following screenshot:

  1. Some common forms of widgets displaying numerical values (integers or floats) include sliders, progress bars, and text inputs. Here, we will demonstrate a slider that allows you to select a range of float values:

In the preceding screenshot, we can see the following:

    • value sets the initially selected lower and upper range float values. 
    • min and max set the limits of the slider; step sets the minimum increment for moving the slider.
    • description sets the slider label.
    • readout_format sets the format (for example, how many decimal places) for displaying the numerical value.
    • We call display() to render the widget in the notebook.

  1. Next, we will use ToggleButton to display a Boolean value (True or False), as shown in the following screenshot:

Just as in the previous widget, value sets the widget's initial state and description sets the widget's label.

Another common way to interact with data is by selecting values from a list. Some examples include drop-down menus, buttons, and sliders. Here, we will demonstrate a widget where a user can use a drop-down menu to select a class, as shown in the following screenshot:

In the preceding screenshot, we can see the following:

  • The options for the drop-down menu are provided in a list.
  • We use a list of tuples, where the first item in the tuple is the option label to be displayed and the second value in the tuple is the corresponding value option.
  • value sets the initially selected value in the list, while description sets the drop-down menu label, as shown in the following screenshot:

Now that you know how to create and display some simple widgets, we can learn how to use them to interact with a dataset.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.8.110