Run any standard Python code quality tool on a Jupyter Notebook
Project description
Run ruff, isort, pyupgrade, mypy, pylint, flake8, black, blacken-docs, and more on Jupyter Notebooks
- ✅ 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.9.1
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
- https://github.com/ComPWA/ampform
- https://github.com/ComPWA/tensorwaves
- https://github.com/DataS-DHSC/os-maps-example
- https://github.com/Eazhi/World-Happiness-Analysis
- https://github.com/GoogleCloudPlatform/ai-platform-samples
- https://github.com/MRCIEU/epigraphdb
- https://github.com/OpenMined/PyDP
- https://github.com/PlasmaPy/PlasmaPy
- https://github.com/ProjectPythia/pythia-foundations
- https://github.com/SeldonIO/alibi
- https://github.com/TeoZosa/deep-learning-v2-pytorch
- https://github.com/WM-SEMERU/mlproj_template
- https://github.com/alan-turing-institute/sktime
- https://github.com/amor71/LiuAlgoTrader
- https://github.com/bbernst/base_envs
- https://github.com/cheginit/HyRiver-examples
- https://github.com/cheginit/pygeohydro
- https://github.com/cheginit/pynhd
- https://github.com/covid-19-impact-lab/sid-germany
- https://github.com/covid-19-impact-lab/sid
- https://github.com/dapperfu/Python-Simulink
- https://github.com/deepcharles/ruptures
- https://github.com/dhassault/cyclegan_pytorch
- https://github.com/gboeing/osmnx
- https://github.com/glotzerlab/freud-examples
- https://github.com/glotzerlab/hoomd-examples
- https://github.com/glotzerlab/signac-docs
- https://github.com/glotzerlab/signac-examples
- https://github.com/grapl-security/grapl
- https://github.com/hainegroup/oceanspy
- https://github.com/henryiii/python-compiled-minicourse
- https://github.com/intake/intake-esm
- https://github.com/jameslamb/lightgbm-dask-testing
- https://github.com/jdb78/pytorch-forecasting
- https://github.com/jhrcook/advent-of-code_2020
- https://github.com/julian-west/asset_price_correlations
- https://github.com/kratsg/drstorage
- https://github.com/madebr/pyOpt
- https://github.com/matthewfeickert/heputils
- https://github.com/matyama/deep-rl-hands-on
- https://github.com/mayou36/raredecay
- https://github.com/neomatrix369/nlp_profiler
- https://github.com/openforcefield/openff-system
- https://github.com/pandas-profiling/pandas-profiling
- https://github.com/paw-lu/dotfiles
- https://github.com/pawamoy/wps-light
- https://github.com/phinate/clarinet
- https://github.com/pik-primap/climate_categories
- https://github.com/pik-primap/primap2
- https://github.com/pik-primap/unfccc_di_api
- https://github.com/pymc-devs/pymc-examples
- https://github.com/pymc-devs/resources
- https://github.com/saturncloud/examples
- https://github.com/scikit-hep/boost-histogram
- https://github.com/scikit-hep/iminuit
- https://github.com/scikit-hep/mplhep
- https://github.com/scikit-hep/pyhf
- https://github.com/scikit-hep/pylhe
- https://github.com/v-goncharenko/data-science-template
- https://github.com/wemake-services/wemake-python-styleguide
- https://github.com/zfit/phasespace
- https://github.com/zfit/zfit
- https://github.com/zfit/zfit-physics
- https://github.com/zfit/zfit-tutorials
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 likeflake8
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):
This project follows the all-contributors specification. Contributions of any kind welcome!
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
Built Distribution
File details
Details for the file nbqa-1.9.1.tar.gz
.
File metadata
- Download URL: nbqa-1.9.1.tar.gz
- Upload date:
- Size: 38.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1f4bcf587c597302fed295951001fc4e1be4ce0e77e1ab1b25ac2fbe3db0cdd |
|
MD5 | 64f32269aca6e1ac408bc415168af2db |
|
BLAKE2b-256 | aa7662d2609924cf34445148cd6b5de694cf64c179cc416cac93182579620e57 |
Provenance
The following attestation bundles were made for nbqa-1.9.1.tar.gz
:
Publisher:
publish_to_pypi.yml
on nbQA-dev/nbQA
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
nbqa-1.9.1.tar.gz
- Subject digest:
a1f4bcf587c597302fed295951001fc4e1be4ce0e77e1ab1b25ac2fbe3db0cdd
- Sigstore transparency entry: 147940632
- Sigstore integration time:
- Predicate type:
File details
Details for the file nbqa-1.9.1-py3-none-any.whl
.
File metadata
- Download URL: nbqa-1.9.1-py3-none-any.whl
- Upload date:
- Size: 35.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95552d2f6c2c038136252a805aa78d85018aef922586270c3a074332737282e5 |
|
MD5 | 59986206ab3c813a5c48717675c472ed |
|
BLAKE2b-256 | 28884789719fbbe166d12d345b3ac66b96105f10001b16e00a9765ba29261a21 |
Provenance
The following attestation bundles were made for nbqa-1.9.1-py3-none-any.whl
:
Publisher:
publish_to_pypi.yml
on nbQA-dev/nbQA
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
nbqa-1.9.1-py3-none-any.whl
- Subject digest:
95552d2f6c2c038136252a805aa78d85018aef922586270c3a074332737282e5
- Sigstore transparency entry: 147940633
- Sigstore integration time:
- Predicate type: