Warning

This documentation is for an old version of IPython. You can find docs for newer versions here.

Module: core.magics.basic

Implementation of basic magic functions.

2 Classes

class IPython.core.magics.basic.MagicsDisplay(magics_manager)

Bases: object

__init__(magics_manager)
class IPython.core.magics.basic.BasicMagics(shell=None, **kwargs)

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(line='')
%alias_magic [-l] [-c] name target

Create an alias for an existing line or cell magic.

Examples

In [1]: %alias_magic t timeit
Created `%t` as an alias for `%timeit`.
Created `%%t` as an alias for `%%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
Created `%whereami` as an alias for `%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.
colors(parameter_s='')

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

Currently implemented schemes: NoColor, Linux, LightBG.

Color scheme names are not case-sensitive.

Examples

To get a plain black and white terminal:

%colors nocolor
doctest_mode(parameter_s='')

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.

gui(parameter_s='')

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.

lsmagic(parameter_s='')

List currently available magic functions.

magic(parameter_s='')

Print information about the magic function system.

Supported formats: -latex, -brief, -rest

notebook(s)
%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 “json”. Likewise using a ”.py” file extension will write the notebook as a Python script
-f FORMAT, --format FORMAT
 Convert an existing IPython notebook to a new format. This option specifies the new format and can have the values: json, py. The target filename is chosen automatically based on the new format. The filename argument gives the name of the source file.
page(parameter_s='')

Pretty print the object and display it through a pager.

%page [options] OBJECT

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

Options:

-r: page str(object), don’t pretty-print it.
pprint(parameter_s='')

Toggle pretty printing on/off.

precision(s='')

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.

Examples

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
profile(parameter_s='')

Print your currently active IPython profile.

See also

prun
run code using the Python profiler (prun())
quickref(arg)

Show a quick reference sheet

xmode(parameter_s='')

Switch modes for the exception handlers.

Valid modes: Plain, Context and Verbose.

If called without arguments, acts as a toggle.