napari macro-kit


Executable script generation for napari plugins

License BSD-3 PyPI Python Version tests codecov napari hub

Executable script generation for napari plugins.

Example showing the real-time recording of GUI operation.

This napari plugin aims at making image analysis reproducible with arbitrary input/output types.


Create a macro object, decorate functions with record method and run!

from napari_macrokit import get_macro

macro = get_macro("my-plugin-specifier")  # get macro object

# define a function
def add(a: float, b: float) -> float:
    return a + b

# run
result = add(3.2, 5.4)
add(result, 1.0)


# Out:
# >>> float0 = add(3.2, 5.4)
# >>> float1 = add(float0, 1.0)

Record GUI Operations

You can use recordable functions in your widgets to keep tracks of GUI operations. More simply, you can double-decorate functions with record and magicgui.

import numpy as np
from magicgui import magicgui
import napari
from napari.types import ImageData
from napari_macrokit import get_macro

macro = get_macro("my-plugin-specifier")  # get macro object

# define recordable magicgui
def add(image: ImageData, b: float) -> ImageData:
    return image + b

viewer = napari.Viewer()  # launch a viewer
viewer.add_image(np.random.random((100, 100)))  # image data
viewer.window.add_dock_widget(add)  # add magicgui to the viewer

Running add twice in GUI and you'll find macro updated like below.

# Out
# >>> image0 = add(viewer.layers['Image'].data, 0.06)
# >>> image1 = add(image0, 0.12)

Combining Plugins

Suppose you have two modules that use napari-macrokit.


from napari.types import ImageData
from scipy import ndimage as ndi
from napari_macrokit import get_macro

macro = get_macro("napari-module-0")

def gaussian_filter(image: ImageData, sigma: float) -> ImageData:
    return ndi.gaussian_filter(image, sigma=sigma)

def threshold(image: ImageData, value: float) -> ImageData:
    return image > value

from napari.types import ImageData
import numpy as np
from napari_macrokit import get_macro
macro = get_macro("napari-module-1")

def estimate_background(image: ImageData) -> float:
    return np.percentile(image, 10.0)

You can use functions from both modules to build an analysis workflow by collecting existing macro objects with collect_macro function. All the recordable actions in the modules will also be recorded to the returned macro object.

import numpy as np
from napari_macrokit import collect_macro
from napari_module_0 import gaussian_filter, threshold
from napari_module_1 import estimate_background

# global_macro will record all the macro available at this point
global_macro = collect_macro()

# start image analysis!
image = np.random.random((100, 100))

out = gaussian_filter(image, 2.0)
thresh = estimate_background(out)
binary = threshold(out, thresh)

# Out
# >>> image0 = gaussian_filter(arr0, 2.0)
# >>> float0 = estimate_background(image0)
# >>> image1 = threshold(image1, float0)

This napari plugin was generated with Cookiecutter using @napari's cookiecutter-napari-plugin template.


You can install napari-macrokit via pip:

pip install napari-macrokit

To install latest development version :

pip install git+


Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.


Distributed under the terms of the BSD-3 license, "napari-macrokit" is free and open source software


If you encounter any problems, please file an issue along with a detailed description.


  • 0.0.1

Last updated:

  • 27 January 2023

First released:

  • 27 January 2023


  • BSD-3-Clause

Supported data:

  • Information not submitted

Plugin type:

GitHub activity:

  • Stars: 2
  • Forks: 0
  • Issues + PRs: 0

Python versions supported:

Operating system:


  • numpy
  • magicgui
  • qtpy
  • macro-kit (>=0.4.0)

Sign up to receive updates