31

I would like my default display for IPython notebook code cells to include line numbers.

I learned from Showing line numbers in IPython/Jupyter Notebooks that I can toggle this with ctrl-M L, which is great, but manual. In order to include line numbers by default, I would need to add something to my ipython_notebook_config.py file. Unless I've missed something, there is not an explanation of how to do this in the documentation.

1
  • For people in Latest 2021 versions, just add this in your advanced settings.json { "codeCellConfig": { "lineNumbers": true} } Jun 26, 2021 at 8:26

3 Answers 3

47

(For Jupyter 4+) In the latest Jupyter versions, they have documented the place to make config changes. So basically, in the Jupyter update, they've removed the concept of profiles, so the custom.js file location is now .jupyter/custom/custom.js, depending on where your .jupyter folder is. So if you don't have a custom folder or the custom.js file, just create them, then put these lines into the newly created file:

define([
    'base/js/namespace',
    'base/js/events'
    ], 
    function(IPython, events) {
        events.on("app_initialized.NotebookApp", 
            function () {
                require("notebook/js/cell").Cell.options_default.cm_config.lineNumbers = true;
            }
        );
    }
);

The above is for setting line numbers to all your cell types at the same time. Code, Markdown and Raw cells will all get line numbers if you do this. If you want line numbers only for code cells, there is a simpler approach. Select a code cell, open the Chrome/Firefox JavaScript console, type the following lines:

var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
    CodeCell:{
        cm_config:{lineNumbers:true}
    }
}
config.update(patch)

Then reload the page. These changes persist because Jupyter will create a json config file in .jupyter/nbconfig to store them. This method is from this page of the documentation, so read the docs for more config changes that you can make.


(old answer)

In the latest version of IPython Notebook (v3.1.0), go to ~/.ipython/<profile_name>/static/custom/custom.js and add these lines:

define([
    'base/js/namespace',
    'base/js/events'
    ], 
    function(IPython, events) {
        events.on("app_initialized.NotebookApp", 
            function () {
                IPython.Cell.options_default.cm_config.lineNumbers = true;
            }
        );
    }
);

The IPython.Cell.options_default.cm_config.lineNumbers = true; line alone will not work as it needs to load the IPython.Cell object before it tries this. Adding this line alone will cause an undefined error in the console. You need to encase it in the event handler as shown.

@William-Denman's code might have worked for an earlier version, but now you will need to do this.

EDIT: The line of code right in the middle has to be changed to require("notebook/js/cell").Cell.options_default.cm_config.lineNumbers = true; for the latest version of IPython/Jupyter (IPython 4.0.0, Jupyter 4.0.6). The old IPython.Cell object will also work, but your web console will throw a deprecation warning, so you can expect the old line to not be supported in future versions.

Also, in the latest IPython/Jupyter, which I'm running using the WinPython portable, I couldn't find the custom.js file within the profile folder. I found it (after much searching) in WinPython-64bit-2.7.10.3\python-2.7.10.amd64\Lib\site-packages\notebook\static\custom. I don't know if this is a WinPython thing or a Jupyter thing. If someone has Jupyter (latest version) installed normally (using pip or whatever) and can still find the custom.js file in the profile folder, please comment.

6
  • 1
    For me using v3.2.0, the accepted answer does not work but this does. Thanks.
    – akai
    Jul 13, 2015 at 7:37
  • @Mindstormer619, thanks. This solution works and should be regarded as the correct answer.
    – Shahar
    Jul 28, 2015 at 13:39
  • 1
    consider moving to the top of the answer the more recent, correct approach :) Oct 2, 2016 at 14:48
  • Thanks for your input. Will do :D Oct 2, 2016 at 19:26
  • 3
    Hi @fx-kirin, I believe this whole thing is a non-issue in the latest version of Jupyter. Just hit Shift+L. This toggles in all cells and persists the setting. Dec 27, 2017 at 9:05
26

In your custom.js file (location depends on your OS) put

IPython.Cell.options_default.cm_config.lineNumbers = true;

If you can't find custom.js, you can just search for it, but generally it will be in your profile_default folder. If it doesn't exist, create the file at $(ipython locate profile)/static/custom/custom.js

If for whatever reason that doesn't work, you can always edit the custom.js file in the site-packages/IPython/html/static/custom/ in the same way.

6
  • Very cool; works. One additional thing I've learned trying to get this to work: at least for me, despite the comment in the default custom.js file (“Placeholder for custom user javascript mainly to be overridden in profile/static/custom/custom.js”), the file must be located directly in the profile directory (for me, ~/.ipython/profile_default/).
    – khstacking
    Nov 25, 2013 at 16:52
  • Awesome! I learned some things as well :D. It would be great if you could accept the answer, cheers. Nov 25, 2013 at 16:56
  • @WilliamDenman, this does not work anymore in the new Jupyter notebook, any idea why or better yet, how to fix this? Thanks
    – Shahar
    Mar 15, 2015 at 19:58
  • I'll be honest, your comment is the first I've heard of Jupyter. It looks really cool! Unfortunately, I am no longer using Python/iPython on a daily basis. Have moved to .NET/C#/VB (for work). So I won't be able to help you. I suggest, if my answer does not work, post a new SO question. I'm sure someone more knowledgeable will be able to answer it. Mar 15, 2015 at 22:27
  • 1
    @Shahar (and future readers), this does in fact still work on Jupyter notebook, at least 3.1.0.
    – jedwards
    Apr 22, 2015 at 10:34
6

Above didn't work for me in 2018

I found that inside ~/.jupyter/nbconfig/notebook.json I needed to add to add the following lines:

"CodeCell": {
  "cm_config": {
  "lineNumbers": true
}

inside the object that was there. So the final object would look like:

{
  "CodeCell": {
    "cm_config": {
      "lineNumbers": true
    }
  }
}
3
  • 4
    Hi Stephen. In the current version of Jupyter, you can just hit Shift+L on a cell (in Command Mode) to toggle line numbers and persist the setting. Going through the config files manually is unneeded :) Mar 16, 2018 at 17:21
  • @Mindstormer619 Haha, thanks! I've been trying out vim for Jupyter and got sick of toggling line numbers for each cell... Didn't realize it was that easy to just turn them all on or off. Eventually ended up with what I said, but your way is easier =)
    – Stephen C
    Mar 18, 2018 at 3:29
  • I'm using jupyter-vim-binding and this method was easy and worked for me, thanks!
    – kashmoney
    Aug 28, 2020 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.