index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* -----------------------------------------------------------------------------
  2. | Copyright (c) 2014-2015, Jupyter Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |----------------------------------------------------------------------------*/
  6. 'use strict';
  7. const services = require('@jupyterlab/services');
  8. // Start a new session.
  9. const options = {
  10. path: 'foo.ipynb',
  11. type: 'notebook',
  12. name: 'foo.ipynb',
  13. kernel: {
  14. name: 'python'
  15. }
  16. };
  17. /* eslint-disable no-console */
  18. console.log('Starting session...');
  19. const kernelManager = new services.KernelManager();
  20. const sessionManager = new services.SessionManager({ kernelManager });
  21. let session;
  22. sessionManager
  23. .startNew(options)
  24. .then(function(s) {
  25. // Rename the session.
  26. session = s;
  27. return session.setPath('bar.ipynb');
  28. })
  29. .then(function() {
  30. console.log('Session renamed to', session.path);
  31. // Execute and handle replies on the kernel.
  32. const future = session.kernel.requestExecute({ code: 'a = 1' });
  33. future.onReply = function(reply) {
  34. console.log('Got execute reply', reply);
  35. };
  36. return future.done;
  37. })
  38. .then(function() {
  39. console.log('Future is fulfilled');
  40. // Shut down the session.
  41. return session.shutdown();
  42. })
  43. .then(function() {
  44. console.log('Session shut down');
  45. process.exit(0);
  46. })
  47. .catch(function(err) {
  48. console.error(err);
  49. process.exit(1);
  50. });