How to enable line wrapping in ipython notebook

前端 未结 3 1040
青春惊慌失措
青春惊慌失措 2021-02-05 09:36

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 configurat

相关标签:
3条回答
  • 2021-02-05 10:05

    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;
                }
            );
        }
    );
    
    0 讨论(0)
  • 2021-02-05 10:11

    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;
    });
    
    0 讨论(0)
  • 2021-02-05 10:16

    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.

    0 讨论(0)
提交回复
热议问题