The pygments_magic extension allows you to display source code files in IPython with syntax highlighting. It requires the Pygments library.

It can be installed with:

%install_ext https://gist.github.com/jiffyclub/5385501/raw/pygments_magic.py
In [1]:
%load_ext pygments_magic

The %highlight magic uses terminal color codes and will work in any of IPython's front ends.

In [2]:
%highlight ipython_rocks.py
from __future__ import print_function

def ipython_rocks():
    """Yes, yes it does."""
    print('IPython rocks!')

ipython_rocks()

The %highlight_html magic outputs HTML and is best used in the Notebook. %highlight_html can show line numbers or display with any avaiable Pygments style. See these previews for some examples.

In [3]:
%highlight_html -n ipython_rocks.py
1
2
3
4
5
6
7
from __future__ import print_function

def ipython_rocks():
    """Yes, yes it does."""
    print('IPython rocks!')

ipython_rocks()
In [4]:
%highlight_html -s monokai ipython_rocks.py
from __future__ import print_function

def ipython_rocks():
    """Yes, yes it does."""
    print('IPython rocks!')

ipython_rocks()
In [ ]: