Using Prefuse through Jython

Prefuse is a very nice visualization toolkit. It is written in Java, it uses Java2D APIs to draw nodes, edges, and other visual items. It also handles events, supports animations, and other things you need to make the visualization become interactive.

The visualization display is implemented on top of JComponent. So it is very easy to embed it on another application, like what I did in LuaDeskSpot (source).

Okay, enough for the introduction. The purpose of this post is I want to show you how to use Prefuse through Jython. Yes, to make Prefuse (er.. Java, actually) scriptable!

  1. First, prepare the Jython installation.

    $ wget http://downloads.sourceforge.net/jython/jython_installer-2.5.0.jar
    $ java -jar jython_installer-2.5.0.jar
    
  2. Create Prefuse jar file. I have made one and you can freely use it.

    $ wget http://labs.fajran.web.id/p/ubuntu-pkg-vis/lib/prefuse.jar
    
  3. Set environment variables. The first one is path to Jython installation.

    $ export PATH=$PATH:/path/ke/lokasi/instalasi/jython
    

    Secondly, set JYTHONPATH so Jython can recognize the Prefuse library.

    $ export JYTHONPATH=$JYTHONPATH:prefuse.jar
    
  4. Make sure the setup is woking.

    $ jython
    >>> import prefuse
    

    If you get no error, then good! Prefuse has been set up properly. Then do whatever you want!

  5. I have made a Jython script that calls Prefuse. Check it out at http://gist.github.com/32288. Let’s try to use it as a demo.

    $ wget -O igv.py http://gist.github.com/raw/32288/503d011d94c564ffbdca91296b3fc204cf7b5186
  6. Now you have a Jython module called igv. It will handle the basic Prefuse initialization things. You just need to do the following to use it.

    $ jython
    >>> from igv import InteractiveGraphVisualization
    >>> ig = InteractiveGraphVisualization()
    

    That’s for the initialization and now let’s try to add some nodes and edges.

    >>> n1 = ig.add_node("satu")
    >>> n2 = ig.add_node("dua")
    >>> n3 = ig.add_node("tiga")
    >>> ig.add_edge(n1, n2)
    >>> ig.add_edge(n1, n3)
    >>> ig.add_edge(n2, n3)
    

And this is the obligatory screencast.

Have fun!

um.. yes, this is the translation of my previous blog entry discussing the same thing.

7 thoughts on “Using Prefuse through Jython”

  1. here is the code I am trying out can you tell where I am mistaken…I have replaced a few lines in you code.
    It doesn't show the shape

    def_init_vis(self)

    r=ShapeRenderer()
    r.setBaseSize(20)
    r.rectangle(10,10,50,100)

  2. Sorry I'm not familiar with Prefuse so I don't know how to answer your question.

    What I'm showing here is another way to use Prefuse classes, which is using Jython to do that. If you know Prefuse and Python, then you can try Jython to make Prefuse based application.

  3. the code is written in jython and I am using one of the prefuse classes .

    I want to just change the shape of the node. But I am unable to do that ….

    if you know how to that just please tell or tell me where can I get help for that

Leave a Reply