Chapter 6.  Interactive Widgets

There is a mechanism built for Jupyter to gather input from the user as the script is running. To do this, we put in coding in the form of a widget or user interface control in the script. The widgets we will use in this chapter are defined at http://ipywidgets.readthedocs.io/en/latest/ .

There are widgets for the following:

  • Text input: Notebook users enter a string that will be used later in the script.
  • Button clicks: The user is presented with multiple options in the form of buttons; then, depending on which button is selected (clicked on), your script can change direction according to the user.
  • Slider: You can provide the user with a slider with which the user can select a value within the range you specify, and then your script can use that value accordingly.
  • Toggle box and checkboxes: The user selects the different options of your script that they are interested in working with.
  • Progress bar: If your script will take some time to process, it would be considerate to present a progress bar so they have some idea of how long it might take to finish. Similarly, a progress bar can be used to show how far along they are in a multi-step process.
  • There are a few constraints to the types of input you can gather from the user. So, you could make really interesting widgets that do not fit the standard user input control paradigm. For example, there is a widget (ipyleaflet) allowing a user to click on a geographical map where the underlying script will get the geographic data point selected and operate accordingly.

In this chapter, we will cover the following topics:

  • Installing widgets
  • Widget basics
  • Interact widget
  • Interactive widget
  • Widgets package

Installing widgets

The widgets package is an upgrade to the standard Jupyter installation. You can update the widgets package using this command:

pip install ipywidgets

Note that, if ipywidgets is already installed on your machine you may need to use this command for the upgrade to take effect:

pip install -upgrade ipywidgets

Once complete, you must then upgrade your Jupyter installation using this command:

jupyter nbextension enable -py widgetsnbextension

Tip

You may have to restart your notebook for the extensions to take effect.

If you do not install the package and the upgrade, then when you run your widgets script you will get a warning message in the display—The installed widget JavaScript is the wrong version:

Installing widgets

Note

I updated my installation, but still received the warning message for some screens. I assume it is a matter of time before this warning message bug is resolved in a future version of the software.

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

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