nbqa: Run Code Quality Tools on Jupyter Notebooks

1.9.1 · active · verified Tue Apr 14

nbqa is a command-line tool that allows you to run any standard Python code quality tool on Jupyter Notebooks. It robustly handles IPython magics, respects your existing configuration files (like `pyproject.toml`), and can lint both code and markdown cells. The current version is 1.9.1, with frequent minor point releases.

Warnings

Install

Quickstart

This quickstart demonstrates how to install `nbqa` and `black`, then use `nbqa` to format a Jupyter Notebook. Remember to install the specific code quality tools (like `black`, `flake8`, `isort`) you wish to use alongside `nbqa`.

# Install nbqa and a code quality tool (e.g., black)
pip install -U nbqa black

# Create a dummy notebook (e.g., my_notebook.ipynb)
echo '{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": ["import os\n\nx =  1 +  2"]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}' > my_notebook.ipynb

# Run black on the notebook using nbqa
nbqa black my_notebook.ipynb

# Expected output will show reformatting, e.g.:
# reformatted my_notebook.ipynb
# All done! ✨ 🍰 ✨ 1 file reformatted.

# To verify changes (if you open the notebook, it should be formatted)
# cat my_notebook.ipynb

view raw JSON →