index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. var services = require('@jupyterlab/services');
  8. var ws = require('ws');
  9. var xhr = require('xmlhttprequest');
  10. // Set the request and socket functions.
  11. var serverSettings = services.ServerConnection.makeSettings({
  12. xhrFactory: function () { return new xhr.XMLHttpRequest(); },
  13. wsFactory: function (url, protocol) { return new ws(url, protocol); }
  14. });
  15. // Start a new session.
  16. var options = {
  17. kernelName: 'python',
  18. path: 'foo.ipynb',
  19. serverSettings: serverSettings
  20. }
  21. var session;
  22. services.Session.startNew(options).then(function(s) {
  23. // Rename the session.
  24. session = s;
  25. return session.setPath('bar.ipynb');
  26. }).then(function() {
  27. console.log('Session renamed to', session.path);
  28. // Execute and handle replies on the kernel.
  29. var future = session.kernel.requestExecute({ code: 'a = 1' });
  30. future.onReply = function(reply) {
  31. console.log('Got execute reply');
  32. }
  33. return future.done;
  34. }).then(function() {
  35. console.log('Future is fulfilled');
  36. // Shut down the session.
  37. return session.shutdown();
  38. }).then(function() {
  39. console.log('Session shut down');
  40. process.exit(0);
  41. }).catch(function(err) {
  42. console.error(err);
  43. process.exit(1);
  44. })