pythonvisual-studio-codejupyter-notebookformatting

Format a Jupyter notebook on save in VSCode


I use black to automatically format all of my Python code whenever I save in VSCode. I'd like the same functionality, but within a Jupyter notebook in VSCode.

This answer shows how to right click and format a cell or a whole notebook from the right click context menu, or a keyboard shortcut. Can I make this happen on save instead?

It looks like there is an issue related to this, but it is over a year old.

Are there any good workarounds? Maybe a way to set the format notebook option to the same keybinding as save?

UPDATE:

If you like me want this functionality to be added please go to the issue and upvote it, the devs said they will need a bunch of upvotes before it's considered.

UPDATE:

This got enough attention that it has been added!


Solution

  • Good news! This is now an option in the newest VSCode release (1.77)

    Setting "notebook.formatOnSave.enabled": true will do the trick.

    You can read more about it here.

    If you have black already enabled for Python it should work fine.