from cStringIO import StringIO from IPython.display import Image def figure2Image(fig): buffer = StringIO() fig.savefig(buffer, format='png') buffer.reset() data = buffer.read() return Image(data) class Slider(object): def __init__(self, python_var, callback, min=1, max=100, step=1): self._min = min self._max = max self._step = step self._value_init = (min+max)/2 self._id = "slider" + str(id(self)) self._python_var = python_var self._callback = callback def update(self, value): return self._callback(value) def _repr_html_(self): javascript = """ \ """ % {"id": self._id, "python_var": self._python_var} html = """ \ %(min)s %(max)s
%(python_var)s = %(value_init)s
Out:

""" % {"id": self._id, "min": self._min, "max": self._max, "step": self._step, "value_init": self._value_init, "python_var": self._python_var} return javascript + html from pylab import plot, figure from numpy import linspace, pi, sin x = linspace(0, 2*pi, 1000) def update(value): fig = figure(figsize=(4, 2)) k = value / 50. plot(x, sin(k*x)) return figure2Image(fig) s = Slider("s", update) s