napari.components.LayerList#

class napari.components.LayerList(data=())[source]#

Bases: SelectableEventedList[Layer]

List-like layer collection with built-in reordering and callback hooks.

Parameters:

data (iterable) – Iterable of napari.layer.Layer

Events:
  • inserting ((index: int)) – emitted before an item is inserted at index
    • inserted ((index: int, value: T)) – emitted after value is inserted at index

    • removing ((index: int)) – emitted before an item is removed at index

    • removed ((index: int, value: T)) – emitted after value is removed at index

    • moving ((index: int, new_index: int)) – emitted before an item is moved from index to new_index

    • moved ((index: int, new_index: int, value: T)) – emitted after value is moved from index to new_index

    • changed ((index: int, old_value: T, value: T)) – emitted when item at index is changed from old_value to value

    • changed <OVERLOAD> ((index: slice, old_value: List[_T], value: List[_T])) – emitted when item at index is changed from old_value to value

    • reordered ((value: self)) – emitted when the list is reordered (eg. moved/reversed).

    • selection.events.changed ((added: Set[_T], removed: Set[_T])) – emitted when the set changes, includes item(s) that have been added and/or removed from the set.

    • selection.events.active ((value: _T)) – emitted when the current item has changed.

    • selection.events._current ((value: _T)) – emitted when the current item has changed. (Private event)

inherited-members:

Methods

append(value)

S.append(value) -- append value to the end of the sequence

clear()

copy()

Return a shallow copy of the list.

count(value)

extend(values)

S.extend(iterable) -- extend sequence by appending elements from the iterable

get_extent(layers)

Return extent for a given layer list.

index(value[, start, stop])

Return first index of value.

insert(index, value)

Insert value before index.

link_layers([layers, attributes])

move(src_index[, dest_index])

Insert object at src_index before dest_index.

move_multiple(sources[, dest_index])

Move a batch of sources indices, to a single destination.

move_selected(index, insert)

Reorder list by moving the item at index and inserting it at the insert index.

pop([index])

Raise IndexError if list is empty or index is out of range.

remove(value)

S.remove(value) -- remove first occurrence of value.

remove_selected()

Remove selected layers from LayerList, but first unlink them.

reverse()

Reverse list IN PLACE.

save(path, *[, selected, plugin, _writer])

Save all or only selected layers to a path using writer plugins.

select_all()

Select all items in the list.

select_next([step, shift])

Selects next item from list.

select_previous([shift])

Selects previous item from list.

toggle_selected_visibility()

Toggle visibility of selected layers

unlink_layers([layers, attributes])

Attributes

extent

Extent of layers in data and world coordinates.

ndim

Maximum dimensionality of layers.

selection

Get current selection.

Details

property extent: Extent#

Extent of layers in data and world coordinates.

Extent bounds are inclusive. See Layer.extent for a detailed explanation of how extents are calculated.

get_extent(layers: Iterable[Layer]) Extent[source]#

Return extent for a given layer list.

Extent bounds are inclusive. This function is useful for calculating the extent of a subset of layers when preparing and updating some supplementary layers. For example see the cross Vectors layer in the multiple_viewer_widget example.

Parameters:

layers (list of Layer) – list of layers for which extent should be calculated

Returns:

extent – extent for selected layers

Return type:

Extent

insert(index: int, value: Layer)[source]#

Insert value before index.

property ndim: int#

Maximum dimensionality of layers.

Defaults to 2 if no data is present.

Returns:

ndim

Return type:

int

remove_selected()[source]#

Remove selected layers from LayerList, but first unlink them.

save(path: str, *, selected: bool = False, plugin: str | None = None, _writer: WriterContribution | None = None) list[str][source]#

Save all or only selected layers to a path using writer plugins.

If plugin is not provided and only one layer is targeted, then we directly call the corresponding``napari_write_<layer_type>`` hook (see single layer writer hookspecs) which will loop through implementations and stop when the first one returns a non-None result. The order in which implementations are called can be changed with the Plugin sorter in the GUI or with the corresponding hook’s bring_to_front() method.

If plugin is not provided and multiple layers are targeted, then we call napari_get_writer() which loops through plugins to find the first one that knows how to handle the combination of layers and is able to write the file. If no plugins offer napari_get_writer() for that combination of layers then the default napari_get_writer() will create a folder and call napari_write_<layer_type> for each layer using the Layer.name variable to modify the path such that the layers are written to unique files in the folder.

If plugin is provided and a single layer is targeted, then we call the napari_write_<layer_type> for that plugin, and if it fails we error.

If plugin is provided and multiple layers are targeted, then we call we call napari_get_writer() for that plugin, and if it doesn`t return a WriterFunction we error, otherwise we call it and if that fails if it we error.

Parameters:
  • path (str) – A filepath, directory, or URL to open. Extensions may be used to specify output format (provided a plugin is available for the requested format).

  • selected (bool) – Optional flag to only save selected layers. False by default.

  • plugin (str, optional) – Name of the plugin to use for saving. If None then all plugins corresponding to appropriate hook specification will be looped through to find the first one that can save the data.

  • _writer (WriterContribution, optional) – private: npe2 specific writer override.

Returns:

File paths of any files that were written.

Return type:

list of str

toggle_selected_visibility()[source]#

Toggle visibility of selected layers