Black: Feature request: Support for formatting the python code in jupyter notebooks

Created on 4 Jun 2018  路  8Comments  路  Source: psf/black

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.

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/

All 8 comments

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.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 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]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

dgingrich picture dgingrich  路  36Comments

devxpy picture devxpy  路  70Comments

mhham picture mhham  路  22Comments

kindjacket picture kindjacket  路  21Comments

lig picture lig  路  84Comments