Creating Interactive Figures

This notebook will demonstrate how you can use pywwt to create interactive figures suitable for submission to a journal such as The Astrophysical Journal.

This won't work correctly with the new viewer!! pywwt won't have the right internal state.

Step 1: Setting up WWT

First, we need to connect to the WWT app. If WWT isn't running yet, activate the JupyterLab command palette from the View menu, and select "AAS WorldWide Telescope". Then all we need to do is run:

In [ ]:
from pywwt.jupyter import connect_to_app
wwt = await connect_to_app().becomes_ready()

We'll also set up a utility function to help us locate data files stored alongside this notebook:

In [ ]:
def datapath(*args):
    from os.path import join
    return join('data', *args)

Step 2: Inserting Some Data

An interactive figure should presumably have some data in it to show off. Here, we'll just load up some of the data used in the other tutorials to show the general principles.

We'll start by displaying a WISE 12µm image towards the Westerhout 5 star forming region:

In [ ]:
w5_img = wwt.layers.add_image_layer(datapath('w5.fits'))

Now is a good time to play around with the visualization parameters of the image, if you'd like. Executing the following line will create some UI elements that let you tune the colormap, stretch, and so on:

In [ ]:

Likewise you can control the settings used for the all-sky contextual imagery:

In [ ]:

Step 3: Exporting to standalone HTML

pywwt, like all Jupyter widgets, is at its heart web-based. We've been working on building the technology to extract the visualizations that you create from Jupyter into standalone web assets that can be included anywhere that you can host HTML + JavaScript + CSS. This trio of technologies is exactly how interactive figures in the AAS journals work!

Once you've set up your viewer, exporting is easy:

In [ ]:

After a few seconds, you should see a file named appear in the folder view sidebar to the left of this notebook. What's cool is that even if you're running this notebook in the cloud, you can right-click that file and select "Download" to save the Zip bundle to your local machine.

If you were to open up this Zip file and inspect its contents, you'd see that it contains an index.html file, some JavaScript, and a data directory containing a FITS file correspond to the image that you just loaded into your view.

Example 1: Submitting the figure to a journal

It’s a bit beyond the scope of this notebook to describe the full submission process here. But the key thing is that the file has virtually everything you need to include an interacting figure in an AAS journal. See this blog post by Peter Williams for a detailed guide involving similar files created with the AAS Timeseries Tool.

Example 2: Including the figure on a personal website

The same files that you submit to a journal can also be included on your own website, if your authoring platform allows you to upload arbitrary files and use HTML <iframe> tags. Here’s a minimal sample HTML file, assuming that you have unpacked your Zip bundle into a directory named my_figure:

<!DOCTYPE html>

<html lang="en">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <style type="text/css">
      .intfig { width: 600px; height: 400px; }
    <h1>Welcome to My Personal Webpage</h1>
    <p>Explore the data from my latest paper!</p>
    <iframe class="intfig" src="my_figure/index.html">
      <p>Unfortunately your browser doesn’t support iframes.</p>

Most of this is just boilerplate — the key is the <iframe> line and the CSS styling that sets it to the correct size.

Example 3: Playing with a figure on your own computer

If you have Node.js installed on your computer, you can view your exported interactive figure in a relatively straightforward way:

  1. Unpack the Zip file
  2. In a terminal, cd to the unpacked directory (the one containing index.html).
  3. Run: npx http-server
  4. Open a browser to the URL printed by the program.

Note: alas, we have a race condition bug that sometimes pops up here. You might need to reload the page for all of your data to appear.

Other one-liner web servers might work, but not all of them serve files with the correct Content-Type headers, which is needed for everything to work here.

Next Steps

To learn more about how to load image data into pywwt, check out the Visualizing Imagery tutorial.


This notebook was prepared by:

  • O. Justin Otor
  • Thomas Robitaille
  • Peter K. G. Williams