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. π£
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.
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.
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
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.
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
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'
-
Create a new repository on GitHub.
-
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
-
You should see your files in the GitHub repo now.
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
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.
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:
- If you don't already have one, create an account at PyPI
- Verify your email address with PyPI, (if you haven't already)
- 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!
- 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!
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 withgit 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
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.
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
.
Documentation generation is not included in this template. We recommend following the getting started guides for one of the following documentation generation tools:
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
If you encounter any problems with this template, please file an issue along with a detailed description.
Distributed under the terms of the BSD-3 license, napari-plugin-template
is free and open source software.
π‘ 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>