index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. require(['jquery', '@jupyterlab/services'], function ($, services) {
  2. 'use strict';
  3. var startNewKernel = services.Kernel.startNew;
  4. var kernelOptions = {
  5. name: 'python',
  6. };
  7. // start a single kernel for the page
  8. startNewKernel(kernelOptions).then(function (kernel) {
  9. console.log('Kernel started:', kernel);
  10. kernel.requestKernelInfo().then(function (reply) {
  11. var content = reply.content;
  12. $('#kernel-info').text(content.banner);
  13. console.log('Kernel info:', content);
  14. })
  15. $('#run').click(function () {
  16. var code = $('#cell').val();
  17. console.log('Executing:', code);
  18. // clear output
  19. $('#output').text('');
  20. // Execute and handle replies on the kernel.
  21. var future = kernel.requestExecute({ code: code });
  22. // record each IOPub message
  23. future.onIOPub = function (msg) {
  24. console.log('Got IOPub:', msg);
  25. $('#output').append(
  26. $('<pre>').text('msg_type: ' + msg.header.msg_type)
  27. );
  28. $('#output').append(
  29. $('<pre>').text(JSON.stringify(msg.content))
  30. );
  31. };
  32. future.onReply = function (reply) {
  33. console.log('Got execute reply');
  34. };
  35. future.done.then(function () {
  36. console.log('Future is fulfilled');
  37. $('#output').append($('<pre>').text('Done!'));
  38. });
  39. });
  40. });
  41. });