* Connect to a Jupyter server and start a kernel session.
(serverUrl: string)
| 53 | * Connect to a Jupyter server and start a kernel session. |
| 54 | */ |
| 55 | async connect(serverUrl: string): Promise<void> { |
| 56 | try { |
| 57 | const url = new URL(serverUrl) |
| 58 | url.protocol = url.protocol === 'https:' ? 'wss:' : 'ws:' |
| 59 | const wsUrl = url.toString() |
| 60 | |
| 61 | const serverSettings = ServerConnection.makeSettings({ |
| 62 | baseUrl: serverUrl, |
| 63 | wsUrl, |
| 64 | WebSocket: createJsonWebSocketFactory(), |
| 65 | }) |
| 66 | |
| 67 | this.kernelManager = new KernelManager({ serverSettings }) |
| 68 | this.sessionManager = new SessionManager({ kernelManager: this.kernelManager, serverSettings }) |
| 69 | |
| 70 | // Wait for session manager to be ready |
| 71 | await this.sessionManager.ready |
| 72 | |
| 73 | // Start a new session with Python kernel |
| 74 | this.session = await this.sessionManager.startNew({ |
| 75 | name: 'deepnote-cli', |
| 76 | path: 'deepnote-cli', |
| 77 | type: 'notebook', |
| 78 | kernel: { name: 'python3' }, |
| 79 | }) |
| 80 | |
| 81 | this.kernel = this.session.kernel |
| 82 | if (!this.kernel) { |
| 83 | throw new Error('Failed to start kernel') |
| 84 | } |
| 85 | |
| 86 | // Wait for kernel to be idle (ready to execute) |
| 87 | await this.waitForKernelIdle() |
| 88 | } catch (error) { |
| 89 | await this.disconnect() |
| 90 | throw error |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Wait for the kernel to reach idle status. |
no test coverage detected