Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Format Jupyter notebook in VSCode?

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?

like image 415
安硕明 Avatar asked Jan 16 '21 08:01

安硕明


People also ask

How do I beautify Python code in Visual Studio code?

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.

How do you beautify your code in VS Code?

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.

How do I change the format of a VS Code in Python?

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”.

Can I use VS Code for Jupyter notebook?

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.


Video Answer


3 Answers

As Kyle Carow stated in their answer to Ian Huff: Formatting of notebooks is available by now.

  • MacOS: Option+Shift+F
  • Windows: 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.

like image 91
Nico Avatar answered Oct 01 '22 22:10

Nico


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

like image 28
Peter Grace Avatar answered Oct 01 '22 21:10

Peter Grace


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

like image 36
PerJensen Avatar answered Oct 01 '22 22:10

PerJensen