Line numbers for every cell in VSCode Jupyter Notebook Cells - python

In the normal Jupyter notebooks, we can add line numbers by pressing the L key after selecting the cell. All the subsequent cells for that notebook will have line numbers automatically.
But this is missing in VSCode. Every time a new cell is created, we need to select the cell and press the L key to enable the numbering for that cell which is very tiresome.
Is there a way to automatically enable line numbering for all the cells in the VSCode Jupyter notebook instead of manually pressing the L key for each and every cell?
Thank you.

In VS Code Jupyter Notebook, you can toggle line numbers by pressing L. The trick is to select the current cell first.
You could select your cell by clicking in the blank area on the left margin of the cell (when it's selected the bar on the left turns blue). Now if you press L on your keyboard, you would be able to turn the line numbers on/off.

In order to toggle on/off showing the line number for all the cells(code):
Click on the blank area (left side of the cell).
Press Shift + L.

As of 2021-01-04, this is sadly not possible.
Subscribe to the GitHub issue (Feature Request) Line Numbers - All Cells #1593 for updates.
Related: Line numbers should be enabled for current cell and subsequent cells when turned on #1659

Adding to this as still comes out the top result for the problem without a proper solution.
Assuming you have the Jupyter Notebook extensions installed for VSCode, in version 1.62.3, you can go into the Settings (Ctrl+,) search for "line numbers" and find the option for "Notebook:Line Numbers" and toggle this on.
This will show line numbers for all code cells in the Notebook by default so no need to keep pressing L
screen shot of Notebook line number toggle option

I always try any combination with modifiers keys.
Found out that pressing Shift + L enables the lines number for all the notebook at once

There is an unbound command to toggle line numbers:
notebook.cell.toggleLineNumbers
BTW, beginning in v1.75 the setting
notebook.lineNumbers can show relative line numbers
VS Code can now use all editor line number options when notebook.lineNumbers is turned on. For example, with "notebook.lineNumbers": "on" and "editor.lineNumbers": "relative", relative line numbers are displayed in cell editor.
from v1.75 Release Notes: Cell Editor Polish
You can now temporarily toggle the line numbers of a cell in the
current session from the cell toolbar, or change the visibility of
line numbers for all notebooks through the notebook.lineNumbers
setting.
Under the ... in the floating menu is a line number toggle. [The gif is too large for SO.]

I tried CTRL + A (outside cells): select all cells and right clicked "show cell line number". It displayed line number for every cell.

As for 2022:
In the upper left corner, select the config-weel.
One of the options you can choose is 'Show Notebook Line Numbers'
Sweet and easy.enter image description here

Adds example for #mark answer, you can enable for current work space using:
Make new folder name .vscode
Make a setting file inside that folder called settings.json
Copy paste code in the bottom of this post.
{
// other setting can be placed here
"notebook.lineNumbers": "on"
}
Edit:
To open settings.json without manually creating the file is by opening workspace or user settings:
open Command Palette (Ctrl+Shift+P)
Type Preferences: Open User Settings (JSON) or Preferences: Open Default Settings (JSON)
Copy paste the snippet setting.

Enter empty space on Jupiter notebook .ipynb file
Click:
F1
Then write:
Show Cell File Numbers

Related

Running find_all() commands in a new view via Sublime Text Plugin changes it into an Instant File Search

I am building an internal plugin for Sublime Text 3 to automate some changes we need done in our HTML.
I'm close to finishing it so we can start using it but I just have one small issue I can't seem to find a solution for.
The plugin will search for some specific HTML code, save them in a file and then modify the code in the view to reference some text in a json file. Then the plugin will open a new view and paste the contents of the file we saved some code in step 1. After which, I need to do some formatting in the new view (replace commas, quotes and brackets with a new line) and then run another internal sublime command in the new view.
The issue is after replacing the commas, quotes and brackets with a new line, the new view changes to an "Instant File Search" which messes with how the view runs the next command and inserts some garbage characters.
Example of the code:
#Open new tab and paste the copied subheads
newView = self.view.window().new_file(syntax="HTML.sublime-syntax")
newView.insert(edit, 0, json.dumps(sections))
# Clear any current selections in the new view
newView.sel().clear()
# Remove quotes, commas and square brackets and separate into new lines
q = newView.find_all(r"(\x{0022}\,\s\x{0022})|(\[\x{0022})|(\x{0022}\])")
for region in reversed(q):
newView.sel().add(region)
newView.replace(edit, region, "\n")
# Clear any current selections in the new view
newView.sel().clear()
q = newView.find_all("{% r")
for region in reversed(q):
newView.sel().add(region)
newView.replace(edit, region, "<p>{% r")
# Clear any current selections in the new view
newView.sel().clear()
q = newView.find_all("endreplace %}")
for region in reversed(q):
newView.sel().add(region)
newView.replace(edit, region, "endreplace %}</p>")
This is what it outputs in the new view:
If I click undo, I get the expected output which seems to mean sublime does something extra after running the last find_all code.
I haven't found anything online regarding this issue so I'm unable to try things to fix this.
Does anyone have an idea on how I should proceed?
P.S. One possible solution is to manually open a new view and run some commands but I'm trying to do all of that in 1 step using my plugin.

How to display a line for every python cell boundary in Visual Studio Code?

In VS Code, when working with python files, if we type #%% it creates a cell separation. When the cursor is within a cell, the editor displays a thick line for the top boundary and a thin line for the bottom boundary (of the current cell only). How can I change the style so that a line is always displayed for every cell boundary (like in the Spyder editor)?
Edit:
I found these settings but they only apply to the interactive window and not the editor:
"workbench.colorCustomizations": {
"notebook.cellBorderColor": "#ff0000",
"notebook.cellToolbarSeparator": "#00ff00"
}
I am expecting to find a way of showing lines at the red arrows below:
I have seen screenshots showing such lines but I can't find how.
Edit 2: Is there a way to customize the style/appearance of every inserted line showing the clickable commands Run Cell | Run Above | Debug Cell ?
take a look at this: extension-visualstudio
maybe we need to checkout [vscode-python] (https://github.com/microsoft/vscode-python): package.nls.json stores a lot of dicts, but i can't find Run Cell | Run Above | Debug Cell too..
As a possible hack, you can extend the cell commands.
The setting for that is called "Jupyter: Code Lenses".
I set mine to:
"jupyter.runcell, jupyter.runallcellsabove, jupyter.debugcell,
jupyter.debugcell, jupyter.debugcell, jupyter.debugcell,
jupyter.debugcell, jupyter.debugcell, jupyter.debugcell,
jupyter.debugcell, jupyter.debugcell, jupyter.debugcell"
long cell command list to mimic a cell border
Jupyter is a good choice for you which has separate code cells.
Create jupyter notebook from command palette then install required module ipykernal, you can run python code cell in it. Reference: Jupyter Notebook in VS Code.

Jupyter notebook Keyboard shortcut to run only selected lines within a cell

Is there a keyboard shortcut to run only selected lines in a cell of Jupyter notebook. for e.g - I have 10 lines in the cell but I only want to select the last line of cell and run that. I have checked online and only able to find below shortcuts
Shift + Enter run the current cell, select below
Ctrl + Enter run selected cells
Alt + Enter run the current cell, insert below
Ctrl + S save and checkpoint
Any ideas ?

How to show line numbers in Google Colaboratory?

Normally it is possible in jupyter or iPython notebooks to show lines number for a cell, however I don't see where in Google Colaboratory (Colab).
Yep, the shortcut (Ctrl + M + L) works,
other option is use the bar menu, at tool -> preference -> show line numbers
Update:
new path: Tools -> Settings -> Editor -> show line numbers
Holding Ctrl and pressing ML (one by one) switches on/off line numbers in the cells containing code.
I see it under Tools->Settings->Editor (as on On 1//1/2020)

Cell mode in Python editors

In recent versions of MATLAB, one can execute a code region between two lines starting with %% using Ctrl-Enter. Such region is called a code cell, and it allows for fast code testing and debugging.
E.g.
%% This is the beginning of the 1st cell
a = 5;
%% This is the end of the 1st cell and beginning of the 2nd cell
% This is just a comment
b = 6;
%% This is the end of the 2nd cell
Are there any python editors that support a similar feature?
EDIT: I just found that Spyderlib supports "block" execution (code regions separated with blank lines) with F9, but as the this thread mentions, this feature is still not very robust (in particular in combination with loops).
The Interactive Editor for Python IEP has a Matlab-style cell notation to mark code sections (by starting a line with '##'), and the shortcut by default is also Ctrl+Enter:
## Cell one
"""
A cell is everything between two commands starting with '##'
"""
a = 3
b = 4
print('The answer is ' + str(a+b))
## Cell two
print('Hello World')
Spyder3 defines a cell as all code between lines starting with #%%.
Run a cell with Ctrl+Enter, or run a cell and advance with Shift+Enter.
Spyder3 & PyCharm: #%% or # %%
Spyder3: Ctrl+Enter: to run current cell, Shift+Enter: to run current cell and advance.
PyCharm: Ctrl+Enter: to run and advance
# %%
print('You are in cell 1')
# %%
print('You are in cell 2')
# %%
print('You are in cell 3')
enter image description here
I have written a vim plugin in which cells are delimited by ## . It sends cells to an ipython interpreter running in tmux. You can define key mappings to execute the current cell, execute current cell and move to next or execute the current line :
https://github.com/julienr/vim-cellmode
I recently started working on a similar plugin for Intellij PyCharm. It can send the cell to either the internal python console (which has some issues with plots) or to an ipython interpreter running in tmux :
https://github.com/julienr/pycharm-cellmode
Pyscripter supports block execution. But it's Win only. And it's limited to select code block - > run it(Ctrl+F7). No notion of cells.
IDLE with IdleX has support for Matlab-like and Sage-like cells using SubCodes. Code in between '##' markers can be executed with Ctrl+Return. It also allows for indented markers so that indented code can be executed.
There is Sage that offers something like this. It is meant to be a python alternative to Matlab, you should take a look.
In a sage notebook, you write python commands within blocks that are pretty similar to matlab's cell.

Categories

Resources