index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. /* eslint-disable no-console */
  9. startNewKernel(kernelOptions).then(function (kernel) {
  10. console.log('Kernel started:', kernel);
  11. kernel.requestKernelInfo().then(function (reply) {
  12. var content = reply.content;
  13. $('#kernel-info').text(content.banner);
  14. console.log('Kernel info:', content);
  15. });
  16. $('#run').click(function () {
  17. var code = $('#cell').val();
  18. console.log('Executing:', code);
  19. // clear output
  20. $('#output').text('');
  21. // Execute and handle replies on the kernel.
  22. var future = kernel.requestExecute({ code: code });
  23. // record each IOPub message
  24. future.onIOPub = function (msg) {
  25. console.log('Got IOPub:', msg);
  26. $('#output').append(
  27. $('<pre>').text('msg_type: ' + msg.header.msg_type)
  28. );
  29. $('#output').append(
  30. $('<pre>').text(JSON.stringify(msg.content))
  31. );
  32. };
  33. future.onReply = function (reply) {
  34. console.log('Got execute reply', reply);
  35. };
  36. future.done.then(function () {
  37. console.log('Future is fulfilled');
  38. $('#output').append($('<pre>').text('Done!'));
  39. });
  40. });
  41. });
  42. });