index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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($('<pre>').text(JSON.stringify(msg.content)));
  30. };
  31. future.onReply = function(reply) {
  32. console.log('Got execute reply', reply);
  33. };
  34. future.done.then(function() {
  35. console.log('Future is fulfilled');
  36. $('#output').append($('<pre>').text('Done!'));
  37. });
  38. });
  39. });
  40. });