Writing an IPython extension to show object graphs

Idea: Reuse a Python package, pyrels, to display data structures graphically (see presentation at EuroPython 2008) in an IPython notebook.

Inspiration: Find an existing extension that already does something similar: display GraphViz DOT files. ipython-hierarchymagic does it!


In [1]:
%install_ext https://raw.github.com/tkf/ipython-hierarchymagic/master/hierarchymagic.py
%load_ext hierarchymagic


Installed hierarchymagic.py. To use it, type:
  %load_ext hierarchymagic

This gives two new IPython magic commands: %hierarchy and %%dot, doing what you expect them to do, at least for %%dot. We skip %hierarchy here, but you can uncomment it to see the standard example.


In [2]:
# %hierarchy get_ipython()

This is actually the command that is pretty similar to what we want:


In [3]:
%%dot -f svg
digraph G {
    a->b; b->c; c->d; c->a; d->b; d->a;
}


G a a b b a->b c c b->c c->a d d c->d d->a d->b

Now install pyrels 0.1.1 (or higher) if you don't have it already:


In [4]:
!pip install pyrels==0.1.1


Requirement already satisfied (use --upgrade to upgrade): pyrels==0.1.1 in /opt/lib/python2.7/site-packages
Cleaning up...

Now let's see if that worked as expected with a very simple example. Here we create some DOT code that we first display manually after passing it to the %%dot command:


In [5]:
from pyrels.pyrels2dot import namespace2dot_str
print namespace2dot_str({"myInt": 42})


digraph G {
    overlap=false;

    name4331282912 [label="myInt", shape="ellipse"];
    int4298193560 [label="int: 42", shape="box"];

    name4331282912 -> int4298193560 [label="ref"];

}


In [6]:
%%dot -f svg
digraph G {
    overlap=false;

    name4368865680 [label="myInt", shape="ellipse"];
    int4298193560 [label="int: 42", shape="box"];

    name4368865680 -> int4298193560 [label="ref"];

}


G name4368865680 myInt int4298193560 int: 42 name4368865680->int4298193560 ref

This is what we want to create directly with an extension using pyrels. The code to do that is on Gist.


In [7]:
# %install_ext file:///Users/dinu/Desktop/joy-of-scipy/ipython_objgraph_magic.py
%install_ext https://gist.github.com/deeplook/4731035/raw/daa82e13f6635f513694bffb0bb9bc8a1b4ceff2/ipython_object_graphs.py
%load_ext ipython_objgraph_magic


Installed ipython_object_graphs.py. To use it, type:
  %load_ext ipython_object_graphs

Now we can create some data structure and display it (as a namespace) with the new magic command %%objgraph:


In [8]:
class C(object):
    pass
n = 23
L = [1, 23, (2, 3), None, C]

In [9]:
%%objgraph -f svg
{"myInt": n, "myList": L}


G name4367705552 myList list4367703360 list: 0 1 2 3 4 name4367705552->list4367703360 ref int4298192568 int: 1 list4367703360->int4298192568 int4298192040 int: 23 list4367703360->int4298192040 tuple4366071424 tuple: 0 1 list4367703360->tuple4366071424 none None list4367703360->none type4368459968 new style class: C list4367703360->type4368459968 int4298192544 int: 2 tuple4366071424->int4298192544 int4298192520 int: 3 tuple4366071424->int4298192520 name4367706896 myInt name4367706896->int4298192040 ref

Neat!


In [ ]: