Webucator Blog

Show Line Numbers by Default in IPython Notebook

In IPython Notebook, you can toggle line numbers for an individual cell using the L shortcut key:

But the notebook doesn’t remember that it was showing line numbers, so the next time you open it, you’ll have to re-show the line numbers to get them back. That can be a pain, especially for notebooks with a lot of cells.

Luckily, there is a way to customize IPython Notebook so that a notebook opens with line numbers showing by default.

  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 the custom folder. My complete path is C:\Users\Nat\.jupyter\custom
  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;
                }
            );
        }
    );
  4. Save the file.

Once you have done this, your notebooks should open in IPython notebook with line numbers showing by default.

Thanks to Mindstormer619 for the response to this question, which is where I found out how to do this. And for the comment below, which made me realize that custom.js had been moved.

For older versions of iPython Notebook, replace the first two steps above with this:

  1. Navigate to your IPython profile_default folder, which you can find by typing the following at the command line:
    ipython locate profile
  2. From there, open the static and then the custom folder. My complete path is C:\Users\Nat\.ipython\profile_default\static\custom

Related Training: Python


  • Hi, it’s the selfsame Mindstormer619 from StackOverflow. I happened to install WinPython, which came with the latest IPython/Jupyter installation (IPython v 4.0.0, Jupyter v 4.0.6), where the line numbers were not enabled by default. So I checked up in the standard location (which I wrote in that StackOverflow answer) and lo and behold, custom.js was not to be found. So I googled it and reached a site which redirected me to your site which amazingly, had my own answer posted haha.

    So, as you can expect, the location and code for this has changed AGAIN. After tons of fiddling and Chrome console monkeying, I found the new location. For me, this is where I had to find custom.js:

    /WinPython-64bit-2.7.10.3/python-2.7.10.amd64/Lib/site-packages/notebook/static/custom

    For some reason, custom.js wasn’t present in the profile folder anymore. I don’t know if this is a thing only with WinPython’s portable or whether it’s with any installation of IPython/Jupyter. If you’ve updated your IPython to the latest Jupyter, and you still see custom.js in your profile config folder, please reply 🙂

    Also, the exact line that goes into the custom.js file has changed to require("notebook/js/cell").Cell.options_default.cm_config.lineNumbers = true;. Using IPython.Cell will still work, but your console will throw a “deprecated” warning.

    I really wish these guys would stop changing their settings so much.

    • Hi, Mindstormer619! Thanks for commenting. I wasn’t aware of the change. I have the same version of IPython/Jypyter installed as you do (IPython v 4.0.0, Jupyter v 4.0.6), but I am on Python 3.5. My custom.js file is unchanged, but I now have it at C:\Users\Nat\.jupyter\custom\custom.js. I didn’t have to put it there though. It was migrated automatically when I upgraded to Anaconda 2.4. I didn’t need to modify the JavaScript. It still uses IPython.Cell.options_default.cm_config.lineNumbers = true; and there is no “deprecated” warning.