How to Format Jupyter notebook in VSCode? - visual-studio-code

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?

As Kyle Carow stated in their answer to Ian Huff: Formatting of notebooks is available by now.
MacOS: Option+Shift+F
Windows: Alt+Shift+F
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

I'm not exactly sure what is being asked here. But currently in VS Code the notebook editor and interactive window do not support formatting python code.
However we are moving to a new UI for those features which will support all the formatting features of VS Code in .py files. If you try out VS Code - Insiders now you should see this support and can try it out. This new UI will eventually be rolling out to VS Code stable.
Edit: The new UI which supports formatting in cells is now out by default for stable users.

If you have a Jupyter notebook open in VS Code with the Jupyter notebook extension, it should be possible to format code cells with Ctrl+ Shift+ I.
I'm not sure if this shortcut differs between operating systems, you can check what the shortcut is yourself by hitting Ctrl+ Shift+ P and then searching for "Format cell". The shortcut should be displayed then.

Related

Highlighting problem in Jupyter Notebook - VSCode

Lately when using Jupyter Notebook in VS Code to write some assignments for my studies I ran into a quite annoying problem - whenever there is a mistake in my code cell that prevents it from running, the "traceback" (or however you call it) to the place where the error persists is colored with a high-contrast marker (the color depends on the theme used) and makes the content pretty much invisible unless you manually "select" it with the mouse coursor. Is there any way I could fix it without going too much in-depth into VSCode/Jupyter Notebook extension settings?
The highlighting looks like shown below.
Yellow syntax marker problem.
And another one here.
I tried all the themes preinstalled with VS Code such as Monokai, Solarized Light etc., and also a custom theme of my choice called Dracula.
Thanks in advance.
This issue has recently reappeared. While the issue is being fixed you can change the highlight colour within the vscode settings.
Open settings and search for "Workbench:ColorCustomizations"
Image of settings page
Create or alter the "terminal.ansiYellow": "#eed202" to a more appropriate value like "terminal.ansiYellow": "#9b4550". See the linked image.
Credit for this fix: https://github.com/microsoft/vscode-jupyter/issues/8717#issuecomment-1241776226
I also have this problem. It looks like they fixed it less than 1 month ago as of writing this, so it may go away if you update to a recent pre-release of Jupyter in VS Code. Personally, I am going to live with it until the next stable release.
My version of Jupyter in VS Code:
Screenshot of Jupyter Versions
Screenshot of vscode-jupyter github: Screenshot of vscode-jupyter github
https://github.com/microsoft/vscode-jupyter/issues/8697

How to hide time of execution in Jupyter cells in Visual Studio Code?

I am following a course on Python and Math. As editor I use Jupyter notebooks in VS Code.
I am not able to eliminate (or hide) the time of execution shown in each cell. I searched in Settings, but did not find the relative option.
Could you help, please?
Thanks.
This now exists in VS Code's 'clickable' settings screen (as opposed to editing the settings.json file.
Search settings for the below, and select 'hidden' from the dropdown.
notebook.showCellStatusBar
Just put this parameter in the settings.json:
"notebook.showCellStatusBar": "hidden"
I have the same question.
I only found this enhancement issue on github.
It does seem like they did not think of a option to disable it.
For now just switch back to normal Jupyter notebooks.

How to copy cells in Visual Studio Code when using Jupyter?

I'm working with a Jupyter notebook. How would you copy cells in Visual Studio Code? C to copy and V to Paste did not work for me as it would online.
Also, Visual Studio doesn't seem to offer right click options when working in the notebook. Is there a extension that would give me some options such as Cut, Copy, Paste, Delete Cell, Etc.?
I'm a developer on this extension, and I have a bit of a two part answer for you.
In the current (as of 2/16/21) stable release version of the extension notebook UI is done by a hosted webview, it was the only supported option at the time. As such it is lacking features both of Jupyter and of VS Code editor instances. It does not currently support cutting, copying and pasting of cells.
We are currently in the process of moving over to VS Code's new notebook UI that they provide. This allows better UI both for matching Jupyter and supporting more VS Code editor features. This current UI does allow for copying / cutting / pasting cells with the familiar Jupyter commands.
This support will not be added to the old editors, but currently the new UI is available if you use the extension with VS Code - Insiders and will soon start rolling out to VS Code Stable.
https://devblogs.microsoft.com/python/notebooks-are-getting-revamped/
Short answer: Supported with VS Code - Insiders now, and will be supported on stable VS Code in the future.
The new version of the visual studio Jupyter Notebook extension support all the standard operations for one or multiple cells of the jupyter notebook. For example, the following list shows some tested shutcuts:
To copy a selected cell(s) use: ctrl+c
To paste a selected cell(s) use: ctrl+v
To cut a selected cell(s) use: ctrl+x
To delete a selected cell(s) use: ctrl+x
etc.
Note that, as usual, to select multiples cells one should hold the ctrl button before selecting.
As of 2021-01-14, you can now select the cells, right click on any single one of the cells you selected (this will select them as a group if you select more than one) and have the option to press COPY. Or you can simply ctrl+C as a keyboard shortcut. The key here is when you copy and paste the cells to another notebook, your cursor needs to be outside any cells; that is you need to press somewhere outside the cell before pasting. In COLAB notebook, even if your cursor is on a cell, the copy and pasting method works. BUT here if you do that you will be pasting all of the codes inside the cell where your cursor is active; it will not paste the actual cells you copied.
Hope this wasn't too confusing.

How to highlight cells in vscode?

Hello,
I have been using vscode (Visual Studio Code) text editor for couple of days. I have come from Spyder IDE for python. I am developing a great likeness towards vscode. It encompasses all of my needs to python programming with also being lightweight.
However, one thing that I am missing and wish it were here in vscode is cell highlighting. Though the editor has feature to segment the code into several cells by typing # %% before a block, it lacks the feature to highlight the cell where mouse pointer is hovered on.
Maybe, there is an option there in settings.json but i don't know where it is in particular. Though it is not a great lack or causes major issues but i like to enable this feature in my vscode editor.
Below pictures illustrate my point.
Previous image is the visual studio code editor and the below one is for Spyder IDE.
It seems it is supported now.
Bellow there's a screenshot for the current version of VSCode using Microsoft's Python extension.
If this answers the question, please, inform as answer.

Enthought Canopy some keyboard shortcuts not working

I'm using Canopy (1.1.0 64-bit on Mac OSX 10.8) to create IPython notebooks and I noticed some keyboard shortcuts are not working. While Command+C and Command+X work as expected, paste (Command+V) and the cell shortcuts (e.g. Ctrl+m m for converting the cell to markdown) are not working.
For Command+V, it makes the cursor jump to the beginning of the cell instead of pasting, so I'm assuming it's been set somewhere to something different than paste. Is there a way to remap it back to paste?
I had the same issue before, which has been solved by installing newer version of Enthought Canopy (>=1.4). See the following link for details. Hope this helps!
https://support.enthought.com/entries/21742524-OSX-Pasting-to-the-notebook