index.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*-----------------------------------------------------------------------------
  2. | Copyright (c) 2014-2015, Jupyter Development Team.
  3. |
  4. | Distributed under the terms of the Modified BSD License.
  5. |----------------------------------------------------------------------------*/
  6. // Polyfill for ES6 Promises
  7. import 'es6-promise';
  8. import {
  9. Session
  10. } from '@jupyterlab/services';
  11. function log(text: string): void {
  12. let el = document.getElementById('output');
  13. el.textContent = el.textContent + '\n' + text;
  14. console.log(text);
  15. }
  16. function main() {
  17. // Start a new session.
  18. let options: Session.IOptions = {
  19. kernelName: 'python',
  20. path: 'foo.ipynb'
  21. };
  22. let session: Session.ISession;
  23. log('Starting session');
  24. Session.startNew(options).then(s => {
  25. log('Session started');
  26. session = s;
  27. // Rename the session.
  28. return session.setPath('bar.ipynb');
  29. }).then(() => {
  30. log(`Session renamed to ${session.path}`);
  31. // Execute and handle replies on the kernel.
  32. let future = session.kernel.requestExecute({ code: 'a = 1' });
  33. future.onReply = (reply) => {
  34. log('Got execute reply');
  35. };
  36. return future.done;
  37. }).then(() => {
  38. log('Future is fulfilled');
  39. // Shut down the session.
  40. return session.shutdown();
  41. }).then(() => {
  42. log('Session shut down');
  43. log('Test Complete!');
  44. }).catch(err => {
  45. console.error(err);
  46. log('Test Failed! See the console output for details');
  47. });
  48. }
  49. window.onload = main;