// Copyright (C) 2014 Jean-Christophe Jaskula // 2015 joshuacookebarnes@gmail.com // // Distributed under the terms of the BSD License. // --------------------------------------------------------------------------- // // Execution timings: // display when a cell was last executed, and how long it took to run // A double click on the timing box makes it disappear define([ 'require', 'jquery', 'moment', 'base/js/namespace', 'base/js/events', 'notebook/js/codecell' ], function ( requirejs, $, moment, Jupyter, events, codecell ) { 'use strict'; var mod_name = 'ExecuteTime'; var log_prefix = '[' + mod_name + ']'; var CodeCell = codecell.CodeCell; // defaults, overridden by server's config var options = { clear_timings_on_clear_output: false, clear_timings_on_kernel_restart: false, default_kernel_to_utc: true, display_absolute_format: 'HH:mm:ss YYYY-MM-DD', display_absolute_timings: true, display_in_utc: false, display_right_aligned: false, highlight: { use: true, color: '#00bb00', }, relative_timing_update_period: 10, template: { executed: 'executed in ${duration}, finished ${end_time}', queued: 'execution queued ${start_time}', }, }; function patch_CodeCell_get_callbacks () { console.log(log_prefix, 'patching CodeCell.prototype.get_callbacks'); var old_get_callbacks = CodeCell.prototype.get_callbacks; CodeCell.prototype.get_callbacks = function () { var callbacks = old_get_callbacks.apply(this, arguments); var cell = this; var prev_reply_callback = callbacks.shell.reply; callbacks.shell.reply = function (msg) { if (msg.msg_type === 'execute_reply') { $.extend(true, cell.metadata, { ExecuteTime: { start_time: add_utc_offset(msg.metadata.started), end_time: add_utc_offset(msg.header.date), } }); var timing_area = update_timing_area(cell); if ($.ui !== undefined && options.highlight.use) { timing_area.stop(true, true).show(0).effect('highlight', {color: options.highlight.color}); } } else { console.log('msg_type', msg.msg_type); } return prev_reply_callback(msg); }; return callbacks; }; } function patch_CodeCell_clear_output () { console.log(log_prefix, 'Patching CodeCell.prototype.clear_output to clear timings also.'); var orig_clear_output = CodeCell.prototype.clear_output; CodeCell.prototype.clear_output = function () { var ret = orig_clear_output.apply(this, arguments); clear_timing_data([this]); return ret; }; } function toggle_timing_display (cells, vis) { for (var i = 0; i < cells.length; i++) { var cell = cells[i]; if (cell instanceof CodeCell) { var ce = cell.element; var timing_area = ce.find('.timing_area'); if (timing_area.length > 0) { if (vis === undefined) { vis = !timing_area.is(':visible'); } timing_area.toggle(vis); } } } return vis; } function clear_timing_data (cells) { cells.forEach(function (cell, idx, arr) { delete cell.metadata.ExecuteTime; cell.element.find('.timing_area').remove(); }); events.trigger('set_dirty.Notebook', {value: true}); } function clear_timing_data_all () { console.log(log_prefix, 'Clearing all timing data'); clear_timing_data(Jupyter.notebook.get_cells()); } function create_menu () { var timings_menu_item = $('
  • ') .addClass('dropdown-submenu') .append( $('') .text('Execution Timings') .on('click', function (evt) { evt.preventDefault(); }) ) .appendTo($('#cell_menu')); var timings_submenu = $('