kernellist.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. define([
  4. 'jquery',
  5. 'base/js/namespace',
  6. 'tree/js/notebooklist',
  7. 'base/js/i18n'
  8. ], function($, IPython, notebooklist, i18n) {
  9. "use strict";
  10. var KernelList = function (selector, options) {
  11. /**
  12. * Constructor
  13. *
  14. * Parameters:
  15. * selector: string
  16. * options: dictionary
  17. * Dictionary of keyword arguments.
  18. * session_list: SessionList instance
  19. * base_url: string
  20. * notebook_path: string
  21. */
  22. notebooklist.NotebookList.call(this, selector, $.extend({
  23. element_name: 'running'},
  24. options));
  25. this.kernelspecs = this.sessions = null;
  26. this.events.on('kernelspecs_loaded.KernelSpec', $.proxy(this._kernelspecs_loaded, this));
  27. };
  28. KernelList.prototype = Object.create(notebooklist.NotebookList.prototype);
  29. KernelList.prototype.add_duplicate_button = function () {
  30. /**
  31. * do nothing
  32. */
  33. };
  34. KernelList.prototype._kernelspecs_loaded = function (event, kernelspecs) {
  35. this.kernelspecs = kernelspecs;
  36. if (this.sessions) {
  37. // trigger delayed session load, since kernelspecs arrived later
  38. this.sessions_loaded(this.sessions);
  39. }
  40. };
  41. KernelList.prototype.sessions_loaded = function (d) {
  42. this.sessions = d;
  43. if (!this.kernelspecs) {
  44. return; // wait for kernelspecs before first load
  45. }
  46. this.clear_list();
  47. var item, path, session, info;
  48. for (path in d) {
  49. if (!d.hasOwnProperty(path)) {
  50. // nothing is safe in javascript
  51. continue;
  52. }
  53. session = d[path];
  54. item = this.new_item(-1);
  55. info = this.kernelspecs[session.kernel.name];
  56. this.add_link({
  57. name: path,
  58. path: path,
  59. type: 'notebook',
  60. kernel_display_name: (info && info.spec) ? info.spec.display_name : session.kernel.name
  61. }, item);
  62. }
  63. $('#running_list_placeholder').toggle($.isEmptyObject(d));
  64. };
  65. KernelList.prototype.add_link = function (model, item) {
  66. notebooklist.NotebookList.prototype.add_link.apply(this, [model, item]);
  67. var running_indicator = item.find(".item_buttons")
  68. .text('');
  69. var that = this;
  70. var kernel_name = $('<div/>')
  71. .addClass('kernel-name')
  72. .text(model.kernel_display_name)
  73. .appendTo(running_indicator);
  74. var shutdown_button = $('<button/>')
  75. .addClass('btn btn-warning btn-xs')
  76. .text(i18n._('Shutdown'))
  77. .click(function() {
  78. var path = $(this).parent().parent().parent().data('path');
  79. that.shutdown_notebook(path);
  80. })
  81. .appendTo(running_indicator);
  82. };
  83. // Backwards compatability.
  84. IPython.KernelList = KernelList;
  85. return {'KernelList': KernelList};
  86. });