index.js 1.4 KB

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