Skip to main content

Run any Python code quality tool on a Jupyter Notebook!

Project description

https://github.com/MarcoGorelli/nbQA/raw/master/assets/logo.png

nbQA

https://dev.azure.com/megorelli/megorelli/_apis/build/status/MarcoGorelli.nbQA?branchName=master https://img.shields.io/azure-devops/coverage/megorelli/megorelli/1/master.svg https://badge.fury.io/py/nbqa.svg https://readthedocs.org/projects/nbqa/badge/?version=latest&style=plastic https://img.shields.io/pypi/pyversions/nbqa.svg

Adapter to run any code-quality tool on a Jupyter notebook. Documentation is hosted here.

Prerequisites

If you don’t have pip installed, this Python installation guide can guide you through the process.

Installation

Install nbqa with

$ pip install nbqa

Check your installation with

$ nbqa --version
nbqa 0.1.7

Quickstart

The general syntax is

nbqa <command> <notebook or directory> <args>

, where command is any Python code quality tool. For example, you could run:

$ nbqa flake8 my_notebook.ipynb
$ nbqa black my_notebook.ipynb --check
$ nbqa mypy my_notebook.ipynb --ignore-missing-imports
$ nbqa pytest my_notebook.ipynb --doctest-modules

You can also pass an entire directory instead of a single file, e.g. nbqa flake8 my_notebooks.

Examples

Format your notebooks using black:

$ nbqa black . --line-length=96
reformatted tweet-sentiment-roberta-pytorch.ipynb
All done!  🍰 
1 files reformatted.

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 ===============================

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:

Configuration

You can pass extra configurations to your tools either via the command line (as in the examples above), or in a .nbqa.ini file, which could look something like this:

[black]
line-length=96

[flake8]
max-line-length=88
ignore=E203,W503,W504

Flags from this .ini will be passed to nbqa as they’re written.

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/MarcoGorelli/nbQA-mirror-0
  rev: master
  hooks:
    - id: nbqa
      args: ['flake8']
      name: nbqa-flake8
- repo: https://github.com/MarcoGorelli/nbQA-mirror-1
  rev: master
  hooks:
    - id: nbqa
      args: ['isort']
      name: nbqa-isort
- repo: https://github.com/MarcoGorelli/nbQA-mirror-2
  rev: master
  hooks:
    - id: nbqa
      args: ['mypy']
      name: nbqa-mypy

It’s entirely optional which tool to use with which mirror, the purpose of having these mirror is to avoid duplicate keys in the .pre-commit.yaml file, but they’re all the same.

See Also

Here are some other code quality tools for Jupyter Notebooks:

Project template from cookiecutter.

History

0.1.2 (2020-07-17)

  • First release on PyPI.

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

nbqa-0.1.7.tar.gz (197.7 kB view details)

Uploaded Source

Built Distribution

nbqa-0.1.7-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file nbqa-0.1.7.tar.gz.

File metadata

  • Download URL: nbqa-0.1.7.tar.gz
  • Upload date:
  • Size: 197.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for nbqa-0.1.7.tar.gz
Algorithm Hash digest
SHA256 9c9daf9eee6b60c866c4ab0bf59ece835e1dad7d6b32792eb3302be7a58f704d
MD5 e1fe26873d9296d735b544d965a3a0fa
BLAKE2b-256 aaf9bde74baf5db6b38139a6b7f347c7adc1a6077c9469310debd2fe5babf5cc

See more details on using hashes here.

Provenance

File details

Details for the file nbqa-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: nbqa-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 15.0 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/44.0.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

File hashes

Hashes for nbqa-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 0aa447f32793850e19eaa259fd9e863816bee9bf321bb81045b87d769380041b
MD5 18b690b092f48f6f22145f17899d314d
BLAKE2b-256 15a45a945efdf2e33b315d648b424c00349876e8b21d8d39677fddd03e5185f7

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page