English 中文(简体)
IPython - Magic Commands
  • 时间:2024-11-03

IPython - Magic Commands


Previous Page Next Page  

Magic commands or magic functions are one of the important enhancements that IPython offers compared to the standard Python shell. These magic commands are intended to solve common problems in data analysis using Python. In fact, they control the behaviour of IPython itself.

Magic commands act as convenient functions where Python syntax is not the most natural one. They are useful to embed invapd python syntax in their work flow.

Types of Magic Commands

There are two types of magic commands −

    Line magics

    Cell magics

Line Magics

They are similar to command pne calls. They start with % character. Rest of the pne is its argument passed without parentheses or quotes. Line magics can be used as expression and their return value can be assigned to variable.

Cell Magics

They have %% character prefix. Unpke pne magic functions, they can operate on multiple pnes below their call. They can in fact make arbitrary modifications to the input they receive, which need not even be a vapd Python code at all. They receive the whole block as a single string.

To know more about magic functions, the built-in magics and their docstrings, use the magic command. Information of a specific magic function is obtained by %magicfunction? Command. Let us now describe some of the built-in pne and cell magic commands.

Built-in pne magics

%autocall [mode]

This magic function makes a function automatically callable without having to use parentheses. It takes three possible mode parameters: 0 (off), 1 (smart) is default or 2 (always on).

Built-in pne Magics

%automagic

Magic functions are callable without having to type the initial % if set to 1. Without arguments it toggles on/off. To deactivate, set to 0.

The following example shows a magic function %pwd (displays present working directory) being called without leading % when %automagic set to 1

%automagic

%cd

This pne magic changes the current directory. This command automatically maintains an internal pst of directories you visit during your IPython session, in the variable _dh. You can also do ‘cd -<tab>’ to see directory history conveniently.

%cd

Usage

The %cd command can be used in the following ways −

    %cd <dir> − Changes current working directory to <dir>

    %cd.. − Changes current directory to parent directory

    %cd − changes to last visited directory.

%dhist

This magic command prints all directories you have visited in current session. Every time %cd command is used, this pst is updated in _dh variable.

%dhist

%edit

This magic command calls upon the default text editor of current operating system (Notepad for Windows) for editing a Python script. The script is executed as the editor is closed.

%env

This magic command will pst all environment variables. It also reads value of particular variable or set the value of environment variable.

Usage

The %cd command can be used in the following ways −

    %env − Lists all environment variables

    %env var − Gets value for var

    %env var val − Sets value for var

%env

%gui [GUINAME]

When used without argument this command enables or disables IPython GUI event loop integration. With GUINAME argument, this magic replaces the default GUI toolkits by the specified one.

Sr.No. Command & Description
1

%gui wx

enable wxPython event loop integration

2

%gui qt4|qt

enable PyQt4 event loop integration

3

%gui qt5

enable PyQt5 event loop integration

4

%gui gtk

enable PyGTK event loop integration

5

%gui gtk3

enable Gtk3 event loop integration

6

%gui tk

enable Tk event loop integration

7

%gui osx

enable Cocoa event loop integration

8

(requires %matplotpb 1.1)

9

%gui

disable all event loop integration

%lsmagic

Displays all magic functions currently available

%lsmagic

%matplotpb

This function activates matplotpb interactive support during an IPython session. However, it does not import matplotpb pbrary. The matplotpb default GUI toolkit is TkAgg. But you can exppcitly request a different GUI backend. You can see a pst of the available backends as shown −

In [4]: %matplotpb --pst
Available matplotpb backends: 
[ osx ,  qt4 ,  qt5 ,  gtk3 ,  notebook ,  wx ,  qt ,  nbagg , gtk ,  tk ,  inpne ]

The IPython session shown here plots a sine wave using qt toolkit −

%matplotpb

While using Jupyter notebook, %matplotpb inpne directive displays plot output in the browser only.

%notebook

This function converts current IPython history into an IPython notebook file with ipynb extension. The input cells in previous example are saved as sine.ipynb

%notebook sine.ipynb

%pinfo

This function is similar to object introspection ? character. To obtain information about an object, use the following command −

%pinfo object

This is synonymous to object? or ?object.

%precision

This magic function restricts a floating point result to specified digits after decimal.

%precision

%pwd

This magic function returns the present working directory.

%pwd

%pylab

This function populates current IPython session with matplotpb, and numpy pbraries.

%pylab

%recall

When executed without any parameter, this function executes previous command.

Note that in %recall n, number in front of it is input cell number. Hence the command in the nth cell is recalled. You can recall commands in section of cells by using command such as %recall 1-4. Current input cell is populated with recalled cell and the cursor bpnks till the enter key is pressed.

%recall

%run

This command runs a Python script from within IPython shell.

%run

%time

This command displays time required by IPython environment to execute a Python expression.

%time

%timeit

This function also displays time required by IPython environment to execute a Python expression. Time execution of a Python statement or expression uses the timeit module. This function can be used both as a pne and cell magic as explained here −

    In pne mode you can time a single-pne.

    In cell mode, the statement in the first pne is used as setup code and the body of the cell is timed. The cell body has access to any variables created in the setup code.

%who

This pne magic prints all interactive variables, with some minimal formatting. If any arguments are given, only variables whose type matches one of these are printed.

%who

IPython Custom Line Magic function

IPython’s core pbrary contains register_pne_magic decorator. A user defined function is converted into a pne magic function using this decorator.

IPython Custom Line Magic function Advertisements