Skip to content

napari/napari-plugin-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

napari-plugin-template

Test plugin template Supported Python versions License npe2 Copier

napari-plugin-template is a convenient copier template for authoring (npe2-based) napari plugins.

Creating a plugin is a great way to extend napari's functionality. This repo provides a template to simplify the development and distribution your plugin. After creating a new plugin, we encourage you to send a post on our Zulip forum to notify the napari community.


πŸ“£ NOTE: This repo is not meant to be cloned/forked directly! Instead, the copier application will be used to execute the template and ask you for configuration information (or you may accept the template's sensible defaults). Please read Getting Started below. πŸ“£


Features

Using the napari-plugin-template offers the following benefits:

  • πŸš€Installable PyPI package
  • πŸ§ͺtox test suite, testing various python versions and platforms.
  • πŸ—’οΈREADME.md file that contains useful information about your plugin
  • βš™οΈContinuous integration configuration for github actions that handles testing and deployment of tagged releases
  • πŸ”‹git-tag-based version management with setuptools_scm
  • πŸͺͺChoose from several licenses, including BSD-3, MIT, MPL v2.0, Apache v2.0, GNU GPL v3.0, or GNU LGPL v3.0

The napari-plugin-template can be applied to pre-existing Python projects; consider using this template to add any of the above features, including plugin functionality, to your project. Conflicts between your current project and the template will be recognized after the template is complete, allowing you to choose what files to overwrite.

Getting Started

These instructions will walk you through how to create a napari plugin. It uses an application called copier to prompt you for configuration input and does the work of creating a functioning, distributable plugin from your source code.

Step 1: Install the template tools.

This step installs the Copier application and the jinja2-time extension. It also installs the napari plugin engine npe2, to help validate your new plugin is configured correctly.

Using conda:

conda create -y --name copier-env python=3.12 copier jinja2-time npe2
conda activate copier-env

Or using venv and pip:

python -m venv .venv
source .venv/bin/activate
python -m pip install copier jinja2-time npe2

Step 2: Create a new napari plugin project.

The next command will use copier to use the napari-plugin-template to generate a new napari plugin project:

copier copy --trust https://github.com/napari/napari-plugin-template <new-plugin-name>

For example, if you want to create a new plugin with the name, napari-growth-cone-finder, you would enter:

copier copy --trust https://github.com/napari/napari-plugin-template napari-growth-cone-finder

Copier will create a new folder in your current working directory named napari-growth-cone-finder. It will also prompt you to begin entering configuration information.

Step 3: Enter plugin configuration information.

Copier prompts you to enter information about your plugin. For more detailed information on each prompt see the prompts reference.

Configuration prompts
copier copy --trust https://github.com/napari/napari-plugin-template napari-growth-cone-finder

Welcome to the napari plugin template!
This template will help you create a new napari plugin with all the necessary structure of a Python package. 

For more detailed information about each prompt, see:
https://github.com/napari/napari-plugin-template/blob/main/PROMPTS.md

🎀 The name of your plugin, used to name the package and repository
   napari-growth-cone-finder
🎀 Display name for your plugin in the napari GUI
   Growth Cone Finder
🎀 Plugin module name, usually the same as the name of the package, but lowercase and with underscores
   napari_growth_cone_finder
🎀 Short description of what your plugin does
   A simple plugin for napari
🎀 Email address
   [email protected]
🎀 Developer name
   Ramon y Cajal
🎀 Github user or organisation name
   creator
🎀 Github repository URL
   https://github.com/creator/napari-growth-cone-finder
🎀 Include reader plugin?
   Yes
🎀 Include writer plugin?
   Yes
🎀 Include sample data plugin?
   Yes
🎀 Include widget plugin?
   Yes
🎀 Use git tags for automatic versioning?
   Yes
🎀 Install pre-commit? (Code formatting checks)
   Yes
🎀 Install dependabot? (Automatic security updates of dependency versions)
   Yes
🎀 Which licence do you want your plugin code to have?
   BSD-3

Select license:
1 - BSD-3
2 - MIT
3 - Mozilla Public License 2.0
4 - Apache Software License 2.0
5 - GNU LGPL v3.0
6 - GNU GPL v3.0
Choose from 1, 2, 3, 4, 5, 6 (1, 2, 3, 4, 5, 6) [1]: 1

After entering the configuration information, the following output will be displayed:

Output
Copying from template version 0.0.0.post126.dev0+95d5ece
    create  .napari-hub
    create  .napari-hub/DESCRIPTION.md
    create  .napari-hub/config-yml
    create  .pre-commit-config.yaml
    create  README.md
    create  MANIFEST.in
    create  LICENSE
    create  .gitignore
    create  .github
    create  .github/workflows
    create  .github/workflows/test_and_deploy
    create  .github/dependabot.yml
    create  tox.ini
    create  pyproject.toml
    create  src
    create  src/napari_growth_cone_finder
    create  src/napari_growth_cone_finder/_writer.py
    create  src/napari_growth_cone_finder/_tests
    create  src/napari_growth_cone_finder/_tests/test_sample_data.py
    create  src/napari_growth_cone_finder/_tests/test_writer.py
    create  src/napari_growth_cone_finder/_tests/__init__.py
    create  src/napari_growth_cone_finder/_tests/test_widget.py
    create  src/napari_growth_cone_finder/_tests/test_reader.py
    create  src/napari_growth_cone_finder/_sample_data.py
    create  src/napari_growth_cone_finder/napari.yaml
    create  src/napari_growth_cone_finder/__init__.py
    create  src/napari_growth_cone_finder/_reader.py
    create  src/napari_growth_cone_finder/_widget.py

 > Running task 1 of 1: ['/Users/creator/Code/repos-napari/.venv/bin/python3', '/private/var/folders/hg/l3v3xynd45sbvd141f3rqh600000gn/T/copier.vcs.clone.i5ou6e_q/_tasks.py', '--plugin_name=napari-growth-cone-finder', '--module_name=napari_growth_cone_finder', '--project_directory=napari-growth-cone-finder', '--install_precommit=True', '--github_repository_url=https://github.com/creator/napari-growth-cone-finder', '--github_username_or_organization=creator']
INFO:pre_gen_project:b"\xe2\x9c\x94 Manifest for 'Growth Cone Finder' valid!"
Switched to a new branch 'main'
install pre-commit ...
/Users/creator/Code/repos-napari/.venv/bin/python: No module named pip
updating pre-commit...
install pre-commit hook...
pre-commit installed at .git/hooks/pre-commit

Your plugin template is ready!  Next steps:
1. `cd` into your new directory
    cd napari-growth-cone-finder
    # you probably want to install your new package into your env
    pip install -e .

    2. Create a github repository with the name 'napari-growth-cone-finder':
    https://github.com/creator/napari-growth-cone-finder.git
    3. Add your newly created github repo as a remote and push:
        git remote add origin https://github.com/creator/napari-growth-cone-finder.git
        git push -u origin main
    4. The following default URLs have been added to `pyproject.toml`:
        Bug Tracker = https://github.com/creator/napari-growth-cone-finder/issues
        Documentation = https://github.com/creator/napari-growth-cone-finder#README.md
        Source Code = https://github.com/creator/napari-growth-cone-finder
        User Support = https://github.com/creator/napari-growth-cone-finder/issues
        These URLs will be displayed on your plugin's napari hub page.
        You may wish to change these before publishing your plugin!
    5. Read the README for more info: https://github.com/napari/napari-plugin-template
    6. We've provided a template description for your plugin page on the napari hub at `.napari-hub/DESCRIPTION.md`.
    You'll likely want to edit this before you publish your plugin.
    7. Consider customizing the rest of your plugin metadata for display on the napari hub:
    https://github.com/chanzuckerberg/napari-hub/blob/main/docs/customizing-plugin-listing.md

πŸŽ‰ You just created a minimal napari plugin, complete with tests and ready for automatic deployment! πŸŽ‰

napari-growth-cone-finder/
β”‚
β”œβ”€β”€ .github
β”‚   |── workflows
β”‚   |  └── test_and_deploy.yml
|   └── dependabot.yml
β”œβ”€β”€ LICENSE
β”œβ”€β”€ MANIFEST.in
β”œβ”€β”€ napari_growth_cone_finder
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ _widget.py
β”‚   β”œβ”€β”€ _reader.py
β”‚   β”œβ”€β”€ napari.yaml
β”‚Β Β  └── _tests
β”‚Β Β      β”œβ”€β”€ __init__.py
β”‚Β Β      β”œβ”€β”€ test_widget.py
β”‚Β Β      └── test_reader.py
β”œβ”€β”€ pyproject.toml
β”œβ”€β”€ README.md
└── tox.ini

Step 4: Initialize a git repository in your source code

After Step 3, your system has a file tree in a directory with the source files for your package. If you haven't already, be sure to set up Git so that git can be used from the command line or from a GUI such as Github Desktop or VSCode. This step will initialize the file directory as a git repo and commit your files to the repo. When in the command line a single . is equivalent to 'perform this action in the current directory'. So, git add . would stage the changes (i.e., the new file tree) in the current directory to the newly initalized git repo. This . will be used in other steps, too.

NOTE: This is important not only for version management, but also if you want to pip install your package locally for testing with pip install -e .. (because the version of your package is managed using git tags, see below)

cd napari-growth-cone-finder
git init
git add .
git commit -m 'initial commit'

Step 5: Upload your repo to GitHub

  1. Create a new repository on GitHub.

  2. Add your newly created GitHub repo as a git remote and push the commited files to GitHub:

    # here, continuing with the example above...
    # but replace with your own username and repo name
    
    git remote add origin https://github.com/neuronz52/napari-growth-cone-finder.git
    git push -u origin main
  3. You should see your files in the GitHub repo now.

Understanding and maintaining the generated plugin

Running tests locally

Tests are automatically setup to run on GitHub when you push changes to your repository.

You may also run your tests locally with pytest. You'll need to make sure that your package is installed in your environment, along with testing requirements (specified in the pyproject.toml project.optional-dependencies section):

pip install -e ".[testing]"
pytest

Automated testing and coverage

The repository should already be setup to run your tests each time you push an update (configuration is in .github/workflows/test_and_deploy.yml). You can monitor them in the "Actions" tab of your github repository. If you're following along, go have a look... they should be running right now!

Currently, the timeout for these runs is set to 30 minutes to save resources. You can modify the settings if necessary. Here you can find information on GitHub workflows and the timeout parameter.

When the tests are done, test coverage will be viewable at codecov.io (assuming your repository is public): https://codecov.io/gh/<your-github-username>/<your-package-name>

You will need to enable the codecov github app for this to work. See codecov installation docs to install the codecov github app and give it access to your napari plugin repository.

Set up automatic deployments

Your new package is also nearly ready to automatically deploy to PyPI (whenever you create a tagged release), so that your users can simply pip install your package. You just need to create an API token to authenticate with PyPi, and then add it to your github repository:

  1. If you don't already have one, create an account at PyPI
  2. Verify your email address with PyPI, (if you haven't already)
  3. Generate an API token at PyPi: In your account settings go to the API tokens section and select "Add API token". Make sure to copy it somewhere safe!
  4. Create a new encrypted secret" in your github repository with the name "TWINE_API_KEY", and paste in your API token.

You are now setup for automatic deployment!

Automatic deployment and version management

Each time you want to deploy a new version, you just need to create a tagged commit, and push it to your main branch on github. Your package is set up to use setuptools_scm for version management, meaning you don't need to hard-code your version anywhere in your package. It will be inferred from the tag each time you release.

# the tag will be used as the version string for your package
# make it meaningful: https://semver.org/
git tag -a v0.1.0 -m "v0.1.0"

# make sure to use follow-tags so that the tag also gets pushed to github
git push --follow-tags

Note: as of git 2.4.1, you can set follow-tags as default with git config --global push.followTags true

Monitor the "actions" tab on your github repo for progress... and when the "deploy" step is finished, your new version should be visible on pypi:

https://pypi.org/project/<your-package-name>/

and available for pip install with:

# for example
pip install napari-growth-cone-finder

Check code style with Pre-commit

This template includes a default yaml configuration for pre-commit. Among other things, it includes checks for best practices in napari plugins. You may edit the config at .pre-commit-config.yaml

To use it run:

pip install pre-commit
pre-commit install

You can also have these checks run automatically for you when you push to github by installing pre-commit ci on your repository.

Receive Dependabot notifications about dependencies

This template also includes a default yaml configuration for Dependabot. This can help you check for security updates to easily update vulnerable dependencies.

You will still need to enable Dependabot in your github settings, see the instructions at this link. Your Dependabot configuration file is located at .github/dependabot.yml.

Create your user documentation

Documentation generation is not included in this template. We recommend following the getting started guides for one of the following documentation generation tools:

  1. Sphinx
  2. MkDocs
  3. JupyterBook

Resources

Please consult the napari plugin docs for more information on how to create a plugin.

Details on why this plugin template is using the src layout can be found here and here

Issues

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

License

Distributed under the terms of the BSD-3 license, napari-plugin-template is free and open source software.

Using uv for getting started

πŸ’‘ Optional πŸ’‘

uv can reduce complexity since it will automatically install and manage a version of Python.

If you prefer using uv, the following commands are used for getting started:

uv venv
source .venv/bin/activate
uv pip install copier jinja2-time npe2
copier copy --trust https://github.com/napari/napari-plugin-template <new-plugin-name>

About

A template repo used to make new napari plugins

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published