IPython Notebook lets you do more than just program

bacho12345, 123RF.com

bacho12345, 123RF.com

Supernatural

The IPython Notebook environment offers much more than just the interactive execution of Python instructions. It can seamlessly integrate documents, programs, and tools with ease.

Python has always offered an interactive mode. The IPython Notebook environment constitutes an extremely multi-faceted and updated extension [1]. Besides many useful tools like direct shell commands, improved help functions, and runtime measurement for program parts, IPython Notebook connects source text with graphics, wiki-like texts, and everything that HTML5 has to offer in one interactive document. Users working with Python should not ignore the advantages of using Notebook.

Interactive Notebook

Entering python in the command line starts the so-called "Read-eval-print loop" (REPL) [2]. REPL delivers a result once a code line is entered. IPython, however, offers a much improved version of REPL. Although its origins are in the scientific community, many sys admins and developers should not do without IPython Notebook.

Using a shell has some disadvantages. In addition to the limitation of one entry per line, users struggle with the ephemeral nature of the input mechanism. The IPython Notebook does offer a command history that reaches across sessions so that, when restarting, the user can find all previously entered commands. Still, IPython Notebook is much more convenient because it saves all input. The output types can include text, HTML, images, and even GUI elements.

Getting Started

The ipython notebook command starts a new browser window when the IPython Notebook is installed (see the "Installation" box for details). You should select New and then Python 3 from the drop-down menu to open a new Notebook. Figure 1 shows the result.

Installation

The IPython Notebook works with Python2 and 3. There are several ways to install the software. If you use pip , then typing pip install "ipython[notebook]" will do the job.

If you would like to install other packages like NumPy or Pandas , then it is a good idea to use the Anaconda distribution. The guidelines for installation are described on the download site [5].

Of course, you can also use the package manager of your Linux distribution. Bear in mind though that your distribution will probably pack a significantly older version that may not support some of the features presented in this article.

Figure 1: A freshly installed Notebook.

The menus make for comfortable operation. The Kernel menu option indicates that Notebook is an active client, which is connected to a local server running on the machine. The software packages Tornado [3] and ZeroMQ [4] are at work in the background.

The first task is always to rename the file. Click on Untitled and then enter the name you have chosen in the window that opens. You will then find a <name>.ipynb file in the filesystem that contains all of the information about the Notebook in JSON format.

Markup with Markdown

The box with the gray background is ready for the input of Python commands, but you can also use it to enter formatted text.

To do this, you will need to change the box's type using the drop-down menu at the top: Change the option from Code to Markdown . Markdown is a very popular tool because it is simple and also because it lets you use HTML directly if necessary. There are three basic rules in using Markdown, which if followed, result in text with a very satisfactory appearance.

These three rules are:

  1. An empty line indicates a new paragraph.
  2. A * indicates a list element.
  3. #, ##, and ### indicate heading levels 1, 2, and 3.

Figure 2 shows an example of Markdown text. Additionally, you can use the Shift+Enter keys to enter a new line.

Figure 2: Displaying Markdown text in the IPython Notebook.

Python in the Browser

Python code is the core of the IPython Notebook. As with an interactive prompt, the result for an expression comes back when the command is submitted, here with Shift+Enter. The numbering for the input cell In[1]: corresponds with the result in Out[1]: and so on. Syntax coloring and automatic indentation are standard. Tab expansion and interactive help offer convenience. A question mark immediately following the name of the object you would like to know more about (e.g., sys? ) brings up the integrated documentation and some additional information as shown in Figure 3.

Figure 3: A question mark brings up Help for an object.

Making Magic

The IPython Notebook offers many so-called magic commands. They always begin with the % symbol. The %quickref command gives a quick overview, and Listing 1 shows an excerpt of the possibilities. Two important magic command representatives are the %ls and %less commands. The functionality of these commands corresponds to the shell commands of the same name. However, they are available independently of the operating system and consequently can also be used when working in Windows. If the magical powers are not strong enough, you can use ! to directly execute shell commands. Figure 4 shows examples.

Listing 1

IPython %quickref

IPython -- An enhanced Interactive Python - Quick Reference Card
================================================================
obj?, obj??      : Get help, or more help for object (also works as
                   ?obj, ??obj).
