We can format python code in the jupyter notebook in the browser. After I change to VSCode and use the Microsoft extension, I find I can not format Jupyter notebook any more. Does anyone know how to solve this?
The code formatting is available in Visual Studio Code (VSCode) through the following shortcuts or key combinations: On Windows Shift + Alt + F. On macOS Shift + Option + F. On Linux Ctrl + Shift + I.
The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.
Go to settings in your VS-Code typing “Ctrl + ,” or clicking at the gear on the bottom left and selecting “Settings [Ctrl+,]” option. Type “format on save” at the search bar on top of the Settings tab and check the box. Search for “python formatting provider” and select “black”.
Getting started. You will need to have Python 3 installed on your machine along with the Microsoft Python extension installed from the VS Code Marketplace. You can review the introductory Python tutorial for help with setup. In addition, you need to install the Jupyter Notebooks extension.
As Kyle Carow stated in their answer to Ian Huff: Formatting of notebooks is available by now.
Option+Shift+F
Ctrl+Shift+I
(This is cited from
Kyle Carow as I didn't test this myself yet.)Pressing these keys will either trigger a formatting of the active cell or the notebook as a whole if no cell is active.
Windows: Shift+Alt+F
Is what worked for me with Black formatter set up on VS Code Version: 1.64.2
Or right click "Format Notebook" to format entire notebook.
Format Notebook
I do not think VS Code support formatting of code cells, but if you use the black
code formatter, then you can use the blackcellmagic %%black
to format the code in a cell.
You need to have pip install black
and blackcellmagic
, details here: https://github.com/csurfer/blackcellmagic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With