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

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.


2 thoughts on “Show Line Numbers by Default in IPython Notebook”