Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPython: Manipulate-like command

Tags:

In Wolfram Mathematica, I can interactively modify the value of a parameter by using the Manipulate[] command.

For example, Manipulate[n, {n, 1, 20}] shows a slider through which is possible to vary the value of n.

Is there any simple way (i.e. something like a magic or a decorator, like in SAGE) to achieve the same result in the IPython notebook?

like image 871
lampo808 Avatar asked Nov 01 '13 17:11

lampo808


People also ask

What is %% capture in Python?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

How do you use %% Timeit in jupyter?

The “%timeit” is a line magic command in which the code consists of a single line or should be written in the same line for measuring the execution time. In the “%timeit” command, the particular code is specified after the “%timeit” is separated by a space.

What are magic commands in IPython?

The magic commands, or magics, are handy commands built into the IPython kernel that make it easy to perform particular tasks, for example, interacting Python's capabilities with the operating system, another programming language, or a kernel. IPython provides two categories of magics: line magics and cell magics.


2 Answers

Update
This was added in IPython 2.0 (released Apr 1, 2014), it's called Interactive Widgets and works in web notebooks.

Original answer
This is ongoing work for 2.0 (release December something-ish) Have a look at the IPython-dev meeting on YouTube to see progress. The last meeting from oct 21 at 28min-ish has a widget demo by John then interact by Brian.

like image 175
Matt Avatar answered Sep 22 '22 05:09

Matt


I am not sure whether this would satisfy all your needs as it is still experimental, but seems to do what you asked for - look at static interactive widgets by Jake VanderPlas.

What I did to get running was the following:

Get the source and install:

git clone https://github.com/jakevdp/ipywidgets
pip install ./ipywidgets

start an ipython notebook and experiment with the notebook example.ipynb in the ipywidgets directory

There's another type of interactive visualisation, where panning, zoom and meta-data are available (possibly more). It is by the same author and reported in his blog D3 Plugins: Truly Interactive Matplotlib In Your Browser.

There are also nice docs: MPLD3: Bringing Matplotlib to the Browser

To experiment with it I did more or less the same:

git clone https://github.com/jakevdp/mpld3
pip install ./mpld3/

One can now run the create_example.py script, or even better, start an ipython notebook and play with the provided mpld3/notebooks/mpld3_demo.ipynb as well as with the attractive examples of mpld3_plugins posted in the blog.

It may be worth noting that I am using the Anaconda distro of python, which includes Jinja2; as far as I know Jinja2 is a requirement for mpld3.

I do agree that an @interact decorator a la sage would be nice.

like image 35
eldad-a Avatar answered Sep 23 '22 05:09

eldad-a