Reader small image

You're reading from  Learning Jupyter

Product typeBook
Published inNov 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781785884870
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Dan Toomey
Dan Toomey
author image
Dan Toomey

Dan Toomey has been developing application software for over 20 years. He has worked in a variety of industries and companies, in roles from sole contributor to VP/CTO-level. For the last few years, he has been contracting for companies in the eastern Massachusetts area. Dan has been contracting under Dan Toomey Software Corp. Dan has also written R for Data Science, Jupyter for Data Sciences, and the Jupyter Cookbook, all with Packt.
Read more about Dan Toomey

Right arrow

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...

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:

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.

Widget basics


All widgets work the same, generally:

  • You create or define an instance of a widget.

  • You can preset attributes of a widget, such as its initial value or a label to be displayed.

  • Widgets can react to different inputs from a user. The inputs are gathered by a handler or a Python function that is invoked when a user performs some action on a widget; for example, a function could be set up to call the handler if the user clicks on a button.

  • The value of a widget can be used later in your script in the same way as any other variable; for example, you could use a widget to determine how many circles to draw.

Interact widget


Interact is the basic widget that is often used to derive all other widgets. It has variable arguments, and, depending on these arguments, can affect many different variations of user input control.

Interact widget slider

We can use interact to produce a slider by passing in an extent. Take the following script:

from ipywidgets import interact
# define a function to work with (cubes the number)
def myfunction(arg):
    return arg+1
interact(myfunction, arg=9);

Here, we have a script that does the following:

  • References the package we want to use

  • Defines a function (which is called for every user input of a value)

  • Calls out to interact, passing our handler and a range of values

When we run this script, we get a scrollbar that is modifiable by the user:

The user is able to slide the vertical bar over the range of values. The upper end is 27 and the lower end is -9 (assume we could pass additional arguments to interact to set the range of values that are selectable). myfunction is...

Interactive widget


There is also an interactive widget. The interactive widget works like the interact widget, but does not display the user input control until called upon directly by the script. This would be useful if you had some calculations that had to be performed for the parameters of the widget display, or even if you wanted to decide whether you needed a control at runtime.

For example, we could have the following script (similar to the preceding script):

from ipywidgets import interactive
def myfunction(x):
    return x
w = interactive(myfunction, x= "Hello World ");
from IPython.display import display
display(w)

There are a couple of changes to the script:

  • We are referencing the interactive widget

  • The interactive function returns a widget, rather than immediately displaying a value

  • We must script the display of the widget ourselves

If we run this script, it looks very similar to the result of the previous script:

Widgets


There is another package of widgets, called ipywidgets, that has all of the standard controls that you might want to use, with many optional parameters available to customize your display.

Progress bar widget

One of the widgets available in this package displays a progress bar to the user. Take the following script:

import ipywidgets as widgets
widgets.FloatProgress(
    value=45,
    min=0,
    max=100,
    step=5,
    description='Percent:',
)

This would display our progress bar as shown here:

Listbox widget

We could also use the list box widget, called as Dropdown, in this script:

import ipywidgets as widgets
from IPython.display import display
w = widgets.Dropdown(
    options={'Pen': 7732, 'Pencil': 102, 'Pad': 33331},
    description='Item:',
)
display(w)
w.value

This script will display a list box to the user with the displayed values of Pen, Pencil, and Pad. When the user selects one of the values, the associated value is returned to the w variable, which we display:

Text...

Summary


In this chapter, we added widgets to our Jupyter installation and used the interact and interactive widgets to produce a variety of user input controls. We then looked in the widgets package in depth to examine some of the user controls available, the properties available in the containers, the events that are available emitting from the controls, and to work out how to build containers of controls.

In the next chapter, we will learn about sharing notebooks with other users. The typical method for sharing a notebook is to post it on a website so anyone can access the notebook. We will also cover how to convert notebooks to different formats. This allows users who don't have direct access to your notebook to see the effects.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning Jupyter
Published in: Nov 2016Publisher: PacktISBN-13: 9781785884870
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dan Toomey

Dan Toomey has been developing application software for over 20 years. He has worked in a variety of industries and companies, in roles from sole contributor to VP/CTO-level. For the last few years, he has been contracting for companies in the eastern Massachusetts area. Dan has been contracting under Dan Toomey Software Corp. Dan has also written R for Data Science, Jupyter for Data Sciences, and the Jupyter Cookbook, all with Packt.
Read more about Dan Toomey