How to enable line wrapping in ipython notebook

纵然是瞬间 提交于 2019-12-31 19:29:06

问题


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.


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



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



回答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.



来源:https://stackoverflow.com/questions/20672537/how-to-enable-line-wrapping-in-ipython-notebook

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