|
| 1 | +# Matplotlib 3rd Party Package Cookiecutter |
| 2 | + |
| 3 | +Turn your matplotlib convenience scripts into a proper python package using this cookiecutter. |
| 4 | + |
| 5 | +``` |
| 6 | +pip install cookiecutter |
| 7 | +cookiecutter gh:ianhi/matplotlib-extension-cookiecutter |
| 8 | +``` |
| 9 | + |
| 10 | +## Building docs |
| 11 | +``` |
| 12 | +cd docs |
| 13 | +make watch |
| 14 | +``` |
| 15 | + |
| 16 | +## Setting up tests |
| 17 | + |
| 18 | +The best way to do testing for a Matplotlib extension is to use [pytest-mpl](https://github.com/matplotlib/pytest-mpl#about). This project inclues a basic test, but does not pregenerate any baselines images. To make the example test work you first need to generate the baseline with: |
| 19 | + |
| 20 | +``` |
| 21 | +pytest --mpl-generate-path=<package-name>/tests/baseline |
| 22 | +``` |
| 23 | + |
| 24 | +For full instructions see: https://github.com/matplotlib/pytest-mpl#using |
| 25 | + |
| 26 | +## Publicizing your package |
| 27 | + |
| 28 | +Once you've made your package other people will likely want to use your hard work, and maybe even contribute to it! But for this to happen they need to know about it. The Matplotlib devs also want you to share your package and like to amplify your advertising. So some great steps to take in order to share your package are: |
| 29 | + |
| 30 | +1. Make a PR to add it to Matplotlib's 3rd party packages page |
| 31 | + - [Example PR](https://github.com/matplotlib/matplotlib/pull/13076) |
| 32 | +2. Tweet about your pacakge and and mention `@matplotlib` for a retweet. |
| 33 | + |
| 34 | +## Releasing to PyPi |
| 35 | +### Manually |
| 36 | +First set ensure you have the right packages installed: |
| 37 | +``` |
| 38 | +pip install build twine |
| 39 | +``` |
| 40 | + |
| 41 | +Then generate an `sdist` and a `wheel`: |
| 42 | + |
| 43 | +1. bump version in `_version.py` |
| 44 | +2. `git add <...>/_version.py` |
| 45 | +3. `commit -m 'version bump'` |
| 46 | +4. `git tag <version number>` |
| 47 | +5. `git push --tags` |
| 48 | +6. `python -m build -sdist -wheel` |
| 49 | +7. `twine upload dist/*` |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +### Using Github Actions |
| 54 | +If you use Github as your Git repository then you can also automate steps 4-7 by using the Github action included in the cookiecutter. |
| 55 | + |
| 56 | +To do this you will need to generate a PyPI api token and add it to your repository's secrets. |
| 57 | + |
| 58 | +**Generating an api token** |
| 59 | + |
| 60 | +Go to you [PyPI account setttings](https://pypi.org/manage/account/), scroll down to the API tokens section and select "Add API token". |
| 61 | + |
| 62 | +**Adding api token to Github Secrets** |
| 63 | +Once you've have copied the token from PyPI go the `Secrets` section of your Github repo's settings and add the token with the name `PYPI_API_TOKEN` |
| 64 | + |
| 65 | + |
| 66 | +With that set up all you need to do to create a release is: |
| 67 | +1. bump version in `_version.py` |
| 68 | +2. `git add <...>/_version.py` |
| 69 | +3. `git commit -m 'version bump' && git push` |
| 70 | +4. Make a release using the github release tool. |
| 71 | + |
| 72 | +To make the release go the the `Releases` section in the repo sidebar: |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +then draft a new release: |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +After you fill out the information the Github action will create a new tag for you, build the wheel, and upload it to PyPI. |
| 81 | + |
| 82 | + |
| 83 | +## Miscellaneous Advice |
| 84 | + |
| 85 | +Do not use Matplotlib private methods. If you really need the functionality then consider opening a feature request to have Matplotlib provide a public API for what you want. |
| 86 | + |
| 87 | +There is some discussion of how to use Matplotlib docstrings on discourse: https://discourse.matplotlib.org/t/docs-for-a-method-wrapping-a-matplotlib-method/21055 |
| 88 | + |
| 89 | +https://colcarroll.github.io/yourplotlib/ is a great read for how to make an extension to Matplotlib. |
| 90 | + |
| 91 | +## LICENSE Advice |
| 92 | + |
| 93 | +You may end up using portions of Matplotlib's code or copying docstrings when making a Matplotlib extension. |
| 94 | + |
| 95 | +As a practical rule: If you end up copying a non-trivial amount of code or docs the safest course of action is to add the Matplotlib license to your project as a derived work. For example see how Matplotlib does it https://github.com/matplotlib/matplotlib/tree/master/LICENSE. |
| 96 | + |
| 97 | +> But what is non-trivial???? |
| 98 | +
|
| 99 | + |
| 100 | +practical copyright rules |
| 101 | + |
| 102 | + If it were homework and you didn't acknowledge would it be cheating? |
| 103 | + If yes then .... (e..g add a comment and include a license file in ___ folder) |
| 104 | + |
| 105 | + |
| 106 | +## Credit |
| 107 | + |
| 108 | +This cookiecutter is partially based on the following cookiecutters |
| 109 | +- https://github.com/jupyter-widgets/widget-ts-cookiecutter |
| 110 | +- The `setup.cfg` from https://github.com/napari/napari |
| 111 | + |
| 112 | +---- |
| 113 | +Happy Plotting! |
0 commit comments