Advanced: Callbacks and Extensions#

So far we have been sending data to Firefly to visualize. This notebook shows how to add extensions to Firefly to bring back data into our Python session through callback functions.

Note

Callbacks involve interactive UI actions (e.g., point selection), so running the code is only half the story—you must also trigger the UI interaction in the Firefly viewer.

Setup#

First, we create a FireflyClient instance and open a Firefly viewer. See Initializing a FireflyClient instance for more details. Then, we display a FITS image from a URL so that we have an image for triggering point-selection event. See Displaying a FITS Image for more details.

[1]:
from firefly_client import FireflyClient

# Initialize a FireflyClient instance
fc = FireflyClient.make_client(url="https://irsa.ipac.caltech.edu/irsaviewer")

# Display a FITS image (from URL of WISE W2 cutout of M51 galaxy)
img_url = 'https://irsa.ipac.caltech.edu/ibe/data/wise/allsky/4band_p1bm_frm/9a/05379a/141/05379a141-w2-int-1b.fits?center=202.4841667,47.23055556&size=400pix'
plot_id = 'wise-cutout'
fc.show_fits_image(file_input=img_url, plot_id=plot_id, title='WISE Cutout')
[1]:
{'success': True}

Define and register a callback#

A callback receives an event dictionary from Firefly. This example prints out world coordinates when a point-selection event occurs.

[2]:
def print_coords(event):
    if 'data' in event and 'wpt' in event['data']:
        wpt = event['data']['wpt'] # world point
        wdata = wpt.split(';')
        ra = float(wdata[0])
        dec = float(wdata[1])
        print(f'ra={ra:.6f}, dec={dec:.6f}')

fc.add_listener(print_coords)

Add an extension to trigger the callback#

To activate the callback in point-selection mode, add it as an extension with ext_type='POINT'. The title of the extension will appear as a clickable text-button when the Firefly viewer is in point-selection mode.

[3]:
fc.add_extension(ext_type='POINT', plot_id=None, title='Print coords', extension_id='pickit')
[3]:
{'success': True}
ra=202.395939, dec=47.228118
ra=202.510304, dec=47.185369
ra=202.496007, dec=47.267874

Trigger the callback (manual step)#

In the Firefly browser tab:

  1. Enable point-selection mode (enable ‘Lock by click’ switch in the bottom right of image display).

  2. Click on the image to select a point.

  3. Click the Print coords button to trigger the callback.

You should see RA/Dec printed in your Python session output (which is the last code cell executed in the context of Python notebooks).

Remove the callback#

When you’re done, remove the listener:

[4]:
fc.remove_listener(print_coords)