|Did you know...?|
LWN.net is a subscriber-supported publication; we rely on subscribers to keep the entire operation going. Please help out by buying a subscription and keeping LWN on the net.
Many different languages have a read-eval-print loop (REPL) for interactive use, but almost inevitably the REPL implementation is too simple for some people. Features like history retrieval and editing, incorporating graphics for data visualization, adding annotation capabilities, and so on, are often things that users want and, thus, a part of any replacement. IPython, or "interactive Python", is a popular substitute for the simple, text-only REPL provided by the standard C Python interpreter. It can also be used in combination with the IPython Notebook to put IPython "documents" into interactive web pages.
The original target for IPython was data visualization, but it has been used for much more than that. It is one of the primary teaching tools used in various informal Python tutorials and explanations found on the web, and it is not uncommon to see conference talks that are accompanied by IPython notebooks. IPython is even included as an interactive shell on the newly redesigned Python home page (look for the ">_" icon).
IPython is similar, in some ways, to commercial numerical computing packages like Mathematica and MATLAB. It also takes on some of the roles of an integrated development environment (IDE) by providing syntax highlighting, tab completion, popping up function API references, and so on. In addition, it provides direct access to Python's "help" functionality with the "help()" (or help(object)) command. But that is just a tiny taste of what IPython does.
To start with, it provides extensive command-line editing features that work especially well with Python's idiosyncratic indentation rules. Pasting code into IPython works well, which is not something that can be said for the standard REPL. To use it, one must learn the difference between Shift-ENTER (execute the code being edited) and ENTER (add a newline at the cursor). That takes a little getting used to, but IPython makes editing Python much easier.
All of the input and output is saved and numbered, so that it can be recalled and manipulated in various ways. The "magic" commands (which start with "%", such as %hist for history) provide extra features outside of Python to do things like load and save files, create aliases and macros, time execution of Python code, call out to the system shell, set up graphics consoles, etc. The %quickref command is one of the more useful when getting started with IPython, though the "?" command that gives a good overview of the features of the program is indispensable in the early going.
The program can be invoked in a number of different ways. For a text-based console in the current terminal window, a simple ipython will do, but the Qt-based console (with menus and tabs for multiple workspaces) can be invoked with the --qt-console command-line switch. The workspaces can either be connected to the same running Python instance (or "kernel") or can have their own. There are also some command-line parameters to control the loading of commonly used libraries. The --matplotlib[=qt4] will preload the matplotlib library (with an optional Qt 4 backend), while the --pylab option loads numpy in addition to matplotlib. The --pylab option is semi-deprecated because it loads numpy into the root namespace (though it can still be useful for quick-and-dirty scratch sessions).
The preloading options clearly show the data visualization roots of IPython, but the program is not limited to numeric plotting by any means. There many kinds of output that it can handle, including image formats, SVG, HTML, audio, video, and LaTeX. While any of that can be done within the console, it is the notebooks that show off IPython output best.
For example, the link in the previous paragraph goes to a notebook created to describe IPython Notebook output types. It uses the IPython Notebook Viewer (often abbreviated as "nbviewer") to display the contents of the notebook, which were created using IPython. Notebooks can be viewed with nbviewer, but interacting with them is done using notebook mode (which we will get to momentarily) locally. There are also extensions for Chrome, Firefox, and Safari that will add an nbviewer icon to the toolbar for even faster access.
There are a number of example notebooks shown on the nbviewer page, ranging from IPython, IRuby, and IJulia introductions to full-on textbooks, as well as notebooks covering the analysis of mathematical functions, an introduction to pandas Python-based data analysis tool, and so on. There are even more notebooks listed in the gallery.
Notebook mode is invoked by running "ipython notebook", which starts a web server process running on the local machine and opens a browser tab to http://127.0.0.1:8888/ to access it. From there, one can create, edit, or interact with an IPython notebook. An existing notebook can be loaded from the local system or from the web, and edited using the same techniques (and keystrokes, commands, etc.) as the console version uses. The notebook can then be saved and shared with others.
As alluded to above, IPython is not restricted to the Python language. Though Python is the only "official" kernel, there are active projects for Julia, Haskell, Ruby, R, and others. The IPython kernel will support multiple languages, as well, so those languages can be mixed and matched as needed.
The stable version is 1.2.1 from February, but a 2.0 release is imminent. The first beta of 2.0 was released on March 7. Development is being funded in large part by a $1.15 million Sloan Foundation grant, though it should also be noted that Microsoft donated $100,000 toward IPython development in August 2013. There is a rather aggressive roadmap for future releases, with lots of new features.
We have only scratched the surface of what IPython is and can do in this look. It is an impressive tool that is worth a look for data visualization, data analysis, and a whole host of other applications. Moving to it permanently instead of the standard REPL seems like a no-brainer for anyone doing anything particularly complicated in that environment.
Copyright © 2014, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds