Search

Ubuntu - Graphviz Previewer & Thumbnailer for Nautilus

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.

DOT files are text files with .dot extension. They use a graph description language named DOT language. These files can be processed by some tools like dot  to generate a graphical view.

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

If you want to have a complete desktop integration for your graphviz DOT files, you need to add a proper thumbnailer and a previewer.

This article explains how to setup a complete desktop integration for Graphviz .dot files with Gnome Shell. You'll then be able to :

  • see your graphviz .dot files thumbnails inside Nautilus
  • see your graphviz .dot file preview straight from Eye of Gnome

This has been tested on Ubuntu Gnome 14.04 LTS Amd64 and LUbuntu 14.04 LTS. It should work on many Linux distribution with little adjustment.

1. Install Packages

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

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

File preview will be done with Eye Of Gnome provided in eog package.

So, make sure all these packages are installed :

Terminal
# sudo apt-get install graphviz imagemagick eog

2. Previewer for Graphviz .dot files

Next step is to setup and declare .dot files preview in Nautilus.

The expected behaviour is simple : when you double-click on a .dot file, it opens a preview window showing the graphical result of the Graphviz generation.

As DOT files are text files, preview will involve the use of a previewer script graphviz-preview in charge of conversion of the .dot file to a .png picture and display it using  previewer.

/usr/local/bin/graphviz-preview
#!/bin/bash
# ---------------------------------------------------
# Preview of Graphviz .DOT files
#
# Procedure :
#   http://bernaerts.dyndns.org/linux/76-gnome/333-gnome-shell-graphviz-viewer-thumbnailer
# Depends on :
#   * dot (graphviz package)
#   * eog
# Parameters :
#   $1 - full path of DOT file
# Revision history :
#   24/04/2015, V1.0 - Creation by N. Bernaerts
# ---------------------------------------------------

# check tools availability
command -v dot >/dev/null 2>&1 || { zenity --error --text="Please install dot [graphviz package]"; exit 1; }
command -v eog >/dev/null 2>&1 || { zenity --error --text="Please install eog [eye of gnome]"; exit 1; }

# select graphviz file if not given as parameter
[ "$1" != "" ] && DOT_FILE="$1" || DOT_FILE="$(zenity --file-selection --file-filter='Graphviz files (dot)|*.dot' --title='Select file to preview')"

# if no file selected, exit
[ "${DOT_FILE}" == "" ] && exit 0

# retrieve file parameters
DOT_DIR=$(dirname "${DOT_FILE}")
DOT_NAME=$(basename "${DOT_FILE}")
DOT_BASE="${DOT_NAME%.*}"

# generate temporary local filename
TMP_DIR=$(mktemp -d -t preview-XXXXXXXX)
TMP_PIX="${TMP_DIR}/${DOT_BASE}.png"

# convert graphviz DOT file to PNG
cd "${DOT_DIR}" 
dot -Tpng "${DOT_FILE}" > "${TMP_PIX}"

# preview display
eog "${TMP_PIX}"

# remove temporary files
rm "${TMP_PIX}"
rmdir "${TMP_DIR}"

Nautilus integration is done thru a graphviz.desktop file, that will declare the preview program and associate it with mimetype text/vnd.graphviz of Graphviz .dot files .

/usr/share/applications/graphviz.desktop
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
ExecTry=/usr/local/bin/graphviz-preview
Exec=/usr/local/bin/graphviz-preview %f
Name=GraphViz Preview
Categories=Graphics
MimeType=text/vnd.graphviz
Icon=graphviz

Installation of the complete Graphviz .dot preview environment can be done with these commands :

Terminal
# sudo wget -O /usr/share/icons/graphviz.png http://bernaerts.dyndns.org/download/ubuntu/graphviz/text-vnd.graphviz.png
# sudo wget -O /usr/local/bin/graphviz-preview https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/graphviz/graphviz-preview
# sudo chmod +x /usr/local/bin/graphviz-preview
# sudo wget -O /usr/share/applications/graphviz.desktop https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/graphviz/graphviz.desktop
# sudo chmod +x /usr/share/applications/graphviz.desktop

3. Thumbnailer for Graphviz .dot files

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 text/vnd.graphviz 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/bin/graphviz-thumbnailer
#!/bin/bash
# --------------------------------------------
# Thumbnailer for Graphviz .DOT files
#
# Procedure :
#   http://bernaerts.dyndns.org/linux/76-gnome/333-gnome-shell-graphviz-viewer-thumbnailer
# Depends on :
#   * dot (graphviz package)
#   * pngtopnm, pnmtopng and pnmscalefixed (netpbm package)
#   * gvfs-copy (gvfs-bin package)
# Parameters :
#   $1 - URI of graphviz file
#   $2 - full path of generated thumbnail
#   $3 - height of thumbnail in pixels
# Revision history :
#   04/08/2013, V1.0 - Creation by N. Bernaerts
#   25/04/2015, V1.1 - Convert to bash
#   07/10/2017, V1.2 - Conversion to URI and speedup with netpbm
# -------------------------------------------

# check tools availability
command -v dot >/dev/null 2>&1 || exit 1
command -v pngtopnm >/dev/null 2>&1 || exit 1
command -v pnmtopng >/dev/null 2>&1 || exit 1
command -v pnmscalefixed >/dev/null 2>&1 || exit 1

# get parameters
FILE_URI="$1"
FILE_THUMB="$2"
SIZE="$3"

# generate temporary local filename
TMP_LOCAL=$(mktemp -t XXXXXXXX.dot)

# if file is a remote one
URI_TYPE="${FILE_URI:0:4}"
if [ "${URI_TYPE}" = "file" ]
then
    # convert URI to local path
    FILE_PATH=$(printf '%b' "${FILE_URI//%/\\x}")

    # extract local path
    FILE_LOCAL="${FILE_PATH:7}"

    # go to graphviz DOT file directory
    cd $(dirname "${FILE_LOCAL}")
else
    # check gvfs-copy availability
    command -v gvfs-copy >/dev/null 2>&1 || exit 1

    # copy input file to temporary local file
    gvfs-copy "${FILE_URI}" "${TMP_LOCAL}"
    FILE_LOCAL="${TMP_LOCAL}"    
fi

# generation
dot -Tpng "${FILE_LOCAL}" | pngtopnm - | pnmscalefixed -xysize ${SIZE} ${SIZE} - | pnmtopng -downscale -compression 0 - > "${FILE_THUMB}"

# remove temporary  file
rm -f "${TMP_LOCAL}"

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

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 %u %o %s
MimeType=text/vnd.graphviz;

Complete thumbnailer environment can be installed with these commands :

Terminal
# sudo wget -O /usr/local/bin/graphviz-thumbnailer https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/graphviz/graphviz-thumbnailer
# sudo chmod +x /usr/local/bin/graphviz-thumbnailer
# sudo wget -O /usr/share/thumbnailers/graphviz.thumbnailer https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/thumbnailer/graphviz/graphviz.thumbnailer

4. Restart

Everything is now ready. You just need to restart your computer.

If you are running Ubuntu Gnome, you can simply restart nautilus.

Terminal
# nautilus -q

Your Graphviz files will now appear as thumbnails in your file manager.

nautilus-graphviz-thumbnail

A double click on any Graphviz .dot files, should launch a graph preview.

nautilus graphviz preview

 

Hope it helps.

Signature Technoblog

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