Showing line numbers in IPython/Jupyter Notebooks

PythonIpythonJupyter

Python Problem Overview


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 possibile to add the line numbers to IPython/Jupyter Notebooks?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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

The menu looks like this

Solution 4 - Python

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; > } > ); > } > );

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

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.

Solution 8 - Python

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

enter image description here

Solution 9 - Python

  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, make sure to do it on the last one, otherwise only one row is impacted.

Solution 10 - Python

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.

Solution 11 - Python

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,
    }
}

Solution 12 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRuggero TurraView Question on Stackoverflow
Solution 1 - PythonminrkView Answer on Stackoverflow
Solution 2 - PythonronnefeldtView Answer on Stackoverflow
Solution 3 - PythonJitu ShindeView Answer on Stackoverflow
Solution 4 - PythonGeorge FisherView Answer on Stackoverflow
Solution 5 - PythonThamme GowdaView Answer on Stackoverflow
Solution 6 - PythonPythonerView Answer on Stackoverflow
Solution 7 - PythondzangView Answer on Stackoverflow
Solution 8 - PythonSayyor YView Answer on Stackoverflow
Solution 9 - PythonVityataView Answer on Stackoverflow
Solution 10 - PythonyanfriView Answer on Stackoverflow
Solution 11 - PythonMRuleView Answer on Stackoverflow
Solution 12 - PythonMo LiView Answer on Stackoverflow