Widgets Demo

IPython widgets allow you to quickly and easily create interactive APIs with Python.

To run this notebook, you'll first have to install ipywidgets using, e.g.

$ conda install ipywidgets

You can find a full set of documentation notebooks here.

Troubleshooting

If the widgets below do not show up in the notebook, try closing the notebook and running the following command in your shell:

$ jupyter nbextension enable --py widgetsnbextension

Then open the notebook again. This enables the widgets notebook extension in the case that it's disabled.

interact: simple interactive widgets

The main idea of ipywidgets is to allow you to transform simple Python functions into interactive widgets. For example


In [ ]:
from ipywidgets import interact

In [ ]:
def times_ten(x):
    return 10 * x

In [ ]:
interact(times_ten, x=10);

In [ ]:
interact(times_ten, x='(^_^)')

In [ ]:
interact(times_ten, x=True)

Specifying Ranges

Using a tuple for the input, we can specify a range for our data:


In [ ]:
interact(times_ten, x=(100, 200))

Interact with Plotting

This can become very powerful when interacting with a plotting command:


In [ ]:
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 1000)

def plot_sine(amplitude, frequency, phase):
    y = amplitude * np.sin(frequency * x - phase)
    plt.plot(x, y)
    plt.ylim(-6, 6)
    
interact(plot_sine,
         amplitude=(0.0, 5.0),
         frequency=(0.1, 10),
         phase=(-5.0, 5.0));

More interesting example: the Lorenz System

Let's walk through an example from the IPython widgets documentation: the Lorenz System