Skip to content

Commit

Permalink
314, fixed assigning execution phase for stream output (#315)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Kadnikov <[email protected]>
  • Loading branch information
sok82 and Sergey Kadnikov authored Sep 15, 2024
1 parent 90b6ddc commit 50fe121
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
31 changes: 30 additions & 1 deletion packages/react/src/examples/OutputWithMonitoring.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,20 @@ const OUTPUTS_2: IOutput[] = [
const SOURCE_ID_3 = 'output-id-3';
const SOURCE_3 = 'a = 5';

const SOURCE_ID_4 = 'output-id-4';
const SOURCE_4 =
"import warnings; warnings.warn('This is a warning message'); print('See warning in output!')";

const SOURCE_ID_5 = 'output-id-5';
const SOURCE_5 = 'print(2+2)';

const OutputWithMonitoring = ({
title,
id,
code,
output,
}: {
title: string,
title: string;
id: string;
code: string;
output?: IOutput[];
Expand Down Expand Up @@ -79,6 +86,14 @@ const OutputWithMonitoring = ({
JSON.stringify(phaseOutput.outputModel?.toJSON()),
]);
break;
case ExecutionPhase.completed_with_warning:
setExecutionLog(executionLog => [
...executionLog,
new Date().toISOString() +
' EXECUTION PHASE - COMPLETED_WITH_WARNING and output ' +
JSON.stringify(phaseOutput.outputModel?.toJSON()),
]);
break;
}
};

Expand Down Expand Up @@ -150,5 +165,19 @@ root.render(
id={SOURCE_ID_3}
code={SOURCE_3}
/>

<OutputWithMonitoring
title="Code generating warning"
key="4"
id={SOURCE_ID_4}
code={SOURCE_4}
/>

<OutputWithMonitoring
title="Code with stream output"
key="5"
id={SOURCE_ID_5}
code={SOURCE_5}
/>
</Jupyter>
);
27 changes: 22 additions & 5 deletions packages/react/src/jupyter/kernel/KernelExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,26 @@ export class KernelExecutor {
.getState()
.getExecutionPhase(this._kernelConnection.id);
if (currentPhase !== ExecutionPhase.completed_with_error) {
// Check if we have warning in output list
let hasWarning = false;
for (let i = 0; i < this.model.length; i++) {
const modelItem = this.model.get(i);
if (
modelItem.type === 'stream' &&
modelItem.toJSON().name === 'stderr'
) {
hasWarning = true;
break;
}
}
const targetPhase = hasWarning
? ExecutionPhase.completed_with_warning
: ExecutionPhase.completed;
kernelsStore
.getState()
.setExecutionPhase(
this._kernelConnection.id,
ExecutionPhase.completed
);
.setExecutionPhase(this._kernelConnection.id, targetPhase);
this._executionPhaseChanged.emit({
executionPhase: ExecutionPhase.completed,
executionPhase: targetPhase,
outputModel: this._model,
});
}
Expand Down Expand Up @@ -307,6 +319,11 @@ export class KernelExecutor {
this._modelChanged.emit(this._model);
break;
case 'stream':
this._outputs.push(message.content as IStream);
this._outputsChanged.emit(this._outputs);
this._model.add(output);
this._modelChanged.emit(this._model);
break;
case 'error':
this._outputs.push(message.content as IStream);
this._outputsChanged.emit(this._outputs);
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/jupyter/kernel/KernelState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum ExecutionPhase {
running = 'RUNNING',
completed = 'COMPLETED',
completed_with_error = 'COMPLETED_WITH_ERROR',
completed_with_warning = 'COMPLETED_WITH_WARNING',
}

export type IKernelState = {
Expand Down
4 changes: 2 additions & 2 deletions packages/react/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ENTRY =
// './src/examples/NotebookColorMode';
// './src/examples/NotebookKernelChange';
// './src/examples/NotebookLite';
'./src/examples/NotebookMutations';
// './src/examples/NotebookMutations';
// './src/examples/NotebookNbformat';
// './src/examples/NotebookNbformatChange';
// './src/examples/NotebookNoContext';
Expand All @@ -60,7 +60,7 @@ const ENTRY =
// './src/examples/NotebookURL';
// './src/examples/ObservableHQ';
// './src/examples/Output';
// './src/examples/OutputWithMonitoring';
'./src/examples/OutputWithMonitoring';
// './src/examples/Outputs';
// './src/examples/Plotly';
// './src/examples/RunningSessions';
Expand Down

0 comments on commit 50fe121

Please sign in to comment.