197

Error reports from most language kernels running in IPython/Jupyter Notebooks indicate the line on which the error occurred; but (at least by default) no line numbers are indicated in notebooks.

Is it possible to add the line numbers to IPython/Jupyter Notebooks?

Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
  • 3
    It would be also usefull to change the default behavior of the ipython notebook such that it displays line numbers by default – gota May 05 '15 at 15:43

12 Answers12

213

CTRL - ML toggles line numbers in the CodeMirror area. See the QuickHelp for other keyboard shortcuts.

In more details CTRL - M (or ESC) bring you to command mode, then pressing the L keys should toggle the visibility of current cell line numbers. In more recent notebook versions Shift-L should toggle for all cells.

If you can't remember the shortcut, bring up the command palette Ctrl-Shift+P (Cmd+Shift+P on Mac), and search for "line numbers"), it should allow to toggle and show you the shortcut.

Matt
  • 27,170
  • 6
  • 80
  • 74
minrk
  • 37,545
  • 9
  • 92
  • 87
89

On IPython 2.2.0, just typing l (lowercase L) on command mode (activated by typing Esc) works. See [Help] - [Keyboard Shortcuts] for other shortcuts.

Also, you can set default behavior to display line numbers by editing custom.js.

Community
  • 1
  • 1
ronnefeldt
  • 2,083
  • 23
  • 24
  • 4
    I don't understand how this answer was not accepted (maybe posted too late?), because this is the correct one if you don't use CodeMirror but just IPython Notebook. EDIT: in fact IPython Notebook uses CodeMirror. So both answers are correct. Learned something new today, thanks! – gaborous Oct 21 '14 at 15:50
  • thanks, this worked for jupyter notebook in chrome. this seems easiest... – user391339 Jul 27 '17 at 22:19
77

Select the Toggle Line Number Option from the View -> Toggle Line Number.

The menu looks like this

Jeril
  • 7,858
  • 3
  • 52
  • 69
Jitu Shinde
  • 891
  • 6
  • 4
  • 2
    I can't believe it was this easy. Not sure why it's not on by default when they give errors by line. Would be nice if the notebook just highlighted the line that gave the error. – Kaito Jan 08 '19 at 22:49
  • This is no longer in the view. :/ have to search for it in the command palette. – Vaidøtas I. Aug 27 '21 at 06:23
  • @Kaito When your code is a bit more complex, there is maybe not one unique line which led to the error, or the error came from code which you wrote in another cell or imported. Thus I think the highlighting approach is not practicable. – NerdOnTour Feb 17 '22 at 14:58
13
  1. Press Escape so the left part becomes blue.

enter image description here

  1. Then press L. It works!

  2. If you want to hide them, use the same algorithm.

  3. If you want to impact all Jupiter rows, press Shift + L, while the left part is blue.

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • 1
    I don't find that toggling line numbers in the last cell affects all cells. But isn't that what you're saying in point 4? – NerdOnTour Feb 22 '22 at 12:56
  • @NerdOnTour - true, I stay corrected and I have edited (about 13+ months later). To impact all rows, Shift should be pressed, when "L" is pressed. – Vityata Mar 29 '23 at 16:24
11

To turn line numbers on by default in all cells at startup I recommend this link. I quote:

  1. Navigate to your jupyter config directory, which you can find by typing the following at the command line:

    jupyter --config-dir
    
  2. From there, open or create the custom folder.

  3. In that folder, you should find a custom.js file. If there isn’t one, you should be able to create one. Open it in a text editor and add this code:

    define([
        'base/js/namespace',
        'base/js/events'
        ],
        function(IPython, events) {
            events.on("app_initialized.NotebookApp",
                function () {
                    IPython.Cell.options_default.cm_config.lineNumbers = true;
                }
            );
        }
    );
    
Jaroslav Bezděk
  • 6,967
  • 6
  • 29
  • 46
George Fisher
  • 3,046
  • 2
  • 16
  • 15
9

Here is how to know active shortcut (depending on your OS and notebook version, it might change)

Help > Keyboard Shortcuts > toggle line numbers

On OSX running ipython3 it was ESC L

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
7

For me, ctrl + m is used to save the webpage as png, so it does not work properly. But I find another way.

On the toolbar, there is a bottom named open the command paletee, you can click it and type in the line, and you can see the toggle cell line number here.

Noordeen
  • 1,547
  • 20
  • 26
Pythoner
  • 5,265
  • 5
  • 33
  • 49
6

Adding to ronnefeldt's accepted answer: Shift L toggles line numbers in all cells. This works in JupyterLab 1.0.0 and in Jupyter Notebooks.

NerdOnTour
  • 634
  • 4
  • 15
dzang
  • 2,160
  • 2
  • 12
  • 21
6

In Jupyter Lab 2.1.5, it is View -> Show Line Numbers.

enter image description here

Sayyor Y
  • 1,130
  • 2
  • 14
  • 27
3

To show line numbers by default in Jupyter lab,

  • Click on the menu SettingsAdvanced Settings Editor (or use the shortcut control+,). An environment with three columns will appear: the settings category, System Defaults, and User Preferences.
  • Select the category notebook
  • Set the key "lineNumbers" to true in the "codeCellConfig" options.

For example, (assuming no other user configured settings),:

{
    "codeCellConfig": {
        "lineNumbers": true,
    }
}
MRule
  • 529
  • 1
  • 6
  • 18
1

You can also find Toggle Line Numbers under View on the top toolbar of the Jupyter notebook in your browser. This adds/removes the lines numbers in all notebook cells.

For me, Esc+l only added/removed the line numbers of the active cell.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
yanfri
  • 51
  • 1
  • 7
-3

1.press esc to enter the command mode 2.perss l(it L in lowcase) to show the line number

Mo Li
  • 27
  • 2