The Python Oracle

Showing line numbers in IPython/Jupyter Notebooks

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC B Schuberts Piano Sonata No 16 D

--

Chapters
00:00 Question
00:32 Accepted answer (Score 209)
01:11 Answer 2 (Score 87)
01:40 Answer 3 (Score 75)
01:55 Answer 4 (Score 11)
02:21 Thank you

--

Full question
https://stackoverflow.com/questions/1097...

Answer 1 links:
[editing ]: https://stackoverflow.com/questions/2019...

Answer 3 links:
[image]: https://i.stack.imgur.com/hdlv6.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #ipython #jupyter

#avk47



ACCEPTED ANSWER

Score 215


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.




ANSWER 2

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




ANSWER 3

Score 82


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

The menu looks like this




ANSWER 4

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