IPython Documentation

Table Of Contents

Previous topic


Next topic


This Page


Module: core.magics.basic

Inheritance diagram for IPython.core.magics.basic:

Implementation of basic magic functions.


class IPython.core.magics.basic.BasicMagics(shell)

Bases: IPython.core.magic.Magics

Magics that provide central IPython functionality.

These are various magics that don’t fit into specific categories but that are all part of the base ‘IPython experience’.


%alias_magic [-l] [-c] name target

Create an alias for an existing line or cell magic.



In [1]: %alias_magic t timeit

In [2]: %t -n1 pass 1 loops, best of 3: 954 ns per loop

In [3]: %%t -n1
...: pass ...:

1 loops, best of 3: 954 ns per loop

In [4]: %alias_magic –cell whereami pwd UsageError: Cell magic function %%pwd not found. In [5]: %alias_magic –line whereami pwd

In [6]: %whereami Out[6]: u’/home/testuser’

positional arguments:
name Name of the magic to be created. target Name of the existing line or cell magic.
optional arguments:
-l, --line Create a line magic alias.
-c, --cell Create a cell magic alias.

Print docstring if incorrect arguments were passed


Switch color scheme for prompts, info system and exception handlers.

Currently implemented schemes: NoColor, Linux, LightBG.

Color scheme names are not case-sensitive.


To get a plain black and white terminal:

%colors nocolor
default_option(fn, optstr)

Make an entry in the options_table for fn, with value optstr


Toggle doctest mode on and off.

This mode is intended to make IPython behave as much as possible like a plain Python shell, from the perspective of how its prompts, exceptions and output look. This makes it easy to copy and paste parts of a session into doctests. It does so by:

  • Changing the prompts to the classic >>> ones.
  • Changing the exception reporting mode to ‘Plain’.
  • Disabling pretty-printing of output.

Note that IPython also supports the pasting of code snippets that have leading ‘>>>’ and ‘...’ prompts in them. This means that you can paste doctests from files or docstrings (even if they have leading whitespace), and the code will execute correctly. You can then use ‘%history -t’ to see the translated history; this will give you the input after removal of all the leading prompts and whitespace, which can be pasted back into an editor.

With these features, you can switch into this mode easily whenever you need to do testing and changes to doctests, without having to leave your existing IPython session.


Format a string for latex inclusion.


Enable or disable IPython GUI event loop integration.

%gui [GUINAME]

This magic replaces IPython’s threaded shells that were activated using the (pylab/wthread/etc.) command line flags. GUI toolkits can now be enabled at runtime and keyboard interrupts should work without any problems. The following toolkits are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX):

%gui wx      # enable wxPython event loop integration
%gui qt4|qt  # enable PyQt4 event loop integration
%gui gtk     # enable PyGTK event loop integration
%gui gtk3    # enable Gtk3 event loop integration
%gui tk      # enable Tk event loop integration
%gui osx     # enable Cocoa event loop integration
             # (requires %matplotlib 1.1)
%gui         # disable all event loop integration

WARNING: after any of these has been called you can simply create an application object, but DO NOT start the event loop yourself, as we have already handled that.


List currently available magic functions.


Print information about the magic function system.

Supported formats: -latex, -brief, -rest

magics = {'cell': {}, 'line': {'profile': 'profile', 'doctest_mode': 'doctest_mode', 'lsmagic': 'lsmagic', 'magic': 'magic', 'gui': 'gui', 'pprint': 'pprint', 'xmode': 'xmode', 'precision': 'precision', 'notebook': 'notebook', 'quickref': 'quickref', 'colors': 'colors', 'alias_magic': 'alias_magic', 'page': 'page'}}

%notebook [-e] [-f FORMAT] filename

Export and convert IPython notebooks.

This function can export the current IPython history to a notebook file or can convert an existing notebook file into a different format. For example, to export the history to “foo.ipynb” do “%notebook -e foo.ipynb”. To export the history to “foo.py” do “%notebook -e foo.py”. To convert “foo.ipynb” to “foo.json” do “%notebook -f json foo.ipynb”. Possible formats include (json/ipynb, py).

positional arguments:
filename Notebook name or filename
optional arguments:
-e, --export Export IPython history as a notebook. The filename argument is used to specify the notebook name and format. For example a filename of notebook.ipynb will result in a notebook name of “notebook” and a format of “xml”. Likewise using a ”.json” or ”.py” file extension will write the notebook in the json or py formats.
-f FORMAT, --format FORMAT
 Convert an existing IPython notebook to a new format. This option specifies the new format and can have the values: xml, json, py. The target filename is chosen automatically based on the new format. The filename argument gives the name of the source file.
options_table = None

Pretty print the object and display it through a pager.

%page [options] OBJECT

If no object is given, use _ (last output).


-r: page str(object), don’t pretty-print it.
parse_options(arg_str, opt_str, *long_opts, **kw)

Parse options passed to an argument string.

The interface is similar to that of getopt(), but it returns back a Struct with the options as keys and the stripped argument string still as a string.

arg_str is quoted as a true sys.argv vector by using shlex.split. This allows us to easily expand variables, glob files, quote arguments, etc.


-mode: default ‘string’. If given as ‘list’, the argument string is returned as a list (split on whitespace) instead of a string.

-list_all: put all option values in lists. Normally only options appearing more than once are put in a list.

-posix (True): whether to split the input line in POSIX mode or not, as per the conventions outlined in the shlex module from the standard library.


Toggle pretty printing on/off.


Set floating point precision for pretty printing.

Can set either integer precision or a format string.

If numpy has been imported and precision is an int, numpy display precision will also be set, via numpy.set_printoptions.

If no argument is given, defaults will be restored.


In [1]: from math import pi

In [2]: %precision 3
Out[2]: u'%.3f'

In [3]: pi
Out[3]: 3.142

In [4]: %precision %i
Out[4]: u'%i'

In [5]: pi
Out[5]: 3

In [6]: %precision %e
Out[6]: u'%e'

In [7]: pi**10
Out[7]: 9.364805e+04

In [8]: %precision
Out[8]: u'%r'

In [9]: pi**10
Out[9]: 93648.047476082982

Print your currently active IPython profile.


Show a quick reference sheet

registered = True
shell = None

Switch modes for the exception handlers.

Valid modes: Plain, Context and Verbose.

If called without arguments, acts as a toggle.