IPython Documentation

Table Of Contents

Previous topic

Module: html.utils

Next topic

Module: html.widgets.trait_types

This Page

Module: html.widgets.interaction

Interact with functions using widgets.

1 Class

class IPython.html.widgets.interaction.fixed(value, **kwargs)

Bases: IPython.utils.traitlets.HasTraits

A pseudo-widget whose value is fixed and never synced to the client.

__init__(value, **kwargs)

3 Functions

IPython.html.widgets.interaction.interactive(__interact_f, **kwargs)

Builds a group of interactive widgets tied to a function and places the group into a Box container.

Parameters:

__interact_f : function

The function to which the interactive widgets are tied. The **kwargs should match the function signature.

**kwargs : various, optional

An interactive widget is created for each keyword argument that is a valid widget abbreviation.

Returns:

container : a Box instance containing multiple widgets

IPython.html.widgets.interaction.interact(__interact_f=None, **kwargs)

Displays interactive widgets which are tied to a function. Expects the first argument to be a function. Parameters to this function are widget abbreviations passed in as keyword arguments (**kwargs). Can be used as a decorator (see examples).

Parameters:

__interact_f : function

The function to which the interactive widgets are tied. The **kwargs should match the function signature. Passed to interactive()

**kwargs : various, optional

An interactive widget is created for each keyword argument that is a valid widget abbreviation. Passed to interactive()

Returns:

f : __interact_f with interactive widget attached to it.

Examples

Render an interactive text field that shows the greeting with the passed in text:

# 1. Using interact as a function
def greeting(text="World"):
    print "Hello {}".format(text)
interact(greeting, text="IPython Widgets")

# 2. Using interact as a decorator
@interact
def greeting(text="World"):
    print "Hello {}".format(text)

# 3. Using interact as a decorator with named parameters
@interact(text="IPython Widgets")
def greeting(text="World"):
    print "Hello {}".format(text)

Render an interactive slider widget and prints square of number:

# 1. Using interact as a function
def square(num=1):
    print "{} squared is {}".format(num, num*num)
interact(square, num=5)

# 2. Using interact as a decorator
@interact
def square(num=2):
    print "{} squared is {}".format(num, num*num)

# 3. Using interact as a decorator with named parameters
@interact(num=5)
def square(num=2):
    print "{} squared is {}".format(num, num*num)
IPython.html.widgets.interaction.interact_manual(f, **kwargs)

As interact(), generates widgets for each argument, but rather than running the function after each widget change, adds a “Run” button and waits for it to be clicked. Useful if the function is long-running and has several parameters to change.