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.
In more details CTRL - M (or ESC ) bring you to command mode, then pressing the L keys should toggle the visibility of current cell line numbers. In more recent notebook versions Shift-L should toggle for all cells.
The easiest way to add line numbers to a Jupyter Notebook is to use the keyboard shortcut, which is Ctrl-m to enter Command Mode, then type L. Just highlight the cell you are interested in adding line numbers to, then hit the keyboard shortcut to toggle the line numbers.
Esc and 'l' will remove the numbering from current cell. Esc and 'L' will remove the numbering from all the cells of notebook.
(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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With