Skip to main content

Run any Python code quality tool on a Jupyter Notebook!

Project description

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

nbQA

https://github.com/nbQA-dev/nbQA/workflows/tox/badge.svg https://codecov.io/gh/nbQA-dev/nbQA/branch/master/graph/badge.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 https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white http://www.mypy-lang.org/static/mypy_badge.svg https://interrogate.readthedocs.io/en/latest/_static/interrogate_badge.svg https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/badge/pylint-10/10-brightgreen.svg

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.

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

There are no dependencies for nbqa so installation should be lightning-fast. Check your installation with

$ nbqa --version
nbqa 0.1.16

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 --allow-mutation
reformatted tweet-sentiment-roberta-pytorch.ipynb
All done!  🍰 
1 files reformatted.

Note that if, as in this last example, you expect your notebooks to be modified, you will need to pass the --allow-mutation flag.

Configuration

By default, nbQA will use your tools’ standard configuration files (e.g. setup.cfg, mypy.ini). You can pass extra configurations to your tools either via the command line (as in the examples above), or in a .nbqa.ini file:

[black]
addopts = --line-length=96

[flake8]
addopts = --max-line-length=96 \
          --ignore=E203,W503,W504 \
          --quiet

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.16
  hooks:
    - id: nbqa
      args: ['flake8']
      name: nbqa-flake8
      additional_dependencies: ['flake8']
    - id: nbqa
      args: ['isort', '--allow-mutation']
      name: nbqa-isort
      additional_dependencies: ['isort']
    - id: nbqa
      args: ['mypy']
      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 specialised code quality tools for Jupyter Notebooks:

Project template from cookiecutter.

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.16.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

nbqa-0.1.16-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nbqa-0.1.16.tar.gz
  • Upload date:
  • Size: 19.3 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

Hashes for nbqa-0.1.16.tar.gz
Algorithm Hash digest
SHA256 50ea99ec389623f430fec4abc2be3a1a1720f2bba35d61bd8a15ea15f1b75328
MD5 fa149b418af282dcd1ca037a1b897752
BLAKE2b-256 661737b4c5b753a5428a105df12ad6685b176ec24cef68eb39ee23a576f9caf2

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: nbqa-0.1.16-py3-none-any.whl
  • Upload date:
  • Size: 17.8 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

Hashes for nbqa-0.1.16-py3-none-any.whl
Algorithm Hash digest
SHA256 69c303ce440737475e863f51d82029a42ba4ad22e58aa1c35f27c8fc87b6aa2c
MD5 7b1c2792fe713df75910ed4a0b092ada
BLAKE2b-256 ca6693289f4e1f472878d607256205b9d25210ac2524263a83867fc611e36aaa

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