IPython Documentation

Table Of Contents

Previous topic

Module: nbconvert.utils.exceptions

Next topic

Module: nbconvert.utils.pandoc

This Page

Note

This documentation is for a development version of IPython. There may be significant differences from the latest stable release.

Module: nbconvert.utils.lexers

Defines a variety of Pygments lexers for highlighting IPython code.

This includes:

IPythonLexer, IPython3Lexer
Lexers for pure IPython (python + magic/shell commands)
IPythonPartialTracebackLexer, IPythonTracebackLexer
Supports 2.x and 3.x via keyword python3. The partial traceback lexer reads everything but the Python code appearing in a traceback. The full lexer combines the partial lexer with an IPython lexer.
IPythonConsoleLexer
A lexer for IPython console sessions, with support for tracebacks.
IPyLexer
A friendly lexer which examines the first line of text and from it, decides whether to use an IPython lexer or an IPython console lexer. This is probably the only lexer that needs to be explicitly added to Pygments.

4 Classes

class IPython.nbconvert.utils.lexers.IPythonPartialTracebackLexer(**options)

Bases: pygments.lexer.RegexLexer

Partial lexer for IPython tracebacks.

Handles all the non-python output. This works for both Python 2.x and 3.x.

class IPython.nbconvert.utils.lexers.IPythonTracebackLexer(**options)

Bases: pygments.lexer.DelegatingLexer

IPython traceback lexer.

For doctests, the tracebacks can be snipped as much as desired with the exception to the lines that designate a traceback. For non-syntax error tracebacks, this is the line of hyphens. For syntax error tracebacks, this is the line which lists the File and line number.

__init__(**options)
class IPython.nbconvert.utils.lexers.IPythonConsoleLexer(**options)

Bases: pygments.lexer.Lexer

An IPython console lexer for IPython code-blocks and doctests, such as:

.. code-block:: ipythonconsole

    In [1]: a = 'foo'

    In [2]: a
    Out[2]: 'foo'

    In [3]: print a
    foo

    In [4]: 1 / 0

Support is also provided for IPython exceptions:

.. code-block:: ipythonconsole

    In [1]: raise Exception

    ---------------------------------------------------------------------------
    Exception                                 Traceback (most recent call last)
    <ipython-input-1-fca2ab0ca76b> in <module>()
    ----> 1 raise Exception

    Exception:
__init__(**options)

Initialize the IPython console lexer.

Parameters:

python3 : bool

If True, then the console inputs are parsed using a Python 3 lexer. Otherwise, they are parsed using a Python 2 lexer.

in1_regex : RegexObject

The compiled regular expression used to detect the start of inputs. Although the IPython configuration setting may have a trailing whitespace, do not include it in the regex. If None, then the default input prompt is assumed.

in2_regex : RegexObject

The compiled regular expression used to detect the continuation of inputs. Although the IPython configuration setting may have a trailing whitespace, do not include it in the regex. If None, then the default input prompt is assumed.

out_regex : RegexObject

The compiled regular expression used to detect outputs. If None, then the default output prompt is assumed.

buffered_tokens()

Generator of unprocessed tokens after doing insertions and before changing to a new state.

get_mci(line)

Parses the line and returns a 3-tuple: (mode, code, insertion).

mode is the next mode (or state) of the lexer, and is always equal to ‘input’, ‘output’, or ‘tb’.

code is a portion of the line that should be added to the buffer corresponding to the next mode and eventually lexed by another lexer. For example, code could be Python code if mode were ‘input’.

insertion is a 3-tuple (index, token, text) representing an unprocessed “token” that will be inserted into the stream of tokens that are created from the buffer once we change modes. This is usually the input or output prompt.

In general, the next mode depends on current mode and on the contents of line.

ipytb_start = <_sre.SRE_Pattern object at 0x7f0d22599410>

The regex to determine when a traceback starts.

class IPython.nbconvert.utils.lexers.IPyLexer(**options)

Bases: pygments.lexer.Lexer

Primary lexer for all IPython-like code.

This is a simple helper lexer. If the first line of the text begins with “In [[0-9]+]:”, then the entire text is parsed with an IPython console lexer. If not, then the entire text is parsed with an IPython lexer.

The goal is to reduce the number of lexers that are registered with Pygments.

__init__(**options)

1 Function

IPython.nbconvert.utils.lexers.build_ipy_lexer(python3)

Builds IPython lexers depending on the value of python3.

The lexer inherits from an appropriate Python lexer and then adds information about IPython specific keywords (i.e. magic commands, shell commands, etc.)

Parameters:

python3 : bool

If True, then build an IPython lexer from a Python 3 lexer.