dnanhkhoa / nb_black

A simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using black.
MIT License
367 stars 41 forks source link

Does not work in VS Code Jupyter Notebooks #19

Open grahamwetzler opened 4 years ago

grahamwetzler commented 4 years ago

Neither %load_ext nb_black nor %load_ext lab_black work with Jupyter Notebooks in Visual Studio Code. There is no error. The code just doesn't format.

michaelaye commented 4 years ago

Can one not simply use the black functionality of VSCode in there?

grahamwetzler commented 4 years ago

The format document command does not appear in the command palette while in a notebook.

MarcoGorelli commented 4 years ago

@grahamwetzler the black functionality in VScode only works on .py files, as far as I can tell.

In case it's useful to you, though, I've made a tool which might be helpful to you - https://github.com/nbQA-dev/nbQA. You can use it via the command-line as

python -m pip install -U nbqa
nbqa black my_notebook.ipynb

It only works via the command-line or as a pre-commit hook though, so it's certainly not a replacement for the excellent tool that black_nb is


pre-commit example:

- repo: https://github.com/nbQA-dev/nbQA
  rev: 1.5.3
  hooks:
    - id: nbqa-black

EDIT: as for 2022, black has a built-in black-jupyter hook, so the above suggestion to use nbQA is no longer necessary

LucasHattoriCosta commented 1 year ago

Just leaving as another suggestion: there is a command for format_cell available in VsCode. In Mac the shortcut is shift+option+F Again, not a real replacement for the excelent tool that is nb_black, but better than having un-linted code 😅

morberg commented 1 year ago

https://github.com/n8henrie/jupyter-black works really well for me.