How to enable line wrapping in ipython notebook

12,410

Solution 1

As @Matt pointed out you have to configure CodeMirror to enable wrapping.

However, this can be achieved by simply adding the following line to your custom.js:

 IPython.Cell.options_default.cm_config.lineWrapping = true;

So there is no need to loop through all the cells. In a similar fashion you can enable line numbers, set the indentation depth and so on (see the link posted by @Matt for other options). The location of your custom.js depends on your OS (on my Ubuntu machine it is ~/.ipython/profile_default/static/custom).

Update:

In IPython 3 the plain call does not work any more, thus it is required to place the setting within an appropriate event handler. A possible solution could look like:

define([
    'base/js/namespace',
    'base/js/events'
    ],
    function(IPython, events) {
        events.on("app_initialized.NotebookApp",
            function () {
                IPython.Cell.options_default.cm_config.lineWrapping = true;
            }
        );
    }
);

Solution 2

To implement line wrapping in notebooks in ipython 3, I used the answer @Jakob linked above and @Jakob's actual answer. Using the single line of code did not work in my case - however adding the following to custom.js does:

$([IPython.events]).on('app_initialized.NotebookApp', function(){
  IPython.CodeCell.options_default['cm_config']['lineWrapping'] = true;
});

Solution 3

Most of notebook is powered by Codemirror, the option you search is hence this one problem is we don't have simple way of passing configuration to CodeMirror, so you will have to figure out some javascript un custom.js to apply the configuration to the right object.

From the top of my head and handwaving :I would say IPython.CodeCell.default_options.cm les lineWrapping to true then loop through IPython.notebook.get_cells() (already instantiated object) grab their editor attribute and setOption('lineWrapping',true).

You can make a JS extension that does it and propose (and take inspiration) here.

Share:
12,410

Related videos on Youtube

dr3311
Author by

dr3311

Updated on September 08, 2022

Comments

  • dr3311
    dr3311 over 1 year

    I have been trying to enable line wrapping in ipython notebook. I googled it with no results and i typed ipython notebook --help in a terminal. This gives me a ton of configuration commands for a config file, but no line wrapping. Does anyone know if ipnotebook has this feature and if so how to enable it? Your help would be greatly appreciated. Thank you.

  • JJC
    JJC about 9 years
    Thanks. Also, in case this saves others some time, the custom.js file you need to add the above to is located in python3.?/site-packages/IPython/html/static/custom/. E.g., on my OS-X 10.10 python3.4 alt-install, it's here: /usr/local/lib/python3.4/site-packages/IPython/html/static/c‌​ustom/custom.js. If you can't find it, use the find utility (e.g., find / -name 'custom.js'), and make sure you're looking at the correct Python tree. :)
  • BCR
    BCR almost 9 years
    I have an Ipython Notebook that utilizes custom.css and the above code doesn't work when input into the custom.js file. For some reason code cell text wrapping isn't working