官术网_书友最值得收藏!

Adding custom controls in the notebook toolbar

The CSS and JavaScript of the HTML notebook can be customized through the files in ~/.ipython/profile_default/static/custom, where ~ is your home directory, and default is your IPython profile.

In this recipe, we will use these customization options to add a new button in the notebook toolbar that linearly renumbers all code cells.

How to do it...

  1. First, we are going to inject JavaScript code directly in the notebook. This is useful for testing purposes, or if we don't want the changes to be permanent. The JavaScript code will be loaded with that notebook only. To do this, we can just use the %%javascript cell magic as follows:
    In [1]: %%javascript
            // This function allows us to add buttons 
            // to the notebook toolbar.
            IPython.toolbar.add_buttons_group([
            {
                // The button's label.
                'label': 'renumber all code cells',
                
                // The button's icon.
                // See a list of Font-Awesome icons here:
                // http://fortawesome.github.io/Font-
                //                              Awesome/icons/
                'icon': 'icon-list-ol',
                
                // The callback function.
                'callback': function () {
                    
                    // We retrieve the lists of all cells.
                    var cells = IPython.notebook.get_cells();
                    
                    // We only keep the code cells.
                    cells = cells.filter(function(c)
                      {
                          return c instanceof IPython.CodeCell; 
                      })
                    
                    // We set the input prompt of all code 
                    // cells.
                    for (var i = 0; i < cells.length; i++) {
                        cells[i].set_input_prompt(i + 1);
                    }
                }
            }]);
  2. Running the preceding code cell adds a button in the toolbar as shown in the following screenshot. Clicking on this button automatically updates the prompt numbers of all code cells.

    Adding a Renumber toolbar button

  3. To make these changes permanent, that is, to add this button on every notebook in the current profile, we can open the ~/.ipython/profile_default/static/custom/custom.js file and add the following lines of code:
    $([IPython.events]).on('app_initialized.NotebookApp',
        function(){
            // Copy the JavaScript code (in step 1) here.
        });

    The preceding code will be automatically loaded in the notebook, and the renumber button will appear on top of every notebook in the current profile.

There's more...

The IPython notebook JavaScript API that allowed us to add a button to the notebook toolbar is still unstable at the time of writing. It may change at any time, and it is not well documented. This recipe has only been tested with IPython 2.0. You may nevertheless find a not-so-official and partial API documentation on this page: http://ipjsdoc.herokuapp.com.

We should expect a more stable API in the future.

See also

  • The Customizing the CSS style in the notebook recipe
主站蜘蛛池模板: 马山县| 凤冈县| 长顺县| 石渠县| 长宁区| 景德镇市| 凉山| 民和| 天长市| 驻马店市| 三台县| 岑溪市| 濮阳县| 永泰县| 昌乐县| 永安市| 辽宁省| 故城县| 社旗县| 洛扎县| 炎陵县| 鸡泽县| 许昌县| 特克斯县| 大田县| 湖北省| 平塘县| 洛南县| 甘孜| 建宁县| 阳高县| 泰顺县| 宁都县| 平邑县| 峡江县| 泗阳县| 曲麻莱县| 沙河市| 蒙城县| 石林| 新龙县|