?foo.*abc*       : List names in 'foo' containing 'abc' in them.
%magic           : Information about IPython's 'magic' % functions.
Magic functions are prefixed by % or %%, and typically take
their arguments without parentheses, quotes or even commas for
convenience. Magical line commands take a single % and magical
cell commands are prefixed with two %%.
Example magic function calls:
%alias d ls -F   : 'd' is now an alias for 'ls -F'
alias d ls -F    : Works if 'alias' is not a Python name
alist = %alias   : Get list of aliases to 'alist'
cd /usr/share    : Obvious. Use cd -<tab> to choose from visited dirs.
%cd??            : See help AND source for magic %cd
%timeit x=10     : time the 'x=10' statement with high precision.
%%timeit x=2**100
x**100           : time 'x**100' with a setup of 'x=2**100';
setup code is not counted.  This is an example of a cell magic.
System commands:
!cp a.txt b/     : System command escape, calls os.system()
cp a.txt b/      : after %rehashx, most system commands work without !
cp ${f}.txt $bar : Variable expansion in magic and system commands
files = !ls /usr : Capture system command output
files.s, files.l, files.n: "a b c", ['a','b','c'], 'a\nb\nc'
History:
_i, _ii, _iii    : Previous, next previous, next next previous input
_i4, _ih[2:5]    : Input history line 4, lines 2-4
exec _i81        : Execute input history line #81 again
%rep 81          : Edit input history line #81
_, __, ___       : previous, next previous, next next previous output
_dh              : Directory history
_oh              : Output history
%hist            : Command history. '%hist -g foo' search history for 'foo'
Autocall:
f 1,2            : f(1,2)  # Off by default, enable with %autocall magic.
/f 1,2           : f(1,2) (forced automatic parenthesization)
,f 1 2           : f("1","2")
;f 1 2           : f("1 2")
Remember: Tab completion works in many contexts, not just file names
or Python names.
The following magic functions are currently available:
%alias:
    Define an alias for a system command.
%alias_magic:
    ::
%autocall:
    Make functions callable without having to type parentheses.
%automagic:
    Make magic functions callable without having to type the initial %.
%autosave:
    Set the autosave interval in the notebook (in seconds).
%bookmark:
    Manage IPython's bookmark system.
%cat:
    Callable object storing the details of one alias.
%cd:
    Change the current working directory.
%clear:
Figure 4: Complete access to the command line from IPython Notebook.

The return values can also be saved in Python variables. As a result, the t = !ls | grep -i ubuntu call saves the result of the grep command in t as a ['Ubuntu User iPython Demo.ipynb'] list.

Code Gallery

Python has a standard way of representing diagrams in the form of matplotlib [6]. IPython Notebook supports the direct output of images. The magic command %matplotlib inline turns on the required mode. Figure 5 shows the result. In this case, the image is only a static PNG file; however, several extensions make it possible to show interactive diagrams in the notebook. These extensions work with JavaScript to interactively enlarge excerpts.

Figure 5: Images are directly displayed in IPython Notebook.

The IPython Notebook environment can also display tables in an attractive way. Objects need only have a _repr_html_() method. IPython Notebook will invoke this method before the usually invoked method __repr__() . Figure 6 shows how a Pandas data frame, which has a _repr_html_() method, looks as a table. Pandas [7] is a commonly used library for fast and extremely convenient work with tabular data.

Figure 6: The table in HTML format comes from the DataFrame object.

Helping Hands

The list of attractive qualities for IPython Notebook is long. Highlights include the possibility for measuring runtime performance of program parts as well as profiling. Figure 7 shows how %timeit measures how long Python needs to execute a line. The %%timeit version works on all of the lines in a cell, as do all magic commands with two percent symbols. The %prun add(1, 1) command is used to start the Python profiler, cProfile , which shows what resources are consumed by each function.

Figure 7: Convenient measurements of runtime with one line of code.

If you would like to figure out how another version of Python or a completely different programming language might solve a particular task, you can stay inside the notebook. The %%python2 command turns a line into a Python2 program. This lets you try out Python2 code in a Python3 notebook. Of course, this also works in reverse. Additional languages such as Perl, LaTeX, JavaScript, and even Fortran are also ready to use when a cell is appropriately marked.

IPython Notebooks can be exported to static HTML pages and also to Python source text files. The accompanying converter supports many more formats. You can also create lecture slides with IPython Notebook. This is, of course, a very interesting exercise for slides prepared for a lecture on Python because you can directly test code examples and even have syntax coloring available as part of the presentation.

Outlook

IPython Notebook has supported other programming languages in addition to Python for a long time. The number of supported languages is about 30. All of the language independent parts of the project are named Jupyter. The IPython team is currently focused on building in new functionality. The main goals are a real-time collaboration capability for developers similar to Google Docs. Software creation of GUI input elements is also getting lots of attention. Based on the speedy rate at which previous development efforts have been completed, you can look forward to much new functionality in the next few years.

Conclusion

The IPython Notebook deserves the name "killer app." It has become the standard tool at scientific conferences for tutorials and increasingly also for lectures. Given its extreme versatility, IPython Notebook is also useful for anyone working with Python who is seeking a lot of payback in exchange for a brief training period.

The environment is highly suitable for trying out ideas. You get an immense number of benefits if you are looking to introduce programming concepts to other people. Last but not least, IPython Notebook makes it easy for a beginner to start programming. It is downright ideal for learning the basics of programming.