Skip to main content

Run any standard Python code quality tool on a Jupyter Notebook

Project description

nbQA

Run ruff, isort, pyupgrade, mypy, pylint, flake8, black, blacken-docs, and more on Jupyter Notebooks

tox codecov pre-commit

versions chat docs

downloads

demo

  • ✅ handles IPython magics robustly
  • ✅ respects your config files
  • ✅ preserves "quiet mode" trailing semicolons
  • ✅ lints both code and markdown cells

Table of contents

🎉 Installation

In your virtual environment, run (note: the $ is not part of the command):

$ python -m pip install -U nbqa

To also install all supported linters/formatters:

$ python -m pip install -U "nbqa[toolchain]"

🚀 Examples

Command-line

Reformat your notebooks with black:

$ nbqa black my_notebook.ipynb
reformatted my_notebook.ipynb
All done! ✨ 🍰 ✨
1 files reformatted.

Sort your imports with isort:

$ nbqa isort my_notebook.ipynb --float-to-top
Fixing my_notebook.ipynb

Upgrade your syntax with pyupgrade:

$ nbqa pyupgrade my_notebook.ipynb --py37-plus
Rewriting my_notebook.ipynb

Format your markdown cells with blacken-docs:

$ nbqa blacken-docs my_notebook.ipynb --nbqa-md --nbqa-diff
Cell 2
------
--- my_notebook.ipynb
+++ my_notebook.ipynb
@@ -1,2 +1 @@
-First level heading
-===
+# First level heading

To apply these changes, remove the `--nbqa-diff` flag

Format .md files saved via Jupytext (requires jupytext to be installed):

$ nbqa black my_notebook.md
reformatted my_notebook.md
All done! ✨ 🍰 ✨
1 files reformatted.

See command-line examples for examples involving doctest, flake8, mypy, pylint, autopep8, pydocstyle, yapf, and ruff.

Pre-commit

Here's an example of how to set up some pre-commit hooks: put this in your .pre-commit-config.yaml file (see usage as pre-commit hook)

- repo: https://github.com/nbQA-dev/nbQA
  rev: 1.8.4
  hooks:
    - id: nbqa-black
      additional_dependencies: [jupytext]  # optional, only if you're using Jupytext
    - id: nbqa-pyupgrade
      args: ["--py37-plus"]
    - id: nbqa-isort
      args: ["--float-to-top"]

If you need to select specific versions of these linters/formatters, add them to additional_dependencies.

🥳 Used by

Click here for (non-exhaustive) list of repos

Is your project missing? Let us know, or open a pull request!

💬 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

Nikita Sobolev, CTO at wemake.services:

It is amazing!

Alex Andorra, Data Scientist, ArviZ & PyMC Dev, Host of 'Learning Bayesian Statistics' Podcast:

well done on nbqa @MarcoGorelli ! Will be super useful in CI

Matthew Feickert, Postdoc at University of Illinois working on LHC physics:

nbqa in your pre-commit hooks along with @codewithanthony 's pre-commit CI service is amazing! Everyone using Jupyter notebooks should be doing this.

Girish Pasupathy, Software engineer and now core-contributor:

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

Simon Brugman, Data scientist & pandas-profiling dev:

nbQA helps us to keep notebooks to the same standards as the rest of the code. If you're serious about your code standards, you should keep them consistent across both notebooks and python scripts. Great addition to the ecosystem, thanks!

Bradley Dice, PhD Candidate in Physics & Scientific Computing:

nbqa is a clean, easy to use, and effective tool for notebook code style. Formatting and readability makes a huge difference when rendering notebooks in a project's documentation!

James Lamb, engineer @saturn_cloud, LightGBM maintainer

today I learned about nbqa, a command-line tool to run linters like flake8 over #Python code in @ProjectJupyter notebooks. Thanks to @jayyqi for pointing me to it. So far, I really really like it.

Lars Yencken, Tech Lead @ Our World In Data

Super useful! I only wish it was built-in to Jupyterlab.

Vincent D. Warmerdam, maintainer @ calmcode.io

Nice. nbQA looks like a great way to prevent the Untitled12.ipynb-phenomenon. I like!

Mani Sarkar, Kaggle 3x expert, contributor

nbQA is an inspiring project, and the team behind it are very knowledgeable and think of things outside the box with ideas and solutions

👥 Contributing

I will give write-access to anyone who makes a useful pull request - see the contributing guide for details on how to do so.

Thanks goes to these wonderful people (emoji key):

Marco Gorelli
Marco Gorelli

💻 🚧 👀 ⚠️ 🤔
Sebastian Weigand
Sebastian Weigand

🔧 👀 📖 🤔
Girish Pasupathy
Girish Pasupathy

💻 🚇 🐛 👀 🤔
fcatus
fcatus

🚇
HD23me
HD23me

🐛
mani
mani

🤔 🚇
Daniel Mietchen
Daniel Mietchen

🤔
Michał Gacka
Michał Gacka

🐛
Happy
Happy

📖
Nat Taylor
Nat Taylor

🤔 💻 🔧 🐛
Caio Ariede
Caio Ariede

📖
Nikita Sobolev
Nikita Sobolev

🤔 🐛 📖
Amichay Oren
Amichay Oren

🤔
pylang
pylang

🤔
Henry Schreiner
Henry Schreiner

🐛
Kaiqi Dong
Kaiqi Dong

📖
Simon Brugman
Simon Brugman

🐛
John Sandall
John Sandall

🐛
Nathan Cooper
Nathan Cooper

🐛
agruenberger
agruenberger

🐛
Rafal Wojdyla
Rafal Wojdyla

🐛
Bradley Dice
Bradley Dice

🤔 💻
Ivan Cheung
Ivan Cheung

🐛
Tony Hirst
Tony Hirst

🐛
Taneli Hukkinen
Taneli Hukkinen

🚧
Tom Begley
Tom Begley

🤔 💻 📖
Steven DeMartini
Steven DeMartini

📖
vincent d warmerdam
vincent d warmerdam

Daniel Sparing
Daniel Sparing

🐛
asdfCYBER
asdfCYBER

📖
Chris Knight
Chris Knight

🐛
Laura Richter
Laura Richter

🤔
Francesco Ballarin
Francesco Ballarin

🐛
Janosh Riebesell
Janosh Riebesell

🐛 📖
Nicolas Oliver
Nicolas Oliver

🐛
Bas Nijholt
Bas Nijholt

🤔
Ralf Gommers
Ralf Gommers

🤔
Toon Verstraelen
Toon Verstraelen

📖
Trinh Quoc Anh
Trinh Quoc Anh

🐛
Martin K. Scherer
Martin K. Scherer

💻
Felix Williams
Felix Williams

🐛 🚇 🐛
Florian Bär
Florian Bär

📖
Michael Kennedy
Michael Kennedy

🔊
Brian Okken
Brian Okken

🔊
Sydney
Sydney

🐛

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

Project details


Release history Release notifications | RSS feed

This version

1.8.4

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbqa-1.8.4.tar.gz (37.7 kB view details)

Uploaded Source

Built Distribution

nbqa-1.8.4-py3-none-any.whl (35.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nbqa-1.8.4.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for nbqa-1.8.4.tar.gz
Algorithm Hash digest
SHA256 ca983e115d81f5cf149f70c4bf5b8baa36694a3eaf0783fe508dbf05b9767e07
MD5 a83aebf38c43e9df98c1effe112c8555
BLAKE2b-256 e3391124b9efa3d1dbbcfd7a4d17c4b0fbe4e7082a53df81ba8db59ebbcca824

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: nbqa-1.8.4-py3-none-any.whl
  • Upload date:
  • Size: 35.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for nbqa-1.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0e2acd73320ad1aa56f15200f9ea517c0ecb5ac388d217aee97fab66272c604b
MD5 60c1598107ec26f0625af352a96647ef
BLAKE2b-256 9c7f2946cad460129ee81720bcca605376582faedfdcaaff745cd35853b38b63

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