/nbQA

Run any standard Python code quality tool on a Jupyter Notebook

Primary LanguagePythonMIT LicenseMIT

nbQA

Run any standard Python code quality tool on a Jupyter Notebook

tox codecov versions pre-commit chat

demo

Table of contents

πŸŽ‰ Installation

In your virtual environment, run one of the following:

  • python -m pip install -U nbqa (minimal installation)
  • python -m pip install -U nbqa[toolchain] (install supported code quality tools as well)
  • conda install -c conda-forge nbqa (if you use conda)

πŸš€ Examples

Command-line

Reformat your notebooks with black:

$ nbqa black my_notebook.ipynb --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

Upgrade your syntax with pyupgrade:

$ nbqa pyupgrade my_notebook.ipynb --py36-plus --nbqa-mutate
Rewriting my_notebook.ipynb

See command-line examples for examples involving pylint, flake8, doctest, and mypy.

Pre-commit

Here's an example of how to set up some pre-commit hooks:

  1. Put this in your pyproject.toml file (see configuration for details)

    [tool.nbqa.config]
    isort = "setup.cfg"
    black = "pyproject.toml"
    
    [tool.nbqa.mutate]
    isort = 1
    black = 1
    pyupgrade = 1
    
    [tool.nbqa.addopts]
    isort = ["--treat-comment-as-code", "# %%"]
    pyupgrade = ["--py36-plus"]
  2. Put this in your .pre-commit-config.yaml file (see usage as pre-commit hook)

    - repo: https://github.com/nbQA-dev/nbQA
      rev: 0.3.5
      hooks:
        - id: nbqa-black
        - id: nbqa-pyupgrade
        - id: nbqa-isort

πŸ₯³ Used by

Take some inspiration from their config files πŸ˜‰

πŸ’¬ Testimonials

Michael Kennedy & Brian Okken, hosts of the Python Bytes podcast

This is really cool. I think it brings so much of the code formatting and code analysis, clean up to notebooks, which I think had been really lacking

Alex Andorra, Data Scientist, ArviZ & PyMC Dev, Host of 'Learning Bayesian Statistics' Podcast πŸŽ™οΈ:

well done on nbqa @MarcoGorelli ! Will be super useful in CI πŸ˜‰

Girish Pasupathy, Software engineer and now core-contributor:

thanks a lot for your effort to create such a useful tool

πŸ‘₯ 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.

Thanks goes to these wonderful people (emoji key):


Marco Gorelli

πŸ’» 🚧 πŸ‘€ ⚠️ πŸ€”

Sebastian Weigand

πŸ”§ πŸ‘€ πŸ“– πŸ€”

Girish Pasupathy

πŸ’» πŸš‡ πŸ› πŸ‘€ πŸ€”

fcatus

πŸš‡

HD23me

πŸ›

mani

πŸ€” πŸš‡

Daniel Mietchen

πŸ€”

MichaΕ‚ Gacka

πŸ›

Happy

πŸ“–

Nat Taylor

πŸ›

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