|
| 1 | +# Template Repo |
| 2 | + |
| 3 | +[](https://test.pypi.org/project/repo-template) |
| 4 | +[](https://github.com/jorgemarpa/repo-template/actions/workflows/pytest.yaml/) [](https://github.com/jorgemarpa/repo-template/actions/workflows/mypy.yaml) [](https://github.com/jorgemarpa/repo-template/actions/workflows/ruff.yaml)[](https://github.com/jorgemarpa/repo-template/actions/workflows/deploy-mkdocs.yaml) |
| 5 | + |
| 6 | +This is a template repository ready to develop code. It uses poetry to manage dependencies, python environment, and packaging. Flake8 and Black to format code. Mkdocs to create documentation from docstring. And adds GitHub actions to automatize tasks. |
| 7 | + |
| 8 | +## Poetry |
| 9 | + |
| 10 | +[Poetry](https://python-poetry.org/docs/) helps you to manage a python package project. It manage dependencies, versions, and python environments. |
| 11 | +First initialize a project: |
| 12 | + |
| 13 | +``` |
| 14 | +poetry new --src <repo-name> |
| 15 | +``` |
| 16 | + |
| 17 | +This will create a new `repo-name` directory with default files and folders. |
| 18 | +We use the `--src` flag to use the source folder layout. |
| 19 | +In the repo directory we can add new dependencies with: |
| 20 | + |
| 21 | +``` |
| 22 | +poetry add numpy matplotlib pandas |
| 23 | +``` |
| 24 | + |
| 25 | +To install the ne package in a virtual env we can do: |
| 26 | + |
| 27 | +``` |
| 28 | +poetry install |
| 29 | +``` |
| 30 | +and to run other commands using the python env, e.g. `pytest`, we can do: |
| 31 | + |
| 32 | +``` |
| 33 | +poetry run pytest... |
| 34 | +``` |
| 35 | + |
| 36 | +To release and publish our package with poetry in PyPi we use: |
| 37 | +``` |
| 38 | +poetry build |
| 39 | +``` |
| 40 | +this will create the wheel files and package the repository in the `dist` folder |
| 41 | +Then after creating a new package in PyPi website and setting up the token we configure this into poetry: |
| 42 | + |
| 43 | +``` |
| 44 | +poetry config pypi-token.pypi <token> |
| 45 | +``` |
| 46 | +and then we publish with: |
| 47 | +``` |
| 48 | +poetry publish |
| 49 | +``` |
| 50 | +This will upload the package to the index. We will do this every time there is a new major or minor version of our package. This can be done also as a GitHub action, will add that later. |
| 51 | + |
| 52 | + |
| 53 | +## GitHub |
| 54 | + |
| 55 | +Initialize Git repository and sync with GitHub: |
| 56 | +In the project directory do |
| 57 | +``` |
| 58 | +git init |
| 59 | +``` |
| 60 | +to initialize the Git repo. Now we can add changes and commit to the main branch and sync with GitHub. |
| 61 | + |
| 62 | +``` |
| 63 | +git add * |
| 64 | +git commit -m "first commit" |
| 65 | +git remote add origin <GITHUB url> |
| 66 | +git push -u origin main |
| 67 | +``` |
| 68 | +More info [here](https://docs.github.com/en/repositories/creating-and-managing-repositories/quickstart-for-repositories?tool=webui). |
| 69 | + |
| 70 | +## MkDocs |
| 71 | + |
| 72 | +To create a documentation page using the docstring in our code we will use `mkdocs` [website](https://docs.readthedocs.io/en/stable/intro/getting-started-with-mkdocs.html). |
| 73 | +First, install the library in the virtual env: |
| 74 | +``` |
| 75 | +poetry add -G dev mkdocs mkdocstrings mkdocs-material |
| 76 | +``` |
| 77 | + |
| 78 | +Now we can initialize MkDocs with: |
| 79 | +``` |
| 80 | +poetry run mkdocs new . |
| 81 | +``` |
| 82 | +This will create the configuration file and docs folder to add to our page. |
| 83 | + |
| 84 | +To build the documentation as a page we use: |
| 85 | +``` |
| 86 | +poetry run mkdocs build |
| 87 | +poetry run mkdocs serve |
| 88 | +``` |
| 89 | +This will build the documentation and create a local view of the page to visualize in our browser. |
| 90 | + |
| 91 | +TO deploy the the page to Github branch with the documentation we do |
| 92 | +``` |
| 93 | +poetry run mkdocs gh-deploy |
| 94 | +``` |
| 95 | +this will create the website [https://jorgemarpa.github.io/repo-template/](https://jorgemarpa.github.io/repo-template/) which has the documentation we have created. |
| 96 | + |
| 97 | + |
| 98 | +## Pytest |
| 99 | + |
| 100 | +Pytest will run any test code that is written in the `tests` folder. Ideally we want to test all our code to ensure nothing breaks and that function behavior is as expected. We can run `pytest` locally before committing to the branch or PR with: |
| 101 | +``` |
| 102 | +poetry run pytest -v tests |
| 103 | +``` |
| 104 | + |
| 105 | +## GitHub Actions |
| 106 | + |
| 107 | +This are automatic actions that happens during pushing new commits and/or pull request. We can configure these actions in a `yaml` file in the `.github/workflows` directory. These actions automatize most of the routine steps described in this document. |
| 108 | + |
| 109 | +In this template we have the following workflows: |
| 110 | + - `ruff`: does code formatting, lint, and sorting. It replaces `flake8`, `isort`, and `black`. |
| 111 | + - `deploy-mkdocs`: creates documentation page from with Mkdocs. |
| 112 | + - `pytest`: runs python test |
| 113 | + - `dependabot`: check for security and version updates of project dependencies and creates PRs when new version are available. |
| 114 | + - `mypy`: check for python static programming. |
| 115 | + - `pypi-publish`: published the project in PyPI when a new GitHub release/tag is created |
| 116 | + |
| 117 | +### Publish to PyPI Action |
| 118 | + |
| 119 | +This repo has the action `pypi-publish.yaml` that automatically publish the package to TestPyPI once a |
| 120 | +release or a tag version is created in GitHub. Note that for this example we used |
| 121 | +[TestPyPI](https://test.pypi.org/) which is an instance of PyPI to test package deployment and publishing |
| 122 | +without affecting the real index. It is ideal to test things. |
| 123 | +We also need to setup a TestPyPI/PyPI API token and add it to GitHub secrets. |
| 124 | + |
| 125 | +When our package is ready to be publish to the real PyPI, we need to update the action as follows: |
| 126 | +``` |
| 127 | +- name: Config Poetry to testPyPI |
| 128 | + run: | |
| 129 | + poetry config repositories.test-pypi https://test.pypi.org/legacy/ |
| 130 | + poetry config pypi-token.test-pypi "${{ secrets.TEST_PYPI_API_KEY }}" |
| 131 | +- name: Publish package to testPyPI |
| 132 | + run: poetry publish -r test-pypi --build |
| 133 | +``` |
| 134 | +to |
| 135 | +``` |
| 136 | +- name: Config Poetry to PyPI |
| 137 | + run: | |
| 138 | + poetry config pypi-token.pypi "${{ secrets.PYPI_API_KEY }}" |
| 139 | +- name: Publish package to testPyPI |
| 140 | + run: poetry publish --build |
| 141 | +``` |
| 142 | +This will change te repository configuration back to publish to PyPI. Remember to add the PyPI |
| 143 | +API token to GitHub secrets. |
| 144 | + |
| 145 | +See this [tutorial](https://www.ianwootten.co.uk/2020/10/23/publishing-to-pypi-using-github-actions/) for more details. |
0 commit comments