Showing line numbers in IPython/Jupyter Notebooks
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Jupyter can show line numbers, but the exact control depends on whether you are using the classic notebook interface or JupyterLab. For quick use, the fastest method is usually the built-in menu or keyboard shortcut. For persistent behavior, use the interface settings so new code cells open with line numbers already enabled.
Quick Toggle for the Current Session
In the classic notebook interface, line numbers can be toggled from the menu or from command mode for the selected cell. In JupyterLab, the feature is also built in, though the menu wording can differ slightly by version.
The practical workflow is:
- click inside a code cell
- leave edit mode if needed so notebook shortcuts apply
- use the line-number toggle from the UI or shortcut
For many users, that is enough. It solves the immediate problem without touching configuration files.
Why Line Numbers Matter in Notebooks
Notebooks are great for exploration, but once a code cell gets long, debugging becomes awkward without line numbers. Tracebacks, code reviews, and pair debugging are all easier when you can say "look at line 18 in this cell" instead of visually counting lines.
This matters especially when you are:
- writing functions directly in a cell
- comparing notebook code with a traceback
- teaching from a shared notebook
- copying code between a notebook and a Python module
Persisting the Setting
If you want line numbers enabled by default, use the configuration or settings system for the notebook frontend you actually run.
For JupyterLab-style settings, the persisted value is typically expressed as JSON-like configuration. A minimal example looks like this:
The exact settings location depends on the frontend version, but the idea is stable: store notebook editor preferences instead of toggling every session by hand.
For classic notebook setups, you can also generate the Jupyter configuration directory if it does not already exist:
That command creates the base config area under your Jupyter home, which is often the starting point for persistent notebook customization.
Version Differences Matter
This topic causes confusion because "Jupyter Notebook" can refer to multiple frontends over time:
- classic Notebook
- Notebook 7, which is built on newer Jupyter components
- JupyterLab
The capability exists across them, but the path to the setting may differ. That is why old blog posts sometimes mention menu paths or customization hooks that no longer match the interface you are looking at.
A good rule is to trust the built-in View menu, notebook settings, or command palette before reaching for browser hacks or outdated JavaScript snippets.
When Built-In Options Are Not Enough
If your environment is managed centrally, such as JupyterHub for a class or company, you may not be able to change the setting permanently from the filesystem. In that case, use the notebook UI toggle or ask the administrator whether frontend defaults can be preconfigured.
It is usually better to use supported settings than to inject custom browser scripts. Custom scripts are brittle and tend to break after notebook upgrades.
Common Pitfalls
The most common mistake is following instructions written for classic notebook while actually running JupyterLab or Notebook 7. The menus and settings layout are similar, but not identical.
Another mistake is staying in cell edit mode and expecting a command-mode shortcut to work. If the shortcut seems ignored, change focus or use the menu path instead.
A third pitfall is over-customizing with old custom.js snippets when a supported built-in setting already exists.
Summary
- Jupyter supports line numbers in both classic and modern notebook interfaces.
- Use the built-in menu or shortcut for a quick session-level toggle.
- Use notebook settings if you want line numbers enabled by default.
- Interface details vary between classic Notebook, Notebook 7, and JupyterLab.
- Prefer supported settings over outdated browser-script hacks.
Related reading
- Shuffle DataFrame rows
- Shuffle DataFrame rows
- Simple Linear Regression in Python
- Simple Machine learning model training returning Nan
- Simple Popularity Algorithm
- Simple way to calculate median with MySQL
- Simple way to measure cell execution time in ipython notebook
- Simple way to visualize a TensorFlow graph in Jupyter?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.