Demo of interactive widgets in IPython.

For more information and examples, see:

http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Interactive%20Widgets/Index.ipynb

Using widgets requires IPython 2.0, see http://ipython.org/install.html for installation advice.

(Not available on SageMathCloud yet.)


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display

An example function to plot $\sin(2\pi kx)$ as a function of $x$ for given wave number $k$.


In [3]:
def plot_sine(k):
    x = linspace(-2,2,1000)
    plot(x,sin(2*pi*k*x))

For one particular $k$:


In [4]:
plot_sine(2)


Make it interactive with a slider bar for $k$ between 0 and 5 by steps of 0.1:


In [5]:
w = interactive(plot_sine, k=(0,5,0.1))
display(w)


An example with SymPy:


In [6]:
import sympy as S
S.init_printing() 
x = S.symbols('x')

In [7]:
def expand_power(n):
    f = (x + 1)**n
    f2 = S.expand(f)
    display(f2)

In [8]:
expand_power(3)


$$x^{3} + 3 x^{2} + 3 x + 1$$

In [9]:
w = interactive(expand_power, n=(1,10,1))
display(w)


$$x^{5} + 5 x^{4} + 10 x^{3} + 10 x^{2} + 5 x + 1$$