6

The %%time and %%timeit magics enable timing of a single cell in a Jupyter or iPython notebook.

Is there similar functionality to turn timing on and off for every cell in a Jupyter notebook?

This question is related but does not have an answer to the more general question posed of enabling a given magic automatically in every cell.

Community
  • 1
  • 1
abeboparebop
  • 7,396
  • 6
  • 37
  • 46
  • https://stackoverflow.com/questions/34415452/choose-sql-as-default-cell-magics-for-jupyter-notebook may be relevant – DharmaTurtle Apr 11 '18 at 06:50

1 Answers1

2

A hacky way to do this is via a custom.js file (usually placed in ~/.jupyter/custom/custom.js)

The example of how to create buttons for the toolbar is located here and it's what I based this answer off of. It merely adds the string form of the magics you want to all cells when pressing the enable button, and the disable button uses str.replace to "turn" it off.

define([
    'base/js/namespace',
    'base/js/events'
], function(Jupyter, events) {
    events.on('app_initialized.NotebookApp', function(){
        Jupyter.toolbar.add_buttons_group([
            {
                'label'   : 'enable timing for all cells',
                'icon'    : 'fa-clock-o', // select your icon from http://fortawesome.github.io/Font-Awesome/icons
                'callback': function () {
                    var cells = Jupyter.notebook.get_cells();
                    cells.forEach(function(cell) {
                        var prev_text = cell.get_text();
                        if(prev_text.indexOf('%%time\n%%timeit\n') === -1) {
                            var text  = '%%time\n%%timeit\n' + prev_text;
                            cell.set_text(text);
                        }
                    });
                }
            },
            {
                'label'   : 'disable timing for all cells',
                'icon'    : 'fa-stop-circle-o', // select your icon from http://fortawesome.github.io/Font-Awesome/icons
                'callback': function () {
                    var cells = Jupyter.notebook.get_cells();
                    cells.forEach(function(cell) {
                        var prev_text = cell.get_text();
                        var text  = prev_text.replace('%%time\n%%timeit\n','');
                        cell.set_text(text);
                    });
                }
            }
            // add more button here if needed.
        ]);
    });
});
Louise Davies
  • 14,781
  • 6
  • 38
  • 41
  • Fantastic answer, but I failed to mention that I am running on a corporate Jupyter server and I'm not sure I have access to `custom.js` to test it. – abeboparebop Feb 15 '17 at 12:29
  • Finally got a chance to test this on a different system. As written, the answer doesn't make much sense -- having both `%%time` and `%%timeit` in every cell is not useful. So everywhere your answer says `%%time\n%%timeit\n`, I replaced it with `%%time\n`. But the general approach works just fine. Thanks! – abeboparebop May 02 '17 at 12:00
  • By the way, the icon for `fa-clock-o` works but not `fa-stop-circle-o`. Jupyter core version 4.2.0. – abeboparebop May 02 '17 at 12:15
  • @abeboparebop Were you able to test it on corporate jupyter server? (I am in the same position and would love to know if you were able to do it.) – Vandan Revanur May 12 '22 at 11:57