How to display line numbers in IPython Notebook code cell by default

Learn how to display line numbers in ipython notebook code cell by default with practical examples, diagrams, and best practices. Covers python, ipython, jupyter-notebook development techniques wit...

Display Line Numbers in IPython Notebook Code Cells by Default

Hero image for How to display line numbers in IPython Notebook code cell by default

Learn how to configure your Jupyter Notebook environment to automatically show line numbers in all code cells, enhancing readability and debugging.

Jupyter Notebook (formerly IPython Notebook) is an incredibly powerful tool for interactive computing. However, by default, code cells do not display line numbers, which can make debugging and referencing specific lines challenging, especially in longer scripts. This article will guide you through several methods to enable line numbers permanently in your Jupyter Notebook environment, improving your coding workflow.

Understanding Jupyter Configuration

Jupyter Notebook's behavior can be customized through configuration files. These files allow you to set default options, including UI elements like line numbers. The primary configuration file for Jupyter Notebook is jupyter_notebook_config.py, which is typically located in your Jupyter configuration directory. Additionally, you can apply custom CSS or JavaScript to modify the notebook's appearance and functionality.

flowchart TD
    A[Start] --> B{Jupyter Notebook Launched?}
    B -- Yes --> C[Load jupyter_notebook_config.py]
    C --> D[Load custom.js]
    D --> E[Apply Settings (e.g., Line Numbers)]
    B -- No --> A
    E --> F[Notebook Ready]

Jupyter Notebook Configuration Loading Process

Method 1: Modifying custom.js for All Notebooks

The most common and recommended way to enable line numbers by default for all your Jupyter Notebooks is by adding a small JavaScript snippet to your custom.js file. This file is executed every time a notebook loads, allowing for persistent UI modifications.

1. Locate your Jupyter configuration directory

Open your terminal or command prompt and run the following command to find your Jupyter configuration directory. This directory is usually ~/.jupyter/ on Linux/macOS or C:\Users\YOUR_USERNAME\.jupyter\ on Windows.

2. Create custom subdirectory (if it doesn't exist)

Navigate to the Jupyter configuration directory. Inside it, create a new folder named custom if it doesn't already exist. This is where custom.js will reside.

3. Create or edit custom.js

Inside the custom directory, create a file named custom.js. If it already exists, open it for editing. Add the following JavaScript code to this file:

4. Restart Jupyter Notebook

After saving the custom.js file, close any running Jupyter Notebook servers and restart them. All new and existing notebooks should now display line numbers by default in their code cells.

define(function() {
    "use strict";
    var Jupyter = require('base/js/namespace');
    var CodeCell = require('notebook/js/codecell').CodeCell;

    var enable_line_numbers = function() {
        // Enable line numbers for all existing code cells
        Jupyter.notebook.get_cells().forEach(function(cell) {
            if (cell.cell_type === 'code') {
                cell.toggle_line_numbers();
            }
        });

        // Enable line numbers for new code cells
        Jupyter.notebook.events.on('create.Cell', function(event, data) {
            if (data.cell.cell_type === 'code') {
                data.cell.toggle_line_numbers();
            }
        });
    };

    // Run on notebook load
    enable_line_numbers();

    console.log('Line numbers enabled by custom.js');
});

JavaScript code for custom.js to enable line numbers by default.

Method 2: Using jupyter_notebook_config.py (Advanced)

While custom.js is simpler for UI changes, you can also configure Jupyter Notebook server-side. This method is less direct for line numbers but demonstrates how to modify the main configuration file. You would typically use this for server-wide settings rather than client-side UI tweaks.

1. Generate the configuration file

If you don't already have a jupyter_notebook_config.py file, generate one by running this command in your terminal:

2. Edit jupyter_notebook_config.py

Open the generated jupyter_notebook_config.py file (located in your Jupyter configuration directory). Search for c.NotebookApp.code_mirror_config and uncomment or add the following lines:

3. Restart Jupyter Notebook

Save the file and restart your Jupyter Notebook server. This change should apply globally.

jupyter notebook --generate-config

Command to generate the default Jupyter Notebook configuration file.

# c.NotebookApp.code_mirror_config = {}
c.NotebookApp.code_mirror_config = {
    'lineNumbers': True
}

Adding lineNumbers configuration to jupyter_notebook_config.py.

Temporary Solution: Enabling Line Numbers in a Single Notebook

If you only need line numbers for a specific session or notebook without making permanent changes, you can enable them directly from the Jupyter Notebook interface.

1. Open a Jupyter Notebook

Launch any Jupyter Notebook.

2. Navigate to View Menu

In the notebook's menu bar, click on View.

3. Toggle Line Numbers

From the dropdown menu, select Toggle Line Numbers. This will immediately display line numbers for all code cells in the current notebook.