Skip to content

Python API: Plugins (Mixins)

These mixins contain the specific commands injected into the main FViewer class.

fviewer.plugins.image_control

ImageControlMixin

Methods for controlling the FViewer canvas display.

Source code in fviewer/plugins/image_control.py
class ImageControlMixin:
    """Methods for controlling the FViewer canvas display."""

    def get_colormap(self) -> str:
        """Retrieves the currently active colormap in the image viewer."""
        return self._send("get_colormap").get("colormap", "")

    def get_stretch(self) -> str:
        """Retrieves the currently active image stretch."""
        return self._send("get_stretch").get("stretch", "")

    def set_colormap(self, cmap: str) -> Dict[str, Any]:
        """Sets the colormap for the image viewer."""
        return self._send("set_colormap", cmap=cmap)

    def set_stretch(self, stretch: str) -> Dict[str, Any]:
        """Sets the stretch algorithm for the image viewer."""
        return self._send("set_stretch", stretch=stretch)

get_colormap()

Retrieves the currently active colormap in the image viewer.

Source code in fviewer/plugins/image_control.py
def get_colormap(self) -> str:
    """Retrieves the currently active colormap in the image viewer."""
    return self._send("get_colormap").get("colormap", "")

get_stretch()

Retrieves the currently active image stretch.

Source code in fviewer/plugins/image_control.py
def get_stretch(self) -> str:
    """Retrieves the currently active image stretch."""
    return self._send("get_stretch").get("stretch", "")

set_colormap(cmap)

Sets the colormap for the image viewer.

Source code in fviewer/plugins/image_control.py
def set_colormap(self, cmap: str) -> Dict[str, Any]:
    """Sets the colormap for the image viewer."""
    return self._send("set_colormap", cmap=cmap)

set_stretch(stretch)

Sets the stretch algorithm for the image viewer.

Source code in fviewer/plugins/image_control.py
def set_stretch(self, stretch: str) -> Dict[str, Any]:
    """Sets the stretch algorithm for the image viewer."""
    return self._send("set_stretch", stretch=stretch)

fviewer.plugins.regions

RegionsMixin

Methods for managing and drawing regions on the FViewer canvas.

Source code in fviewer/plugins/regions.py
class RegionsMixin:
    """Methods for managing and drawing regions on the FViewer canvas."""

    def get_regions(self, format: str = "image") -> List[Dict[str, Any]]:
        """
        Retrieves a list of all currently drawn regions from the viewer.

        Args:
            format (str): The coordinate system to return the regions in.
                Valid options are 'image' (pixel coordinates), 'physical',
                'fk5' (RA/Dec), or 'wcs'. Defaults to "image".

        Returns:
            List[Dict[str, Any]]: A list of dictionaries, where each dictionary
            represents a region and its properties (type, coordinates,
            color, etc.).

        Raises:
            ValueError: If an unsupported format string is provided.
        """
        valid_formats = ["image", "physical", "fk5", "wcs"]
        if format not in valid_formats:
            raise ValueError(
                "format must be 'image', 'physical', 'fk5', or 'wcs'"
            )

        return self._send("get_regions", format=format).get("regions", [])

    def clear_regions(self) -> Dict[str, Any]:
        """
        Removes all currently drawn regions from the canvas.

        Returns:
            Dict[str, Any]: A dictionary containing the status of the operation
            (e.g., `{"status": "ok"}`).
        """
        return self._send("clear_regions")

    def add_circle(
        self,
        x: float,
        y: float,
        radius: float,
        color: str = "#00ff00",
        format: str = "image",
        is_background: bool = False,
    ) -> Dict[str, Any]:
        """
        Draws a circular region on the image.

        Args:
            x (float): The X coordinate (or RA) of the circle's center.
            y (float): The Y coordinate (or Dec) of the circle's center.
            radius (float): The radius of the circle.
            color (str): Hex color code for the region outline.
                Defaults to "#00ff00".
            format (str): The coordinate system used for x, y, and radius.
                Options: 'image', 'physical', 'fk5', 'wcs'.
                Defaults to "image".
            is_background (bool): If True, designates this region
                as a background estimation area (typically rendered
                with a dashed line).

        Returns:
            Dict[str, Any]: Server acknowledgment of the command.
        """
        return self._send(
            "add_region",
            type="circle",
            x=x,
            y=y,
            radius=radius,
            color=color,
            format=format,
            isBackground=is_background,
        )

    def add_box(
        self,
        x: float,
        y: float,
        width: float,
        height: float,
        angle: float = 0,
        color: str = "#00ff00",
        format: str = "image",
        is_background: bool = False,
    ) -> Dict[str, Any]:
        """
        Draws a rectangular box region on the image.

        Args:
            x (float): The X coordinate (or RA) of the box's center.
            y (float): The Y coordinate (or Dec) of the box's center.
            width (float): The full width of the box.
            height (float): The full height of the box.
            angle (float): Rotation angle in degrees (counter-clockwise).
                Defaults to 0.
            color (str): Hex color code for the region outline.
                Defaults to "#00ff00".
            format (str): The coordinate system used. Defaults to "image".
            is_background (bool): Designates as a background region.
                Defaults to False.

        Returns:
            Dict[str, Any]: Server acknowledgment of the command.
        """
        return self._send(
            "add_region",
            type="box",
            x=x,
            y=y,
            width=width,
            height=height,
            angle=angle,
            color=color,
            format=format,
            isBackground=is_background,
        )

    def add_ellipse(
        self,
        x: float,
        y: float,
        rx: float,
        ry: float,
        angle: float = 0,
        color: str = "#00ff00",
        format: str = "image",
        is_background: bool = False,
    ) -> Dict[str, Any]:
        """
        Draws an elliptical region on the image.

        Args:
            x (float): The X coordinate (or RA) of the ellipse's center.
            y (float): The Y coordinate (or Dec) of the ellipse's center.
            rx (float): The semi-major axis radius.
            ry (float): The semi-minor axis radius.
            angle (float): Rotation angle in degrees. Defaults to 0.
            color (str): Hex color code for the region outline.
                Defaults to "#00ff00".
            format (str): The coordinate system used. Defaults to "image".
            is_background (bool): Designates as a background region.
                Defaults to False.

        Returns:
            Dict[str, Any]: Server acknowledgment of the command.
        """
        return self._send(
            "add_region",
            type="ellipse",
            x=x,
            y=y,
            rx=rx,
            ry=ry,
            angle=angle,
            color=color,
            format=format,
            isBackground=is_background,
        )

    def add_annulus(
        self,
        x: float,
        y: float,
        inner_r: float,
        outer_r: float,
        color: str = "#00ff00",
        format: str = "image",
        is_background: bool = False,
    ) -> Dict[str, Any]:
        """
        Draws an annular (ring) region on the image.

        Args:
            x (float): The X coordinate (or RA) of the annulus center.
            y (float): The Y coordinate (or Dec) of the annulus center.
            inner_r (float): The inner radius of the ring.
            outer_r (float): The outer radius of the ring.
            color (str): Hex color code for the region outline.
                Defaults to "#00ff00".
            format (str): The coordinate system used. Defaults to "image".
            is_background (bool): Designates as a background region.
                Defaults to False.

        Returns:
            Dict[str, Any]: Server acknowledgment of the command.
        """
        return self._send(
            "add_region",
            type="annulus",
            x=x,
            y=y,
            innerR=inner_r,
            outerR=outer_r,
            color=color,
            format=format,
            isBackground=is_background,
        )

    def load_regions(self, filepath: str) -> Dict[str, Any]:
        """
        Reads a local DS9 `.reg` file and applies it to the viewer.

        Args:
            filepath (str): The absolute or relative path to the
                region file on disk.

        Returns:
            Dict[str, Any]: Server acknowledgment after parsing and drawing.
        """
        with open(filepath, "r") as f:
            content = f.read()

        return self._send("load_regions_from_string", content=content)

    def save_regions(
        self, filepath: str, format: str = "image"
    ) -> Dict[str, str]:
        """
        Fetches regions from the viewer and saves them to a `.reg` file.

        This method requests a formatted DS9 region string from the React
        frontend and writes it securely to the local disk. It includes
        safeguards against unsupported extensions and excessively large
        memory payloads.

        Args:
            filepath (str): The destination path for the saved file.
                Must end with '.reg' or '.txt'.
            format (str): The coordinate system to save the regions in.
                Options: 'image' or 'fk5'. Defaults to "image".

        Returns:
            Dict[str, str]: A dictionary containing the status and the
                saved file path.

        Raises:
            ValueError: If an invalid format or unsafe file extension is
                provided.
            RuntimeError: If the region payload returned by the UI exceeds 5MB.
        """
        if format not in ["image", "physical", "fk5"]:
            raise ValueError("format must be 'image' or 'fk5'")

        safe_exts = (".reg", ".txt")
        if not filepath.lower().endswith(safe_exts):
            raise ValueError(
                f"For security, filepath must end with {safe_exts}"
            )

        response = self._send("get_regions_string", format=format)
        content = response.get("content", "")

        max_bytes = 5 * 1024 * 1024
        if len(content.encode("utf-8")) > max_bytes:
            raise RuntimeError(
                "Received region data exceeds 5MB limit. Aborting save."
            )

        with open(filepath, "w") as f:
            f.write(content)

        return {"status": "ok", "file": filepath}

