aaltay commented on a change in pull request #12372: URL: https://github.com/apache/beam/pull/12372#discussion_r463166779
########## File path: sdks/python/apache_beam/runners/interactive/extensions/apache-beam-jupyterlab-sidepanel/src/kernel/KernelModel.ts ########## @@ -0,0 +1,161 @@ +// Licensed under the Apache License, Version 2.0 (the 'License'); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +/** + * The module holds the model that handles messaging between the frontend and + * the connected kernel. + */ + +import { ISessionContext } from '@jupyterlab/apputils'; + +import { + IDisplayData, + IDisplayUpdate, + IExecuteResult +} from '@jupyterlab/nbformat'; + +import { Kernel, KernelMessage } from '@jupyterlab/services'; + +import { ISignal, Signal } from '@lumino/signaling'; + +import { KernelCode } from '../kernel/KernelCode'; + +export class KernelModel { + constructor(sessionContext: ISessionContext, enableConsoleLog = false) { + this._sessionContext = sessionContext; + this._enableConsoleLog = enableConsoleLog; + } + + get future(): Kernel.IFuture< + KernelMessage.IExecuteRequestMsg, + KernelMessage.IExecuteReplyMsg + > | null { + return this._future; + } + + set future( + value: Kernel.IFuture< + KernelMessage.IExecuteRequestMsg, + KernelMessage.IExecuteReplyMsg + > | null + ) { + if (this._future === value) { + return; + } + + if (this._future) { + this._future.dispose(); + } + + this._future = value; + + if (!value) { + return; + } + + value.onIOPub = this._onIOPub.bind(this); + } + + get executeResult(): object { + if (this._executeResult) { + const dataInPlainText = this._executeResult.data['text/plain'] as string; + if (dataInPlainText) { + try { + // The slice removes trailing single quotes from the nbformat output. + // The replace removes literal backslashes from the nbformat output. Review comment: it seems to replace a backslash and a quote? `\\'` ########## File path: sdks/python/apache_beam/runners/interactive/extensions/apache-beam-jupyterlab-sidepanel/src/__tests__/kernel/KernelModel.test.ts ########## @@ -0,0 +1,124 @@ +// Licensed under the Apache License, Version 2.0 (the 'License'); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +/** + * Tests for KernelModel module. + * + * Non camelcase fields are nbformat fields used in notebooks. Lint is ignored + * for them. + */ + +import { KernelModel } from '../../kernel/KernelModel'; + +const fakeSessionContext = { + session: { + kernel: { + requestExecute: function(): object { + return { + onIOPub: function(): void { + // do nothing + } + }; + } + } + } +}; + +it('creates new future with IOPub callbacks when executing new code in kernel', () => { + const kernelModel = new KernelModel(fakeSessionContext as any); + kernelModel.execute('new code'); + expect(kernelModel.future).not.toBe(null); + expect(kernelModel.future.onIOPub).not.toBe(null); +}); + +it('handles execute result from IOPub channel', () => { + const kernelModel = new KernelModel(fakeSessionContext as any); + kernelModel.execute('any code'); + kernelModel.future.onIOPub({ + header: { + // eslint-disable-next-line @typescript-eslint/camelcase + msg_type: 'execute_result' + }, + content: { + data: { + 'text/plain': + '\'{"pipelineId": {"metadata": {"name": "pipeline", "inMemoryId": 1, "type": "pipeline"}, "pcolls": {"pcollId": {"name": "pcoll", "inMemoryId": 2, "type": "pcollection"}}}}\'' + }, + channel: 'iopub' + } + } as any); + expect(kernelModel.executeResult).toEqual({ + pipelineId: { + metadata: { + name: 'pipeline', + inMemoryId: 1, + type: 'pipeline' + }, + pcolls: { + pcollId: { + name: 'pcoll', + inMemoryId: 2, + type: 'pcollection' + } + } + } + }); +}); + +it('handles display data from IOPub channel', () => { + const kernelModel = new KernelModel(fakeSessionContext as any); + kernelModel.execute('any code'); + const displayData = { + // eslint-disable-next-line @typescript-eslint/camelcase + output_type: 'display_data', + data: { + 'text/html': '<div></div>', + 'application/javascript': 'console.log(1);' Review comment: What does this console.log's do? Are they executed? ########## File path: sdks/python/apache_beam/runners/interactive/extensions/apache-beam-jupyterlab-sidepanel/src/__tests__/kernel/KernelModel.test.ts ########## @@ -0,0 +1,124 @@ +// Licensed under the Apache License, Version 2.0 (the 'License'); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +/** + * Tests for KernelModel module. + * + * Non camelcase fields are nbformat fields used in notebooks. Lint is ignored + * for them. + */ + +import { KernelModel } from '../../kernel/KernelModel'; + +const fakeSessionContext = { + session: { + kernel: { + requestExecute: function(): object { + return { + onIOPub: function(): void { + // do nothing + } + }; + } + } + } +}; + +it('creates new future with IOPub callbacks when executing new code in kernel', () => { + const kernelModel = new KernelModel(fakeSessionContext as any); + kernelModel.execute('new code'); + expect(kernelModel.future).not.toBe(null); + expect(kernelModel.future.onIOPub).not.toBe(null); +}); + +it('handles execute result from IOPub channel', () => { + const kernelModel = new KernelModel(fakeSessionContext as any); + kernelModel.execute('any code'); + kernelModel.future.onIOPub({ + header: { + // eslint-disable-next-line @typescript-eslint/camelcase + msg_type: 'execute_result' + }, + content: { + data: { + 'text/plain': + '\'{"pipelineId": {"metadata": {"name": "pipeline", "inMemoryId": 1, "type": "pipeline"}, "pcolls": {"pcollId": {"name": "pcoll", "inMemoryId": 2, "type": "pcollection"}}}}\'' + }, + channel: 'iopub' + } + } as any); + expect(kernelModel.executeResult).toEqual({ + pipelineId: { + metadata: { + name: 'pipeline', + inMemoryId: 1, + type: 'pipeline' + }, + pcolls: { + pcollId: { + name: 'pcoll', + inMemoryId: 2, + type: 'pcollection' + } + } + } + }); +}); + +it('handles display data from IOPub channel', () => { + const kernelModel = new KernelModel(fakeSessionContext as any); + kernelModel.execute('any code'); + const displayData = { + // eslint-disable-next-line @typescript-eslint/camelcase + output_type: 'display_data', + data: { + 'text/html': '<div></div>', + 'application/javascript': 'console.log(1);' + }, + metadata: { + some: 'data' + } + }; + + kernelModel.future.onIOPub({ + header: { + // eslint-disable-next-line @typescript-eslint/camelcase + msg_type: 'display_data' + }, + content: displayData + } as any); + expect(kernelModel.displayData).toEqual([displayData]); +}); + +it('handles display update from IOPub channel', () => { Review comment: what is the difference between display data and display update messages? ########## File path: sdks/python/apache_beam/runners/interactive/extensions/apache-beam-jupyterlab-sidepanel/src/kernel/KernelModel.ts ########## @@ -0,0 +1,161 @@ +// Licensed under the Apache License, Version 2.0 (the 'License'); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. + +/** + * The module holds the model that handles messaging between the frontend and + * the connected kernel. + */ + +import { ISessionContext } from '@jupyterlab/apputils'; + +import { + IDisplayData, + IDisplayUpdate, + IExecuteResult +} from '@jupyterlab/nbformat'; + +import { Kernel, KernelMessage } from '@jupyterlab/services'; + +import { ISignal, Signal } from '@lumino/signaling'; + +import { KernelCode } from '../kernel/KernelCode'; + +export class KernelModel { + constructor(sessionContext: ISessionContext, enableConsoleLog = false) { + this._sessionContext = sessionContext; + this._enableConsoleLog = enableConsoleLog; + } + + get future(): Kernel.IFuture< + KernelMessage.IExecuteRequestMsg, + KernelMessage.IExecuteReplyMsg + > | null { + return this._future; + } + + set future( + value: Kernel.IFuture< + KernelMessage.IExecuteRequestMsg, + KernelMessage.IExecuteReplyMsg + > | null + ) { + if (this._future === value) { + return; + } + + if (this._future) { + this._future.dispose(); + } + + this._future = value; + + if (!value) { + return; + } + + value.onIOPub = this._onIOPub.bind(this); + } + + get executeResult(): object { + if (this._executeResult) { + const dataInPlainText = this._executeResult.data['text/plain'] as string; + if (dataInPlainText) { + try { + // The slice removes trailing single quotes from the nbformat output. + // The replace removes literal backslashes from the nbformat output. + const dataInJsonString = dataInPlainText + .slice(1, -1) + .replace(/\\'/g, "'"); + return JSON.parse(dataInJsonString); + } catch (e) { + console.error(e); + return {}; + } + } + } + return {}; + } + + get displayData(): Array<IDisplayData> { + return this._displayData; + } + + get displayUpdate(): Array<IDisplayUpdate> { + return this._displayUpdate; + } + + get stateChanged(): ISignal<KernelModel, void> { + return this._stateChanged; + } + + execute(code: string, expectReply = true): void { + // Dispose the kernel future so that no more IOPub will be handled. + if (this.future) { + this.future.dispose(); + this.future = null; + } + // Clear the outputs from previous kernel executions. + this._executeResult = null; + this._displayData.length = 0; + this._displayUpdate.length = 0; + if (!this._sessionContext || !this._sessionContext.session?.kernel) { + return; + } + this.future = this._sessionContext.session?.kernel?.requestExecute({ + code: KernelCode.COMMON_KERNEL_IMPORTS + code, + silent: !expectReply, Review comment: If silent is the name of the underlying api, why do we use an oppositely behaving `expectReply` flag? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org