Jupyter notebooks are quite popular and are used a lot (especially in tasks related to data analysis, visualization etc.).
These notebooks are also used as presentations and tutorials.
It would be great if black could format the code within the cells of the notebooks.
This is great.. Thanks!
Is there something like
$ black-notebook analysis.ipynb
that runs black on all code cells in the notebook(s) from the command line and saves them back in-place?
I guess it's just ~ 10 lines using the Jupyter API, but I'd still prefer using it over writing it if someone has written that already.
@cdeil and others interested in being able to format notebooks from the command line: mcflugen/nbblack is a fairly new project that looks to offer that.
It's pretty easy to configure the code-prettify Nbextension to use Black. Heres the configuration I use and some install instructions.
https://gist.github.com/MarvinT/a072aa992e977496974aaf492287b08c
and
https://marvint.github.io/Black-Jupyter/
Is there something like
$ black-notebook analysis.ipynbthat runs black on all code cells in the notebook(s) from the command line and saves them back in-place?
I guess it's just ~ 10 lines using the Jupyter API, but I'd still prefer using it over writing it if someone has written that already.
@cdeil you can do this with nbQA https://github.com/nbQA-dev/nbQA
e.g. from the command line:
nbqa black notebook.ipynb --nbqa-mutate
e.g. as pre-commit hook:
repos:
- repo: https://github.com/nbQA-dev/nbQA
rev: 0.5.3
hooks:
- id: nbqa-black
args: [--nbqa-mutate]
Most helpful comment
It's pretty easy to configure the code-prettify Nbextension to use Black. Heres the configuration I use and some install instructions.
https://gist.github.com/MarvinT/a072aa992e977496974aaf492287b08c
and
https://marvint.github.io/Black-Jupyter/