Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notifyOnComplete parameter for KernelExecutor #291

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
},
"githubPullRequests.ignoredPullRequestBranches": [
"main"
],
}
9 changes: 6 additions & 3 deletions packages/react/src/components/output/Output.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type IOutputProps = {
showEditor: boolean;
showKernelProgressBar?: boolean;
toolbarPosition: 'up' | 'middle' | 'none';
notifyOnComplete? : boolean;
};

export const Output = (props: IOutputProps) => {
Expand All @@ -62,6 +63,7 @@ export const Output = (props: IOutputProps) => {
showControl,
showEditor,
showKernelProgressBar = true,
notifyOnComplete = false,
id: sourceId,
toolbarPosition,
} = props;
Expand Down Expand Up @@ -121,7 +123,7 @@ export const Output = (props: IOutputProps) => {
useEffect(() => {
if (adapter) {
if (autoRun) {
adapter.execute(code);
adapter.execute(code, notifyOnComplete);
}
}
}, [adapter]);
Expand All @@ -141,12 +143,12 @@ export const Output = (props: IOutputProps) => {
const executeRequest = outputStore.getExecuteRequest(sourceId);
useEffect(() => {
if (adapter && executeRequest && executeRequest === id) {
adapter.execute(code);
adapter.execute(code, notifyOnComplete);
}
}, [executeRequest, adapter]);
useEffect(() => {
if (adapter && executeTrigger > 0) {
adapter.execute(code);
adapter.execute(code, notifyOnComplete);
}
}, [executeTrigger]);
useEffect(() => {
Expand Down Expand Up @@ -244,6 +246,7 @@ Output.defaultProps = {
metadata: {},
},
],
notifyOnComplete : false,
toolbarPosition: 'up',
} as Partial<IOutputProps>;

Expand Down
6 changes: 4 additions & 2 deletions packages/react/src/components/output/OutputAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { IOutput } from '@jupyterlab/nbformat';
import { JSONObject } from '@lumino/coreutils';
import { IOutputAreaModel, OutputArea, OutputAreaModel } from '@jupyterlab/outputarea';
import { IRenderMime, RenderMimeRegistry, standardRendererFactories } from '@jupyterlab/rendermime';
import { rendererFactory as jsonRendererFactory } from '@jupyterlab/json-extension';
Expand Down Expand Up @@ -79,10 +80,11 @@ export class OutputAdapter {
this.initKernel();
}

public async execute(code: string) {
public async execute(code: string, notifyOnComplete? : boolean) {
if (this._kernel) {
this.clear();
const done = execute(this._id, code, this._outputArea, this._kernel);
const metadata : JSONObject = {};
const done = execute(this._id, code, this._outputArea, this._kernel, metadata, notifyOnComplete);
await done;
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/components/output/OutputExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export async function execute(
code: string,
output: OutputArea,
kernel: Kernel,
metadata?: JSONObject
metadata?: JSONObject,
notifyOnComplete? : boolean
): Promise<KernelMessage.IExecuteReplyMsg | undefined> {
// Override the default for `stop_on_error`.
let stopOnError = true;
Expand All @@ -33,6 +34,7 @@ export async function execute(
{
model: output.model,
stopOnError,
notifyOnComplete : notifyOnComplete
}
);
const future = kernelExecutor!.future;
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/jupyter/kernel/Kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export class Kernel {
stopOnError,
storeHistory,
allowStdin,
notifyOnComplete = false
}: {
model?: IOutputAreaModel;
iopubMessageHooks?: IOPubMessageHook[];
Expand All @@ -202,12 +203,14 @@ export class Kernel {
stopOnError?: boolean;
storeHistory?: boolean;
allowStdin?: boolean;
notifyOnComplete? : boolean
} = {}
): KernelExecutor | undefined {
if (this._kernelConnection) {
const kernelExecutor = new KernelExecutor({
connection: this._kernelConnection,
model,
notifyOnComplete,
});
kernelExecutor.execute(code, {
iopubMessageHooks,
Expand Down
25 changes: 21 additions & 4 deletions packages/react/src/jupyter/kernel/KernelExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export type IKernelExecutorOptions = {
* Outputs model to populate with the execution results.
*/
model?: IOutputAreaModel;
/**
* Flag defining if notification about model changes
* must only be made when execution complete
*/
notifyOnComplete? : boolean;
}

export class KernelExecutor {
Expand All @@ -54,13 +59,15 @@ export class KernelExecutor {
private _outputsChanged = new Signal<KernelExecutor, IOutput[]>(this);
private _future?: JupyterKernel.IFuture<KernelMessage.IExecuteRequestMsg, KernelMessage.IExecuteReplyMsg>;
private _shellMessageHooks = new Array<ShellMessageHook>();
private _notifyOnComplete : boolean = false;

public constructor({ connection, model }: IKernelExecutorOptions) {
public constructor({ connection, model, notifyOnComplete }: IKernelExecutorOptions) {
this._executed = new PromiseDelegate<IOutputAreaModel>();
this._kernelConnection = connection;
this._model = model ?? new OutputAreaModel();
this._outputs = [];
this._kernelState = kernelsStore.getState();
this._notifyOnComplete = !!notifyOnComplete;
}

/**
Expand Down Expand Up @@ -118,6 +125,10 @@ export class KernelExecutor {
// Wait for future to be done before resolving the exectud promise.
this._future.done.then(() => {
kernelsStore.getState().setExecutionPhase(this._kernelConnection.id, ExecutionPhase.completed);
// We emit model changes only when execution completed
if (this._notifyOnComplete) {
this._modelChanged.emit(this._model);
}
this._executed.resolve(this._model);
});
return this._executed.promise;
Expand Down Expand Up @@ -205,13 +216,17 @@ export class KernelExecutor {
this._outputs.push(message.content as IExecuteResult);
this._outputsChanged.emit(this._outputs);
this._model.add(output);
this._modelChanged.emit(this._model);
if (!this._notifyOnComplete) {
this._modelChanged.emit(this._model);
}
break;
case 'display_data':
this._outputs.push(message.content as IDisplayData);
this._outputsChanged.emit(this._outputs);
this._model.add(output);
this._modelChanged.emit(this._model);
if (!this._notifyOnComplete) {
this._modelChanged.emit(this._model);
}
break;
case 'stream':
case 'error':
Expand All @@ -232,7 +247,9 @@ export class KernelExecutor {
this._outputsChanged.emit(this._outputs);
// FIXME this needs more advanced analysis see OutputArea
this._model.add(output);
this._modelChanged.emit(this._model);
if (!this._notifyOnComplete) {
this._modelChanged.emit(this._model);
}
break;
case 'status':
const executionState = (message.content as any).execution_state as KernelMessage.Status;
Expand Down
Loading