index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 { Session } from '@jupyterlab/services';
  9. function log(text: string): void {
  10. let el = document.getElementById('output');
  11. el.textContent = el.textContent + '\n' + text;
  12. console.log(text);
  13. }
  14. function main() {
  15. // Start a new session.
  16. let options: Session.IOptions = {
  17. kernelName: 'python',
  18. path: 'foo.ipynb'
  19. };
  20. let session: Session.ISession;
  21. log('Starting session');
  22. Session.startNew(options)
  23. .then(s => {
  24. log('Session started');
  25. session = s;
  26. // Rename the session.
  27. return session.setPath('bar.ipynb');
  28. })
  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. })
  38. .then(() => {
  39. log('Future is fulfilled');
  40. // Shut down the session.
  41. return session.shutdown();
  42. })
  43. .then(() => {
  44. log('Session shut down');
  45. log('Test Complete!');
  46. })
  47. .catch(err => {
  48. console.error(err);
  49. log('Test Failed! See the console output for details');
  50. });
  51. }
  52. window.onload = main;