add_annulus(x, y, inner_r, outer_r, color='#00ff00', format='image', is_background=False)

Draws an annular (ring) region on the image.

Parameters:

Name Type Description Default
x float

The X coordinate (or RA) of the annulus center.

required
y float

The Y coordinate (or Dec) of the annulus center.

required
inner_r float

The inner radius of the ring.

required
outer_r float

The outer radius of the ring.

required
color str

Hex color code for the region outline. Defaults to "#00ff00".

'#00ff00'
format str

The coordinate system used. Defaults to "image".

'image'
is_background bool

Designates as a background region. Defaults to False.

False

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Server acknowledgment of the command.

Source code in fviewer/plugins/regions.py
def add_annulus(
    self,
    x: float,
    y: float,
    inner_r: float,
    outer_r: float,
    color: str = "#00ff00",
    format: str = "image",
    is_background: bool = False,
) -> Dict[str, Any]:
    """
    Draws an annular (ring) region on the image.

    Args:
        x (float): The X coordinate (or RA) of the annulus center.
        y (float): The Y coordinate (or Dec) of the annulus center.
        inner_r (float): The inner radius of the ring.
        outer_r (float): The outer radius of the ring.
        color (str): Hex color code for the region outline.
            Defaults to "#00ff00".
        format (str): The coordinate system used. Defaults to "image".
        is_background (bool): Designates as a background region.
            Defaults to False.

    Returns:
        Dict[str, Any]: Server acknowledgment of the command.
    """
    return self._send(
        "add_region",
        type="annulus",
        x=x,
        y=y,
        innerR=inner_r,
        outerR=outer_r,
        color=color,
        format=format,
        isBackground=is_background,
    )

add_box(x, y, width, height, angle=0, color='#00ff00', format='image', is_background=False)

Draws a rectangular box region on the image.

Parameters:

Name Type Description Default
x float

The X coordinate (or RA) of the box's center.

required
y float

The Y coordinate (or Dec) of the box's center.

required
width float

The full width of the box.

required
height float

The full height of the box.

required
angle float

Rotation angle in degrees (counter-clockwise). Defaults to 0.

0
color str

Hex color code for the region outline. Defaults to "#00ff00".

'#00ff00'
format str

The coordinate system used. Defaults to "image".

