hooks

Module: hooks

Inheritance diagram for IPython.hooks:

hooks for IPython.

In Python, it is possible to overwrite any method of any object if you really want to. But IPython exposes a few ‘hooks’, methods which are _designed_ to be overwritten by users for customization purposes. This module defines the default versions of all such hooks, which get used by IPython if not overridden by the user.

hooks are simple functions, but they should be declared with ‘self’ as their first argument, because when activated they are registered into IPython as instance methods. The self argument will be the IPython running instance itself, so hooks have full access to the entire IPython object.

If you wish to define a new hook and activate it, you need to put the necessary code into a python file which can be either imported or execfile()’d from within your ipythonrc configuration.

For example, suppose that you have a module called ‘myiphooks’ in your PYTHONPATH, which contains the following definition:

import os import IPython.ipapi ip = IPython.ipapi.get()

def calljed(self,filename, linenum):

“My editor hook calls the jed editor directly.” print “Calling my own editor, jed ...” if os.system(‘jed +%d %s’ % (linenum,filename)) != 0:

raise ipapi.TryNext()

ip.set_hook(‘editor’, calljed)

You can then enable the functionality by doing ‘import myiphooks’ somewhere in your configuration files or ipython command line.

Class

CommandChainDispatcher

class IPython.hooks.CommandChainDispatcher(commands=None)

Dispatch calls to a chain of commands until some func can handle it

Usage: instantiate, execute “add” to add commands (with optional priority), execute normally via f() calling mechanism.

__init__(commands=None)
add(func, priority=0)
Add a func to the cmd chain with given priority

Functions

IPython.hooks.clipboard_get(self)
Get text from the clipboard.
IPython.hooks.editor(self, filename, linenum=None)

Open the default editor at the given filename and linenumber.

This is IPython’s default editor hook, you can use it as an example to write your own modified one. To set your own editor function as the new editor hook, call ip.set_hook(‘editor’,yourfunc).

IPython.hooks.fix_error_editor(self, filename, linenum, column, msg)

Open the editor at the given filename, linenumber, column and show an error message. This is used for correcting syntax errors. The current implementation only has special support for the VIM editor, and falls back on the ‘editor’ hook if VIM is not used.

Call ip.set_hook(‘fix_error_editor’,youfunc) to use your own function,

IPython.hooks.generate_output_prompt(self)
IPython.hooks.generate_prompt(self, is_continuation)
calculate and return a string with the prompt to display
IPython.hooks.input_prefilter(self, line)

Default input prefilter

This returns the line as unchanged, so that the interpreter knows that nothing was done and proceeds with “classic” prefiltering (%magics, !shell commands etc.).

Note that leading whitespace is not passed to this hook. Prefilter can’t alter indentation.

IPython.hooks.late_startup_hook(self)
Executed after ipython has been constructed and configured
IPython.hooks.pre_prompt_hook(self)

Run before displaying the next prompt

Use this e.g. to display output from asynchronous operations (in order to not mess up text entry)

IPython.hooks.pre_runcode_hook(self)
Executed before running the (prefiltered) code in IPython
IPython.hooks.result_display(self, arg)

Default display hook.

Called for displaying the result to the user.

IPython.hooks.shell_hook(self, cmd)
Run system/shell command a’la os.system()
IPython.hooks.show_in_pager(self, s)
Run a string through pager
IPython.hooks.shutdown_hook(self)

default shutdown hook

Typically, shotdown hooks should raise TryNext so all shutdown ops are done

IPython.hooks.synchronize_with_editor(self, filename, linenum, column)