Skip to main content

Run any Python code quality tool on a Jupyter Notebook!

Project description

logo

nbQA

image image image image image image image image image image image All Contributors

demo

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

Installation

Install nbqa with pip:

pip install -U nbqa

Examples

Reformat your notebook with black:

$ nbqa black my_notebook.ipynb --line-length=96 --nbqa-mutate
reformatted my_notebook.ipynb
All done!  🍰 1 files reformatted.

Sort your imports with isort:

$ nbqa isort my_notebook.ipynb --treat-comment-as-code='# %%' --nbqa-mutate
Fixing my_notebook.ipynb

Check your type annotations with mypy:

$ nbqa mypy my_notebook.ipynb --ignore-missing-imports
my_notebook.ipynb:cell_10:5: error: Argument "num1" to "add" has incompatible type "str"; expected "int"

Run your docstring tests with doctest:

$ nbqa doctest my_notebook.ipynb
**********************************************************************
File "my_notebook.ipynb", cell_2:11, in my_notebook.add
Failed example:
    add(2, 2)
Expected:
    4
Got:
    5
**********************************************************************
1 items had failures:
1 of   2 in my_notebook.hello
***Test Failed*** 1 failures.

Configuration

Here's an example .nbqa.ini file - see configuration for more on configuration:

[isort]
config = setup.cfg
mutate = 1
addopts = --treat-comment-as-code '# %%%%'

[flake8]
config = setup.cfg

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.30
  hooks:
    - id: nbqa
      args: ["flake8"]
      name: nbqa-flake8
      alias: nbqa-flake8
      additional_dependencies: ["flake8"]
    - id: nbqa
      args: ["isort", "--nbqa-mutate"]
      name: nbqa-isort
      alias: nbqa-isort
      additional_dependencies: ["isort"]

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:

Contributing

I will give write-access to anyone who contributes anything useful (e.g. pull request / bug report) - see the contributing guide for details on how to do so.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Marco Gorelli

💻 🚧 👀 ⚠️ 🤔

Sebastian Weigand

🔧 👀 📖 🤔

Girish Pasupathy

💻 🚇 🐛 👀

fcatus

🚇

HD23me

🐛

This project follows the all-contributors specification. Contributions of any kind welcome!

Supported by

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