session.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import {
  4. Session,
  5. SessionAPI,
  6. KernelManager,
  7. SessionManager
  8. } from '@jupyterlab/services';
  9. import { log } from './log';
  10. export async function main() {
  11. log('Starting session manager');
  12. const kernelManager = new KernelManager();
  13. const sessionManager = new SessionManager({ kernelManager });
  14. log('Start a new session');
  15. const options: Session.ISessionOptions = {
  16. kernel: {
  17. name: 'python'
  18. },
  19. path: 'foo.ipynb',
  20. type: 'notebook',
  21. name: 'foo.ipynb'
  22. };
  23. const sessionConnection = await sessionManager.startNew(options);
  24. log('Change the session path to "bar.ipynb"');
  25. await sessionConnection.setPath('bar.ipynb');
  26. log('Execute "a=1"');
  27. const future = sessionConnection.kernel.requestExecute({ code: 'a = 1' });
  28. future.onReply = reply => {
  29. log(`Got execute reply with status ${reply.content.status}`);
  30. };
  31. await future.done;
  32. log('Shut down session');
  33. await sessionConnection.shutdown();
  34. log('Get a list of session models and connect to one if any exist');
  35. const sessionModels = await SessionAPI.listRunning();
  36. if (sessionModels.length > 0) {
  37. const session = sessionManager.connectTo({ model: sessionModels[0] });
  38. log(`Connected to ${session.kernel.name}`);
  39. }
  40. }