2020-01-18 19:35:49 +01:00
First, you might want to see the basic ways to [help Typer and get help ](help-typer.md ){.internal-link target=_blank}.
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
## Developing
2019-12-24 13:31:59 +01:00
If you already cloned the repository and you know that you need to deep dive in the code, here are some guidelines to set up your environment.
2020-03-11 09:43:04 +01:00
### Virtual environment with `venv`
You can create a virtual environment in a directory using Python's `venv` module:
2020-03-29 14:26:54 +02:00
< div class = "termy" >
2020-03-11 09:43:04 +01:00
```console
$ python -m venv env
```
2019-12-24 13:31:59 +01:00
2020-03-29 14:26:54 +02:00
< / div >
2020-03-11 09:43:04 +01:00
That will create a directory `./env/` with the Python binaries and then you will be able to install packages for that isolated environment.
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
### Activate the environment
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
Activate the new environment with:
2020-04-18 12:38:12 +02:00
=== "Linux, macOS"
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
```console
$ source ./env/bin/activate
```
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< / div >
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
=== "Windows PowerShell"
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
```console
$ .\env\Scripts\Activate.ps1
```
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< / div >
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
=== "Windows Bash"
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
Or if you use Bash for Windows (e.g. < a href = "https://gitforwindows.org/" class = "external-link" target = "_blank" > Git Bash< / a > ):
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
```console
$ source ./env/Scripts/activate
```
< / div >
2020-03-29 14:26:54 +02:00
2020-03-11 09:43:04 +01:00
To check it worked, use:
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
=== "Linux, macOS, Windows Bash"
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
```console
$ which pip
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
some/directory/typer/env/bin/pip
```
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< / div >
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
=== "Windows PowerShell"
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
```console
$ Get-Command pip
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
some/directory/typer/env/bin/pip
```
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
< / div >
I f it shows the `pip` binary at `env/bin/pip` then it worked. 🎉
2020-03-29 14:26:54 +02:00
2020-03-11 09:43:04 +01:00
!!! tip
Every time you install a new package with `pip` under that environment, activate the environment again.
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
This makes sure that if you use a terminal program installed by that package (like `flit` ), you use the one from your local environment and not any other that could be installed globally.
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
### Flit
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
**Typer** uses < a href = "https://flit.readthedocs.io/en/latest/index.html" class = "external-link" target = "_blank" > Flit< / a > to build, package and publish the project.
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
After activating the environment as described above, install `flit` :
2019-12-24 13:31:59 +01:00
2020-03-29 14:26:54 +02:00
< div class = "termy" >
2020-03-11 09:43:04 +01:00
```console
$ pip install flit
2020-03-29 14:26:54 +02:00
---> 100%
2020-03-11 09:43:04 +01:00
```
2020-03-29 14:26:54 +02:00
< / div >
2020-03-11 09:43:04 +01:00
Now re-activate the environment to make sure you are using the `flit` you just installed (and not a global one).
And now use `flit` to install the development dependencies:
2020-04-18 12:38:12 +02:00
=== "Linux, macOS"
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
```console
$ flit install --deps develop --symlink
2020-03-11 09:43:04 +01:00
2020-04-18 12:38:12 +02:00
---> 100%
```
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< / div >
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
=== "Windows"
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
If you are on Windows, use `--pth-file` instead of `--symlink` :
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
< div class = "termy" >
2020-03-29 14:26:54 +02:00
2020-04-18 12:38:12 +02:00
```console
$ flit install --deps develop --pth-file
---> 100%
```
< / div >
2020-03-29 14:26:54 +02:00
2020-03-11 09:43:04 +01:00
It will install all the dependencies and your local Typer in your local environment.
#### Using your local Typer
If you create a Python file that imports and uses Typer, and run it with the Python from your local environment, it will use your local Typer source code.
2019-12-24 13:31:59 +01:00
2020-04-18 12:38:12 +02:00
And if you update that local Typer source code, as it is installed with `--symlink` (or `--pth-file` on Windows), when you run that Python file again, it will use the fresh version of Typer you just edited.
2020-03-11 09:43:04 +01:00
That way, you don't have to "install" your local version to be able to test every change.
2019-12-24 13:31:59 +01:00
### Format
There is a script that you can run that will format and clean all your code:
2020-03-29 14:26:54 +02:00
< div class = "termy" >
2020-03-11 09:43:04 +01:00
```console
$ bash scripts/format.sh
2019-12-24 13:31:59 +01:00
```
2020-03-29 14:26:54 +02:00
< / div >
2019-12-24 13:31:59 +01:00
2020-03-29 14:26:54 +02:00
It will also auto-sort all your imports.
2020-03-11 09:43:04 +01:00
2020-03-29 14:26:54 +02:00
For it to sort them correctly, you need to have Typer installed locally in your environment, with the command in the section above using `--symlink` (or `--pth-file` on Windows).
2020-03-11 09:43:04 +01:00
### Format imports
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
There is another script that formats all the imports and makes sure you don't have unused imports:
2020-03-29 14:26:54 +02:00
< div class = "termy" >
2020-03-11 09:43:04 +01:00
```console
$ bash scripts/format-imports.sh
2019-12-24 13:31:59 +01:00
```
2020-03-29 14:26:54 +02:00
< / div >
2020-03-11 09:43:04 +01:00
As it runs one command after the other and modifies and reverts many files, it takes a bit longer to run, so it might be easier to use `scripts/format.sh` frequently and `scripts/format-imports.sh` only before committing.
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
## Docs
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
The documentation uses < a href = "https://www.mkdocs.org/" class = "external-link" target = "_blank" > MkDocs< / a > .
2019-12-24 13:31:59 +01:00
All the documentation is in Markdown format in the directory `./docs` .
Many of the tutorials have blocks of code.
In most of the cases, these blocks of code are actual complete applications that can be run as is.
2020-07-09 22:09:53 +02:00
In fact, those blocks of code are not written inside the Markdown, they are Python files in the `./docs_src/` directory.
2019-12-24 13:31:59 +01:00
And those Python files are included/injected in the documentation when generating the site.
2020-03-11 09:43:04 +01:00
### Docs for tests
2019-12-24 13:31:59 +01:00
Most of the tests actually run against the example source files in the documentation.
This helps making sure that:
* The documentation is up to date.
* The documentation examples can be run as is.
2020-03-11 09:43:04 +01:00
* Most of the features are covered by the documentation, ensured by test coverage.
2019-12-24 13:31:59 +01:00
During local development, there is a script that builds the site and checks for any changes, live-reloading:
2020-03-29 14:26:54 +02:00
< div class = "termy" >
2020-03-11 09:43:04 +01:00
```console
$ bash scripts/docs-live.sh
2020-03-29 14:26:54 +02:00
< span style = "color: green;" > [INFO]< / span > - Building documentation...
< span style = "color: green;" > [INFO]< / span > - Cleaning site directory
< span style = "color: green;" > [INFO]< / span > - Documentation built in 2.74 seconds
< span style = "color: green;" > [INFO]< / span > - Serving on http://127.0.0.1:8008
2019-12-24 13:31:59 +01:00
```
2020-03-29 14:26:54 +02:00
< / div >
2020-03-11 09:43:04 +01:00
It will serve the documentation on `http://127.0.0.1:8008` .
2019-12-24 13:31:59 +01:00
That way, you can edit the documentation/source files and see the changes live.
2020-03-11 09:43:04 +01:00
## Tests
2019-12-24 13:31:59 +01:00
2020-03-11 09:43:04 +01:00
There is a script that you can run locally to test all the code and generate coverage reports in HTML:
2019-12-24 13:31:59 +01:00
2020-03-29 14:26:54 +02:00
< div class = "termy" >
2020-03-11 09:43:04 +01:00
```console
$ bash scripts/test-cov-html.sh
2019-12-24 13:31:59 +01:00
```
2020-03-29 14:26:54 +02:00
< / div >
2020-03-11 09:43:04 +01:00
This command generates a directory `./htmlcov/` , if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing.