How to enable line wrapping in ipython notebook

谁说我不能喝 提交于 2019-12-03 01:46:37
Jakob

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;
            }
        );
    }
);
AGS

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;
});

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!