Skip to article frontmatterSkip to article content

Workshop 2: Extending napari with Scripts

Level: Intermediate
Duration: 3-4 hours

Overview

This workshop teaches you how to extend napari’s functionality through scripting and custom code. You’ll learn to create interactive widgets using magicgui, respond to layer events, handle mouse interactions, and customize napari’s appearance. This workshop bridges the gap between using napari as a GUI application and developing custom analysis tools, providing the foundation needed for plugin development.

Learning Objectives

By completing this workshop, you will be able to:

Prerequisites

Workshop Structure

This workshop is organized into six main modules that progressively build your skills:

1. Scripting Basics with napari

Transition from GUI-based interaction to programmatic control of napari.

Topics covered:

2. Exploratory Analysis: Spot Detection

Develop an exploratory analysis workflow for detecting spots in microscopy data.

Topics covered:

3. From Functions to Widgets

Transform your analysis functions into interactive widgets using magicgui.

Topics covered:

4. Mouse Callbacks & Layer Events

Create interactive tools that respond to user actions and layer changes.

Topics covered:

5. Custom Colormaps

Customize napari’s appearance and improve data visualization.

Topics covered:

6. Keybindings (Coming Soon)

Add custom keyboard shortcuts to streamline your workflows.

Topics covered:

Key Concepts

magicgui: Function-to-GUI Magic

magicgui is the secret sauce that makes creating napari widgets easy. It automatically generates GUI elements from Python function signatures:

from magicgui import magicgui

@magicgui
def threshold_image(image: "napari.layers.Image", threshold: float = 0.5):
    return image.data > threshold

This creates a widget with an image layer dropdown and a threshold slider!

Event-Driven Architecture

napari uses an event-driven architecture via the psygnal library. Layers emit events when their properties change:

def update_on_data_change(event):
    print(f"Data changed for layer: {event.source.name}")

layer.events.data.connect(update_on_data_change)

Bridging GUI and Code

The power of napari comes from seamless integration between interactive GUI manipulation and programmatic control. Changes in code update the GUI, and GUI interactions are accessible from code.

Practical Applications

Skills from this workshop enable you to:

Getting Help

Next Steps

After completing this workshop: