Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Widgets

DS 2023 | Communicating with Data

Source

Overview

When exploring data, you often want to try different parameters — bin sizes, smoothing windows, color mappings — without having to enter these values and rerun cells by hand.

Graphical widgets let you expose those parameters as sliders, dropdowns, and checkboxes so you can adjust them continuously.

This is useful for two reasons.

  • First, it speeds up your own exploratory analysis.

  • Second, it lets you share a notebook where others can explore the data themselves without touching any code.

The Jupyter Widgets library ipywidgets provides a nice collection of widgets you can use to allow users to interact with your code.

The @interact decorator connects them to your functions in a quick way.

This notebook demonstrates the use of @interact.

Set up

To use Jupyter Widgets, import the ipywidgets library.

Here, we begin by just grabbing the display asset.

A simple example

Let’s begin by looking at a simple line plot from the flights dataset.

<Figure size 640x480 with 1 Axes>

As you know by now, there are many parameters you can adjust when creating plots.

Here we adjust figure size and the kind of plot.

<Figure size 600x400 with 1 Axes>

Widgets let you put these paramters into widgets that the user can enter directly into the notebook.

A simple way to do that is use interact.

The @interact is a decorator that inspects a function’s arguments and creates widgets automatically.

It infers the widget type from the default value: an integer becomes a slider, a list becomes a dropdown, a boolean becomes a checkbox.

You can also pass a tuple (min, max, step) to override the slider range for a numeric argument, or a list to force a dropdown.

Loading...
Loading...
Loading...

Interacting with other libraries

@interact works with any plotting library that renders output in a cell.

Indeed, it can work with anything that take parameters and produces and output.

Here we use the decorator with Plotly’s parallel_categories chart function on the Penguins dataset.

Loading...

Colormap viewer

A practical utility: browse all available matplotlib colormaps interactively. The second version filters out reversed variants (names ending in _r) and adds a checkbox to apply the reversal manually.

Loading...
Loading...

Updating EDA parameters

A common use case: tune distribution plots without rerunning cells. Here, a dropdown selects the column and sliders control the histogram bin count and KDE bandwidth.

Loading...

Labeling widgets

By default, @interact uses the argument name as the widget label. Pass an explicit widget constructor — such as widgets.IntSlider(description="Figure height") — to use a friendlier label.

You can also display and reuse a widget object independently of @interact.

Loading...
Loading...
Loading...
Loading...

Slowing down updates

By default, @interact re-runs the function on every widget change.

You can use @interact_manual to add a “Run” button instead — useful when the computation is slow.

Loading...

Selecting axes

A common EDA pattern is to let the user pick which columns map to x, y, and hue.

Loading...

Freezing parameters with fixed()

Wrap a value in fixed() to prevent @interact from creating a widget for it.

The function still receives the value — it just isn’t exposed to the user.

Loading...