'image'
is_background bool

Designates as a background region. Defaults to False.

False

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Server acknowledgment of the command.

Source code in fviewer/plugins/regions.py
def add_box(
    self,
    x: float,
    y: float,
    width: float,
    height: float,
    angle: float = 0,
    color: str = "#00ff00",
    format: str = "image",
    is_background: bool = False,
) -> Dict[str, Any]:
    """
    Draws a rectangular box region on the image.

    Args:
        x (float): The X coordinate (or RA) of the box's center.
        y (float): The Y coordinate (or Dec) of the box's center.
        width (float): The full width of the box.
        height (float): The full height of the box.
        angle (float): Rotation angle in degrees (counter-clockwise).
            Defaults to 0.
        color (str): Hex color code for the region outline.
            Defaults to "#00ff00".
        format (str): The coordinate system used. Defaults to "image".
        is_background (bool): Designates as a background region.
            Defaults to False.

    Returns:
        Dict[str, Any]: Server acknowledgment of the command.
    """
    return self._send(
        "add_region",
        type="box",
        x=x,
        y=y,
        width=width,
        height=height,
        angle=angle,
        color=color,
        format=format,
        isBackground=is_background,
    )

add_circle(x, y, radius, color='#00ff00', format='image', is_background=False)

Draws a circular region on the image.

Parameters:

Name Type Description Default
x float

The X coordinate (or RA) of the circle's center.

required
y float

The Y coordinate (or Dec) of the circle's center.

required
radius float

The radius of the circle.

required
color str

Hex color code for the region outline. Defaults to "#00ff00".

'#00ff00'
format str

The coordinate system used for x, y, and radius. Options: 'image', 'physical', 'fk5', 'wcs'. Defaults to "image".

'image'
is_background bool

If True, designates this region as a background estimation area (typically rendered with a dashed line).

False

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Server acknowledgment of the command.

Source code in fviewer/plugins/regions.py
def add_circle(
    self,
    x: float,
    y: float,
    radius: float,
    color: str = "#00ff00",
    format: str = "image",
    is_background: bool = False,
) -> Dict[str, Any]:
    """
    Draws a circular region on the image.

    Args:
        x (float): The X coordinate (or RA) of the circle's center.
        y (float): The Y coordinate (or Dec) of the circle's center.
        radius (float): The radius of the circle.
        color (str): Hex color code for the region outline.
            Defaults to "#00ff00".
        format (str): The coordinate system used for x, y, and radius.
            Options: 'image', 'physical', 'fk5', 'wcs'.
            Defaults to "image".
        is_background (bool): If True, designates this region
            as a background estimation area (typically rendered
            with a dashed line).

    Returns:
        Dict[str, Any]: Server acknowledgment of the command.
    """
    return self._send(
        "add_region",
        type="circle",
        x=x,
        y=y,
        radius=radius,
        color=color,
        format=format,
        isBackground=is_background,
    )

add_ellipse(x, y, rx, ry, angle=0, color='#00ff00', format='image', is_background=False)

Draws an elliptical region on the image.

Parameters:

Name Type Description Default
x float

The X coordinate (or RA) of the ellipse's center.

required
y float

The Y coordinate (or Dec) of the ellipse's center.

required
rx float

The semi-major axis radius.

required
ry float

The semi-minor axis radius.

required
angle float

Rotation angle in degrees. Defaults to 0.

0
color str

Hex color code for the region outline. Defaults to "#00ff00".

'#00ff00'
format str

The coordinate system used. Defaults to "image".

'image'
is_background bool

Designates as a background region. Defaults to False.

False

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Server acknowledgment of the command.

