microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
153 stars 36 forks source link

[Feature Request] Support for black in Jupyter Notebooks #87

Open vancromy opened 2 years ago

vancromy commented 2 years ago

Hello 👋,

First of all thanks for your hard work for bringing black to VSCode. My autoformatting has become so much more seamless now that I don't need to worry about having it installed in all of my envs/projects.

I was wondering whether you have looked into (apologies if this has been asked before - I did a quick scan but didn't find anything) enabling the use of black in jupyter notebooks. The isort plugin for VSCode has done this and black do mention in their docs that it can be enabled by pip installing black[jupyter] (https://black.readthedocs.io/en/stable/getting_started.html). Perhaps this could be a user enabled feature if it is not desired to have this by default? :)

karthiknadig commented 2 years ago

@vancromy We currently support formatting individual python cells in a notebook. Since the Language Server Protocol (v3.16) treats each cell as a document. To support the document as a whole, we will need to upgrade to LSP v3.17 which allows us to look at notebooks as a whole document. Currently there is a technical limitation because the library that we use (pygls) is still on v3.16 of LSP. Once that is upgraded this should be fairly easy to do and entirely in python. We are happy to take any PRs if the community wants to get to this before we do.

vancromy commented 1 year ago

@karthiknadig while we wait for this feature, could you elaborate on how individual cells can be formatted using the black plugin? Is it a command from the control palette?

vancromy commented 1 year ago

Apologies for the spam but I discovered the answer to my own question and thought it would help others to document it here. This can be achieved either via right-clicking on the cell and selecting "Format Cell" or by using the dedicated shortcut for that control (via the Control Palette or keyboard shortcut).

danieltomasz commented 1 year ago

Sorry for spam; as the other issue was closed in the favour of this one, are there any progress with leaving the semi-colon in jupyter notebooks? It keeps to be removed

karthiknadig commented 1 year ago

@danieltomasz pygls merged the notebook support last week. Waiting for a release to start work on this.

ankostis commented 9 months ago

Maybe it psf/black#4025 it relevant about dealing with notebook syntax from hydrogen .py files.