Ubuntu - Setup Graphviz Viewer & Thumbnailer

Contents[Hide]

dropcap-ubuntu-graphviz

Graphviz (Graph Visualization Software) is a package of open-source tools initiated by AT&T Labs Research for drawing graphs specified in DOT language scripts. It consists of a graph description language named the DOT language and a set of tools that can generate and/or process DOT files.

Under Ubuntu and Debian, you get a set of command line tools but no GUI (Graphical User Interface) for Gnome Shell.

Hopefully, a graph visualizer implemented in Java is available with ZGRViewer project. It is based upon the Zoomable Visual Transformation Machine. It is specifically aimed at displaying graphs expressed using the DOT language by using GraphViz binaries.

ZGRViewer is designed to handle large graphs, and offers a Zoomable User Interface, which enables smooth zooming and easy navigation in the visualized structure.

This article explains how to install Graphwiz & ZGRViewer and to declare interaction with Gnome Shell. You'll then be able to :

  • launch ZGRViewer from the Graphics menu
  • double-click on a DOT file to launch ZGRViewer
  • see your graphviz DOT files thumbnails inside Nautilus

This has been tested on Ubuntu Precise 12.04 LTS Amd64 and Gnome Shell 3.4. It should work on any distribution running Gnome Shell.

1. Install ZGRViewer

Under Ubuntu, GraphViz is easily available from the graphviz package.

To resize icons, we also need to install imagemagick package that will provide convert tool.

# sudo apt-get install graphviz imagemagick

ZGRViewer is not provided in the Ubuntu repositories. You need to download and install it from sourceforge.

As it is using Java, you have to make sure that Open JDK is installed.

# java -version
java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.5) (6b27-1.12.5-0ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

If Java is not installed, just install it.

# sudo apt-get install default-jre

You then need to download and install latest ZGRViewer stable version from ZGRViewer download page.

At the time of this article, version 0.9.0 was not working. I used version 0.8.2 which was totally ok.

Installation will be done under /opt/zgrviewer.

The run.sh launch script will be duplicated under zgrviewer.sh to set default path to /opt/zgrviewer.

# wget http://sourceforge.net/projects/zvtm/files/zgrviewer/0.8.2/zgrviewer-0.8.2.zip
# sudo unzip -d /opt zgrviewer-*.zip
# sed 's/^ZGRV_HOME=./ZGRV_HOME=\/opt\/zgrviewer/' /opt/zgrviewer/run.sh | sudo tee /opt/zgrviewer/zgrviewer.sh
# sudo chmod +x /opt/zgrviewer/zgrviewer.sh
# rm zgrviewer-*.zip

2. Add Icon to Current Theme

To make it simple, I've used the icon from Graphviz home site for association with ZGRViewer and DOT files.

Icon has to be placed in an active theme's directory. Under Gnome Shell, the gnome theme is used with every theme. So, Graphviz icon will be installed under gnome default theme.

Once you've placed the new icon, you need to regenerate your theme cache file.

# sudo wget -O /usr/share/icons/gnome/256x256/mimetypes/text-vnd.graphviz.png http://bernaerts.dyndns.org/download/ubuntu/graphviz/text-vnd.graphviz.png
# sudo gtk-update-icon-cache --force /usr/share/icons/gnome/
gtk-update-icon-cache: Cache file created successfully.

3. Declare ZGRViewer as Graphviz Viewer

Next step is to setup a full Nautilus interaction for Graphviz .dot files.

The expected behaviour is simple :

  • DOT files are shown in Nautilus with Graphviz icon
  • When you double-click on a DOT file, it opens ZGRViewer with the file as parameter.

This can be done in 2 steps :

  1. create a .desktop file to declare the action
  2. create a file association for this action

Action to open Graphviz DOT files will be handled by /usr/share/applications/graphviz.desktop.

/usr/share/applications/graphviz.desktop

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
ExecTry=/opt/zgrviewer/zgrviewer.sh
Exec=/opt/zgrviewer/zgrviewer.sh %f
Name=GraphViz ZGRViewer
Categories=Graphics
MimeType=text/vnd.graphviz
Icon=/usr/share/icons/gnome/256x256/mimetypes/text-vnd.graphviz.png

Graphviz DOT files are having the mime type text/vnd.graphviz. We can now associate graphviz.desktop with this mime type.

# sudo wget -O /usr/share/applications/graphviz.desktop http://bernaerts.dyndns.org/download/ubuntu/graphviz/graphviz.desktop
# sudo chmod +x /usr/share/applications/graphviz.desktop
# xdg-mime default graphviz.desktop text/vnd.graphviz

4. Nautilus Thumbnails for Graphviz Files

4.1. Declare Thumbnailer

Instead of having a generic Graphviz icon for the DOT files, you can setup Nautilus to generate a thumbnail of the graph.

To do that, Nautilus is using a 'thumbnailer' associated to the mime type of the file. This program (or script) is in charge of generating the thumbnail in a PNG format with a given height.

The following script is a Nautilus 'thumbnailer' for Graphviz DOT files :

/usr/local/sbin/graphviz-thumbnailer

#!/bin/sh
# --------------------------------------------
# Thumbnailer for Graphviz files
# Depends on :
# * imagemagick
# * graphviz
# Revision history :
# 04/08/2013 - Creation by N. Bernaerts
# -------------------------------------------

# get parameters
INPUT="$1"
OUTPUT="$2"
HEIGHT=$3

# go to graphviz DOT file directory
cd 'dirname "${INPUT}"'

# generation
dot -Tpng "${INPUT}" | convert -resize x${HEIGHT} - "${OUTPUT}"

To allow Nautilus to use this thumbnailer for DOT files, we need to declare this script as the thumbnailer for the mime type of DOT files (text/vnd.graphviz).

This declaration is done via the file /usr/share/thumbnailers/apk.thumbnailer.

/usr/share/thumbnailers/graphviz.thumbnailer

[Thumbnailer Entry]
TryExec=/usr/local/sbin/graphviz-thumbnailer
Exec=/usr/local/sbin/graphviz-thumbnailer %i %o %s
MimeType=text/vnd.graphviz;

These files can be installed with these commands :

# sudo wget -O /usr/local/sbin/graphviz-thumbnailer http://bernaerts.dyndns.org/download/ubuntu/graphviz/graphviz-thumbnailer
# sudo chmod +x /usr/local/sbin/graphviz-thumbnailer
# sudo wget -O /usr/share/thumbnailers/graphviz.thumbnailer http://bernaerts.dyndns.org/download/ubuntu/graphviz/graphviz.thumbnailer

4.2. Restart Nautilus

You can now restart nautilus to reload its complete environment.

# nautilus -q

Your Graphviz files will now appear as thumbnails in Nautilus.

nautilus-graphviz-thumbnail

Everything is now ready :

  • ZGRViewer should be available from the Graphics menu
  • Your DOT files should appear in Nautilus with a preview thumbnail
  • A double-click on a DOT file should launch ZGRViewer

 

Hope it helps.

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus