Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 804
Matthew Salvatore Viglione Avatar asked Dec 13 '25 18:12

Matthew Salvatore Viglione


1 Answers

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.

like image 179
Nikos Korovesis Avatar answered Dec 16 '25 15:12

Nikos Korovesis