jupyter-black
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:
Features
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
Introduction
black is an extremely popular python formatter. Jupyter is an
awesome way to run python. This extension helps you automatically blacken
your jupyter.
Dependencies
- Python >= 3.7
- See
setup.cfg
Quickstart
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:
Configurable (recommended):
import jupyter_black
jupyter_black.load()
To look at configuration options:
jupyter_black.load??
For example:
import black
import jupyter_black
jupyter_black.load(
lab=False,
line_length=79,
verbosity="DEBUG",
target_version=black.TargetVersion.PY310,
)
The other way:
%load_ext jupyter_black
This will load the extension using your defaults from pyproject.toml if
available, or use the black defaults. Please note that this defaults to
lab=True, since moving to lab instead of standalone notebook installations
seems to be the direction of the jupyter project; this means this method of
loading will only work in JupyterLab, not in a standard notebook. For now,
users running a standalone notebook (instead of lab) need to use the
recommended (configurable) loading approach above.
Development Setup
- 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
TODO
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
- Write tests for jupyter lab, currently only tested for notebook
- I think the fixture could easily be modularized to also work for
lab, but haven't done the work yet
- I think the fixture could easily be modularized to also work for
Contributing
Please see CONTRIBUTING.md and TODO.
Troubleshooting / FAQ
- How can I install an older / specific version of jupyter black?
- Install from a tag:
- pip install git+git://github.com/n8henrie/jupyter-black.git@v0.1.0
- Install from a specific commit:
- pip install git+git://github.com/n8henrie/jupyter-black.git@aabc123def456ghi789
- Install from a tag:
Acknowledgements
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.
Buy Me a Coffee
Changelog
0.4.0 :: 2024-08-30
- Drop support for python 3.7
- Remove functionality for old-style jupyter notebook
- Modern versions of
labandnotebookboth work the same way -- a much better way -- reducing the amount of code maintenance - Users that are stuck on old versions of
notebook(<7) will need to pin an older version of jupyter-black
- Modern versions of
- Add some nix stuff
- Update dependencies
0.3.4 :: 2023-04-27
- Only pass to
black.Modeoptions frompyproject.tomlthat are valid forblack.Mode. Thanks @rldotai, https://github.com/n8henrie/jupyter-black/issues/7
0.3.2, 0.3.3 :: 2022-11-20
- Remove version constraints (thanks: @JakobGM, https://github.com/n8henrie/jupyter-black/issues/6)
- Update CI to ensure publishing should work
0.3.1 :: 2022-03-08
- Fix description (thanks: @bryanwweber)
- Version bump for PyPI
0.3.0 :: 2022-03-06
- Default to
lab=True- Add warning popup for users that load in notebook with
lab=True(including via%load_ext) - Fix tests for the above
- Might as well minor version bump since this changes the API, even if
still
0.x
- Add warning popup for users that load in notebook with
0.2.1 :: 20220-03-04
- Python 3.10 support
- Black 22 support
0.2.0 :: 2021-11-14
- Breaking change: default to
lab=True;%load_ext jupyter_blackwill now work in jupyterlab and no longer work in a standalone notebook
0.1.1 :: 2021-09-28
- Unload the proper event
0.1.0 :: 2021-09-28
- First release on PyPI.
Metadata
Release files for jupyter-black 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| jupyter_black-0.4.0.tar.gz | 22.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| jupyter_black-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.7 kB
Release files / jupyter_black-0.4.0.tar.gz
| Download URL | jupyter_black-0.4.0.tar.gz |
|---|---|
| Size | 22.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c88e01eff670d933f96bdf0dbc84bb71880b8a2ef38efca08a0fe6cfd6296db4
|
|
BLAKE2b-256 checksum How to use checksums |
0ca1bdcefe44555914f4acf7bb65c97cf40749f56834fc5a6fb9c052e557075c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.5
|
Release files / jupyter_black-0.4.0-py3-none-any.whl
| Download URL | jupyter_black-0.4.0-py3-none-any.whl |
|---|---|
| Size | 7.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aa4166786ad213e8863e2ea11e94cde4096c3b8d2734b2f64e0c4aab470beecc
|
|
BLAKE2b-256 checksum How to use checksums |
bc480b90a8d75f0baf494cacd0c23d4007639faf5089293744755dbbf5154ded
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.5
|
Security for jupyter-black 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.