Run any Python code quality tool on a Jupyter Notebook!
Project description
nbQA
Adapter to run any code-quality tool on a Jupyter notebook. This is intended to be run as a pre-commit hook and/or during continuous integration.
Documentation is hosted here.
Installation
Install nbqa with pip:
$ pip install nbqa
Quickstart
The general syntax is
nbqa <command> <notebook or directory> <args>
where command is any standard Python code quality tool.
Examples
Check static type annotations:
$ nbqa mypy tweet-sentiment-roberta-pytorch.ipynb --ignore-missing-imports
tweet-sentiment-roberta-pytorch.ipynb:cell_10:5: error: Argument "batch_size" to "get_test_loader" has incompatible type "str"; expected "int"
Check any examples in your docstrings are correct:
$ nbqa pytest tweet-sentiment-roberta-pytorch.ipynb --doctest-modules
============================= test session starts ==============================
platform linux -- Python 3.8.2, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /home/marco/tweet-sentiment-extraction
plugins: cov-2.10.0
collected 3 items
tweet-sentiment-roberta-pytorch.ipynb . [100%]
============================== 1 passed in 0.03s ===============================
Format your notebooks using black:
$ nbqa black . --line-length=96 --nbqa-mutate
reformatted tweet-sentiment-roberta-pytorch.ipynb
All done! ✨ 🍰 ✨
1 files reformatted.
Configuration
You can configure nbQA either at the command line, or by using a .nbqa.ini file. We’ll see some examples below.
Extra flags
If you wish to pass extra flags (e.g. --ignore W503 to flake8) you can either run
nbqa flake8 my_notebook.ipynb --ignore W503
or you can put the following in your .nbqa.ini file
[flake8]
addopts = --ignore W503
Config file
If you already have a config file for your third-party tool (e.g. .mypy.ini for mypy), you can run
nbqa mypy my_notebook.ipynb --nbqa-config .mypy.ini
or you can put the following in your .nbqa.ini file
[mypy]
config = .mypy.ini
Allow mutations
By default, nbQA won’t modify your notebooks. If you wish to let your third-party tool modify your notebooks, you can
either pass the --nbqa-mutate flag at the command-line, e.g.
nbqa black my_notebook.ipynb --nbqa-mutate
or you can put the following in your .nbqa.ini file
[black]
mutate = 1
Empty __init__.py files
Some tools, such as mypy, require (possibly empty) __init__.py files to be in each subdirectory you wish to analyse.
To make nbQA aware of this, you can either pass the --nbqa-preserve-init flag, e.g.
nbqa mypy my_dir/my_subdir/my_notebook.ipynb --nbqa-preserve-init
or you can put the following in your .nbqa.ini file
[mypy]
preserve_init = 1
Usage as pre-commit hook
If you want to use nbqa with pre-commit, here’s an example of what you
could add to your .pre-commit-config.yaml file:
- repo: https://github.com/nbQA-dev/nbQA
rev: 0.1.19
hooks:
- id: nbqa
args: ['flake8']
name: nbqa-flake8
additional_dependencies: ['flake8']
- id: nbqa
args: ['isort', '--nbqa-mutate']
name: nbqa-isort
additional_dependencies: ['isort']
- id: nbqa
args: ['mypy', '--nbqa-preserve-init']
name: nbqa-mypy
additional_dependencies: ['mypy']
Supported third party packages
In theory, nbqa can adapt any Python code-quality tool to a Jupyter Notebook.
In practice, here are the tools it’s been tested with:
See Also
Here are some other code quality tools for Jupyter Notebooks:
nbstripout (clear notebook cells’ outputs);
jupyterlab_code_formatter (Jupyter Lab plugin);
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nbqa-0.1.19.tar.gz.
File metadata
- Download URL: nbqa-0.1.19.tar.gz
- Upload date:
- Size: 19.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
edc67afa5236770e004a30094176c68031198661b688031291ac58d080ab16f5
|
|
| MD5 |
098a7b8e93b1decb5b432c14ac37be85
|
|
| BLAKE2b-256 |
d4529e64166a4e478e2a7b77151ee1df2eabef419757c90f4533007660cc3bc7
|
File details
Details for the file nbqa-0.1.19-py3-none-any.whl.
File metadata
- Download URL: nbqa-0.1.19-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7de4da53e9cfa06faccda4751bedddf3e2cd9c89e39620593ab372d0e06183a7
|
|
| MD5 |
6081fab48387d4de46c86e98fcc03bc3
|
|
| BLAKE2b-256 |
5547afef7f7bae6e5902ce1a3ae623fc683a0288367967a9b51cdfa0272b0189
|