Source code in fviewer/plugins/regions.py
def add_ellipse(
    self,
    x: float,
    y: float,
    rx: float,
    ry: float,
    angle: float = 0,
    color: str = "#00ff00",
    format: str = "image",
    is_background: bool = False,
) -> Dict[str, Any]:
    """
    Draws an elliptical region on the image.

    Args:
        x (float): The X coordinate (or RA) of the ellipse's center.
        y (float): The Y coordinate (or Dec) of the ellipse's center.
        rx (float): The semi-major axis radius.
        ry (float): The semi-minor axis radius.
        angle (float): Rotation angle in degrees. Defaults to 0.
        color (str): Hex color code for the region outline.
            Defaults to "#00ff00".
        format (str): The coordinate system used. Defaults to "image".
        is_background (bool): Designates as a background region.
            Defaults to False.

    Returns:
        Dict[str, Any]: Server acknowledgment of the command.
    """
    return self._send(
        "add_region",
        type="ellipse",
        x=x,
        y=y,
        rx=rx,
        ry=ry,
        angle=angle,
        color=color,
        format=format,
        isBackground=is_background,
    )

clear_regions()

Removes all currently drawn regions from the canvas.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary containing the status of the operation

Dict[str, Any]

(e.g., {"status": "ok"}).

Source code in fviewer/plugins/regions.py
def clear_regions(self) -> Dict[str, Any]:
    """
    Removes all currently drawn regions from the canvas.

    Returns:
        Dict[str, Any]: A dictionary containing the status of the operation
        (e.g., `{"status": "ok"}`).
    """
    return self._send("clear_regions")

get_regions(format='image')

Retrieves a list of all currently drawn regions from the viewer.

Parameters:

Name Type Description Default
format str

The coordinate system to return the regions in. Valid options are 'image' (pixel coordinates), 'physical', 'fk5' (RA/Dec), or 'wcs'. Defaults to "image".

'image'

Returns:

Type Description
List[Dict[str, Any]]

List[Dict[str, Any]]: A list of dictionaries, where each dictionary

List[Dict[str, Any]]

represents a region and its properties (type, coordinates,

List[Dict[str, Any]]

color, etc.).

Raises:

Type Description
ValueError

If an unsupported format string is provided.

Source code in fviewer/plugins/regions.py
def get_regions(self, format: str = "image") -> List[Dict[str, Any]]:
    """
    Retrieves a list of all currently drawn regions from the viewer.

    Args:
        format (str): The coordinate system to return the regions in.
            Valid options are 'image' (pixel coordinates), 'physical',
            'fk5' (RA/Dec), or 'wcs'. Defaults to "image".

    Returns:
        List[Dict[str, Any]]: A list of dictionaries, where each dictionary
        represents a region and its properties (type, coordinates,
        color, etc.).

    Raises:
        ValueError: If an unsupported format string is provided.
    """
    valid_formats = ["image", "physical", "fk5", "wcs"]
    if format not in valid_formats:
        raise ValueError(
            "format must be 'image', 'physical', 'fk5', or 'wcs'"
        )

    return self._send("get_regions", format=format).get("regions", [])

load_regions(filepath)

Reads a local DS9 .reg file and applies it to the viewer.

Parameters:

Name Type Description Default
filepath str

The absolute or relative path to the region file on disk.

required

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: Server acknowledgment after parsing and drawing.

Source code in fviewer/plugins/regions.py
def load_regions(self, filepath: str) -> Dict[str, Any]:
    """
    Reads a local DS9 `.reg` file and applies it to the viewer.

    Args:
        filepath (str): The absolute or relative path to the
            region file on disk.

    Returns:
        Dict[str, Any]: Server acknowledgment after parsing and drawing.
    """
    with open(filepath, "r") as f:
        content = f.read()

    return self._send("load_regions_from_string", content=content)

save_regions(filepath, format='image')

Fetches regions from the viewer and saves them to a .reg file.

This method requests a formatted DS9 region string from the React frontend and writes it securely to the local disk. It includes safeguards against unsupported extensions and excessively large memory payloads.

Parameters:

Name Type Description Default
filepath str

The destination path for the saved file. Must end with '.reg' or '.txt'.

required
format str

The coordinate system to save the regions in. Options: 'image' or 'fk5'. Defaults to "image".

'image'

Returns:

Type Description
Dict[str, str]

Dict[str, str]: A dictionary containing the status and the saved file path.

Raises:

Type Description
ValueError

If an invalid format or unsafe file extension is provided.

RuntimeError

If the region payload returned by the UI exceeds 5MB.

Source code in fviewer/plugins/regions.py
def save_regions(
    self, filepath: str, format: str = "image"
) -> Dict[str, str]:
    """
    Fetches regions from the viewer and saves them to a `.reg` file.

    This method requests a formatted DS9 region string from the React
    frontend and writes it securely to the local disk. It includes
    safeguards against unsupported extensions and excessively large
    memory payloads.

    Args:
        filepath (str): The destination path for the saved file.
            Must end with '.reg' or '.txt'.
        format (str): The coordinate system to save the regions in.
            Options: 'image' or 'fk5'. Defaults to "image".

    Returns:
        Dict[str, str]: A dictionary containing the status and the
            saved file path.

    Raises:
        ValueError: If an invalid format or unsafe file extension is
            provided.
        RuntimeError: If the region payload returned by the UI exceeds 5MB.
    """
    if format not in ["image", "physical", "fk5"]:
        raise ValueError("format must be 'image' or 'fk5'")

    safe_exts = (".reg", ".txt")
    if not filepath.lower().endswith(safe_exts):
        raise ValueError(
            f"For security, filepath must end with {safe_exts}"
        )

    response = self._send("get_regions_string", format=format)
    content = response.get("content", "")

    max_bytes = 5 * 1024 * 1024
    if len(content.encode("utf-8")) > max_bytes:
        raise RuntimeError(
            "Received region data exceeds 5MB limit. Aborting save."
        )

    with open(filepath, "w") as f:
        f.write(content)

    return {"status": "ok", "file": filepath}

fviewer.plugins.datacube

DataCubeMixin

Source code in fviewer/plugins/datacube.py
class DataCubeMixin:
    def set_slice(self, slice_indices: list[int]):
        """
        Set the current frame/slice for multidimensional FITS data cubes.

        Args:
            slice_indices: A list of 1-based indices for the extra dimensions.
                           For a 3D cube (X, Y, Z), pass [Z].
                           For a 4D cube (X, Y, Z, W), pass [Z, W].

        Example:
            viewer.set_slice([42])     # Move to Z=42
            viewer.set_slice([42, 5])  # Move to Z=42, W=5
        """
        if not isinstance(slice_indices, list):
            # Allow users to pass a single integer for 3D cubes convenience
            slice_indices = [slice_indices]

        return self._send("set_slice", sliceIndices=slice_indices)

set_slice(slice_indices)

Set the current frame/slice for multidimensional FITS data cubes.

Parameters:

Name Type Description Default
slice_indices list[int]

A list of 1-based indices for the extra dimensions. For a 3D cube (X, Y, Z), pass [Z]. For a 4D cube (X, Y, Z, W), pass [Z, W].

required
Example

viewer.set_slice([42]) # Move to Z=42 viewer.set_slice([42, 5]) # Move to Z=42, W=5

Source code in fviewer/plugins/datacube.py
def set_slice(self, slice_indices: list[int]):
    """
    Set the current frame/slice for multidimensional FITS data cubes.

    Args:
        slice_indices: A list of 1-based indices for the extra dimensions.
                       For a 3D cube (X, Y, Z), pass [Z].
                       For a 4D cube (X, Y, Z, W), pass [Z, W].

    Example:
        viewer.set_slice([42])     # Move to Z=42
        viewer.set_slice([42, 5])  # Move to Z=42, W=5
    """
    if not isinstance(slice_indices, list):
        # Allow users to pass a single integer for 3D cubes convenience
        slice_indices = [slice_indices]

    return self._send("set_slice", sliceIndices=slice_indices)