A simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using Black. Fork of dnanhkhoa/nb_black with a few minor modifications:
Once loaded, automatically format syntactically correct jupyter cells with black once they are run.
Enhancements compared to dnanhkhoa/nb_black:
- Configurability:
- Try to read black config from
pyproject.tomlif available - Override settings such as line length and
black.TargetVersionif desired
- Try to read black config from
- Uses
black.format_cellto greatly simplify the codebase - Adds tests
- Slightly more responsive (no longer requires
setTimeoutand a delay) - Free software: MIT
black is an extremely popular python formatter. Jupyter is an awesome way to run python.
This extension helps you automatically blacken your jupyter.
- Python >= 3.8
jupyterlab>=4 /notebook>= 7- older versions of
notebookworked well untiljupyter-black0.4.0, but recent updates tonotebookhave done away with the old way of formatting cells - consider pinning to
v0.3.4if you're stuck on an older version ofnotebook
- older versions of
- See
setup.cfg
python3 -m venv .venv && source ./.venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install jupyter-black jupyter
python3 -m jupyter notebook
From here, there are two ways to load the extension:
import jupyter_black
jupyter_black.load()To look at configuration options:
jupyter_black.load??For example:
import black
import jupyter_black
jupyter_black.load(
line_length=79,
verbosity="DEBUG",
target_version=black.TargetVersion.PY310,
)%load_ext jupyter_blackThis will load the extension using your defaults from pyproject.toml if available, or use the black defaults.
- Clone the repo:
git clone https://github.com/n8henrie/jupyter-black && cd jupyter-black - Make a virtualenv:
python3 -m venv .venv - Activate venv, update pip, and install editable test/dev version:
$ source ./.venv/bin/activate
$ ./.venv/bin/python -m pip install --upgrade pip
$ ./.venv/bin/python -m pip install -e .[test,dev]Notes:
- Tests use playwright
- You'll need to run this command (once) prior to running the tests:
python -m playwright install --with-deps firefox
toxwill automatically run these installation steps (helpful for CI)- If desired, pass the
--no-headlessflag topytestfor local debugging - See also [
dev-notes.txt]
Contribution ideas:
- More tests, currently very basic
- Ensure config is overridden in unsurprising ways
- Ensure other config options are properly passed to
black - Ensure that
pyproject.tomlis searched for properly
Please see CONTRIBUTING.md and TODO.
- How can I install an older / specific version of jupyter black?
- Install from a tag:
- pip install git+git://github.com/n8henrie/[email protected]
- Install from a specific commit:
- pip install git+git://github.com/n8henrie/jupyter-black.git@aabc123def456ghi789
- Install from a tag:
Many thanks to dnanhkhoa/nb_black for the original version!
And of course many thanks to the black and jupyter teams.
Also, after establishing the repo and reserving the name on PyPI, I noticed there is another library of the same name: drillan/jupyter-black.
It looks like there have been no commits in the last 2 years, and it was never put in PyPI, so I think at this point I'll continue with this name.
Sorry if this causes any trouble or confusion.
I'll note that @drillan's library probably does things the right way by installing as an nbextension.