Customizing your Glue environment

Using a file as described in Configuring Glue via a startup file, you can customize many aspects of your Glue environment, which are described in the following sections.


Before we talk about the different components of the Glue environment that you can customize, we first need to look at registries. Glue is written so as to allow users to easily register new data viewers, tools, exporters, and more. Registering such components can be done via registries located in the glue.config sub-package. Registries include for example link_function, data_factory, colormaps, and so on. As demonstrated below, some registries can be used as decorators (see e.g. Custom Link Functions) and for others you can add items using the add method (see e.g. Custom Colormaps).

In the following sections, we show a few examples of registering new functionality, and a full list of available registries is given in Complete list of registries.

Custom Data Loaders

Glue lets you create custom data loader functions, to use from within the GUI.

Here’s a quick example: the default image loader in Glue reads each color in an RGB image into 3 two-dimensional components. Perhaps you want to be able to load these images into a single 3-dimensional component called cube. Here’s how you could do this:

from glue.config import data_factory
from glue.core import Data
from import imread

def is_jpeg(filename, **kwargs):
    return filename.endswith('.jpeg')

@data_factory('3D image loader', is_jpeg)
def read_jpeg(file_name):
    im = imread(file_name)
    return Data(cube=im)

Let’s look at this line-by-line:

  • The is_jpeg function takes a filename and keywords as input, and returns True if a data factory can handle this file
  • The @data_factory decorator is how Glue “finds” this function. Its two arguments are a label, and the is_jpeg identifier function
  • The first line in read_jpeg uses scikit-image to load an image file into a NumPy array.
  • The second line constructs a Data object from this array, and returns the result.

If you put this in your file, you will see a new file type when loading data:


If you open a file using this file type selection, Glue will pass the path of this file to your function, and use the resulting Data object.

If you are defining a data factory that may clash with an existing one, for example if you are defining a loader for a specific type of FITS file, then make sure that the identifier function (e.g. is_jpeg above) returns True only for that specific subset of FITS files. Then you can set the priority= keyword in the @data_factory decorator. The value should be an integer or floating-point number, with larger numbers indicating a higher priority.

For more examples of custom data loaders, see the example repository.

Custom importers

The Custom Data Loaders described above allow Glue to recognize more file formats than originally implemented, but it is also possible to write entire new ways of importing data, including new GUI dialogs. An example would be a dialog that allows the user to query and download online data.

Currently, an importer should be defined as a function that returns a list of Data objects. In future we may relax this latter requirement and allow existing tools in Glue to interpret the data.

An importer can be defined using the @importer decorator:

from glue.config import importer
from glue.core import Data

@importer("Import from custom source")
def my_importer():
    # Main code here
    return [Data(...), Data(...)]

The label in the @importer decorator is the text that will appear in the Import menu in Glue.

Custom menubar tools

In some cases, it might be desirable to add tools to Glue that can operate on any aspects of the data or subsets, and can be accessed from the menubar. To do this, you can define a function that takes two arguments (the session object, and the data collection object), and decorate it with the @menubar_plugin decorator, giving it the label that will appear in the Tools menubar:

from glue.config import menubar_plugin

@menubar_plugin("Do something")
def my_plugin(session, data_collection):
    # do anything here

The function can do anything, such as launch a QWidget, or anything else (such as a web browser, etc.), and does not need to return anything (instead it can operate by directly modifying the data collection or subsets).

Custom Colormaps

You can add additional matplotlib colormaps to Glue’s image viewer by adding the following code into

from glue.config import colormaps
from import Paired
colormaps.add('Paired', Paired)

Custom Subset Actions

You can add menu items to run custom functions on subsets. Use the following pattern in

from glue.config import single_subset_action

def callback(subset, data_collection):
    print "Called with %s, %s" % (subset, data_collection)

single_subset_action('Menu title', callback)

This menu item is available by right clicking on a subset when a single subset is selected in the Data Collection window. Note that you must select the subset specific to a particular Data set, and not the parent Subset Group.

Complete list of registries

A few registries have been demonstrated above, and a complete list of main registries are listed below. All can be imported from glue.config - each registry is an instance of a class, given in the second column, and which provides more information about what the registry is and how it can be used.

Registry name Registry class
qt_client glue.config.QtClientRegistry
tool_registry glue.config.QtToolRegistry
data_factory glue.config.DataFactoryRegistry
link_function glue.config.LinkFunctionRegistry
link_helper glue.config.LinkHelperRegistry
colormaps glue.config.ColormapRegistry
exporters glue.config.ExporterRegistry
settings glue.config.SettingRegistry
fit_plugin glue.config.ProfileFitterRegistry
single_subset_action glue.config.SingleSubsetLayerActionRegistry

Deferring loading of plug-in functionality (advanced)

In some cases, you may want to defer the loading of your component/functionality until it is actually needed. To do this:

  • Place the code for your plugin in a file or package that could be imported from the (but don’t import it directly - it just has to be importable)
  • Include a function called setup alongside the plugin, and this function should contain code to actually add your custom tools to the appropriate registries.
  • In, you can then add the plugin file or package to a registry by using the lazy_add method and pass a string giving the name of the package or sub-package containing the plugin.

Imagine that you have created a data viewer MyQtViewer. You could directly register it using:

from glue.config import qt_client

but if you want to defer the loading of the MyQtViewer class, you can place the definition of MyQtViewer in a file called e.g. that is located in the same directory as your file. This file should look something like:

class MyQtViewer(...):

def setup():
    from glue.config import qt_client

then in, you can do:

from glue.config import qt_client

With this in place, the setup in your plugin will only get called if the Qt data viewers are needed, but you will avoid unecessarily importing Qt if you only want to access glue.core.