How to automating Code Formatting in VSCode for Jupyter Notebooks with Black Formatter?

Question:

I’ve been enjoying the convenience of the Black Formatter extension in Visual Studio Code, especially its "Format on Save" feature for Python files. Being able to automatically format my code upon saving with Ctrl+S has significantly streamlined my workflow.

However, I’ve encountered a limitation when working with Jupyter notebooks (.ipynb files) in VSCode. While the Black Formatter seamlessly formats .py files on save, I’ve noticed that formatting code within Jupyter notebooks requires manually triggering the format command for each cell using Alt+Shift+F. This inconsistency in the user experience between .py files and .ipynb files disrupts the workflow and diminishes the efficiency gained from the "Format on Save" feature for Python scripts.

I’m reaching out to see if anyone has found a solution or a workaround to extend the "Format on Save" functionality to Jupyter notebooks within VSCode. Ideally, I’m looking for a method to automatically format all code cells in a notebook when saving the notebook file, similar to how it works for .py files.

Has anyone else experienced this issue or found a way to make code formatting as effortless in .ipynb files as it is in .py files within VSCode? Any advice, plugins, or settings recommendations that could help achieve this would be greatly appreciated.

In my quest to streamline my development workflow, I’ve successfully configured the Black Formatter extension in VSCode to automatically format Python (.py) files on save, using the following settings in my settings.json:

"[python]": {
  "editor.defaultFormatter": "ms-python.black-formatter",
  "editor.formatOnSave": true
}

This setup works perfectly for Python scripts, automatically formatting them each time I press Ctrl+S, aligning with my expectations for a seamless and efficient coding experience.

Transitioning to Jupyter notebooks within VSCode, my expectation was to replicate this level of automation. Given the widespread use of notebooks for data science and machine learning projects, automating code formatting within these notebooks would greatly enhance productivity. I anticipated that either the same settings would apply or there would be a straightforward alternative for .ipynb files.

What I tried was to apply the same logic and settings, hoping VSCode would interpret and extend the "Format on Save" feature to the cells of Jupyter notebooks. I explored the VSCode and Black Formatter extension settings but found no direct reference or solution for applying automatic formatting to .ipynb files upon saving, similar to .py files.

What I’m seeking is either a confirmation that this functionality currently doesn’t exist for .ipynb files in VSCode or guidance on a workaround. Perhaps a different configuration or an extension that bridges this functionality gap? Any shared experience, advice, or solution that would allow for automatic formatting of Jupyter notebook cells on save, enhancing the consistency and efficiency of the development process within VSCode, would be invaluable.

Asked By: Ellie Su

||

Answers:

After setting up Black formatter, you could search for Notebook: formatOnSave and Notebook: formatOnCellExecution in settings(Ctrl+,).

Check these options, You can make formatter work when you save the file in jupyter notebook.

enter image description here

enter image description here

Answered By: MingJie-MSFT

found this solution online:
pip install black[jupyter]

Answered By: Aminu Emmanuel