Skip to content

Commit

Permalink
update cell output api from value to data
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed May 26, 2021
1 parent 3e516a0 commit 5769974
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 111 deletions.
12 changes: 12 additions & 0 deletions NotebookTestScript.dib
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ document.getElementById("output").innerText = `The value is ${x}.`;

#!markdown

# Execute the next cell, the output should be displayed as JSON like so:

``` json
{"Name":"Developer","Salary":42}
```

#!csharp

display(new { Name = "Developer", Salary = 42 }, "application/json");

#!markdown

# Execute the next cell and verify the following error appears:

```
Expand Down
7 changes: 3 additions & 4 deletions src/dotnet-interactive-vscode/common/interactiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,14 @@ export class InteractiveClient {
}

private displayEventToCellOutput(disp: DisplayEvent): vscodeLike.NotebookCellOutput {
const encoder = new TextEncoder();
let outputItems: Array<vscodeLike.NotebookCellOutputItem> = [];
if (disp.formattedValues && disp.formattedValues.length > 0) {
for (let formatted of disp.formattedValues) {
let value: any = formatted.mimeType === 'application/json'
? JSON.parse(formatted.value)
: formatted.value;
let data = encoder.encode(formatted.value);
outputItems.push({
mime: formatted.mimeType,
value,
data,
});
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/dotnet-interactive-vscode/common/interfaces/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ export function isTextOutput(arg: any): arg is NotebookCellTextOutput {
&& typeof arg.text === 'string';
}

export function reshapeOutputValueForVsCode(mimeType: string, value: unknown): unknown {
if (mimeType === notebook.ErrorOutputMimeType &&
typeof value === 'string') {
// this looks suspiciously like an error message; make sure it's the shape that vs code expects
return {
ename: 'Error',
evalue: value,
traceback: [],
};
export function reshapeOutputValueForVsCode(value: Uint8Array | string, mime: string): Uint8Array {
if (typeof value === 'string') {
const encoder = new TextEncoder();
const dataString = mime === notebook.ErrorOutputMimeType
? JSON.stringify({
ename: 'Error',
evalue: value,
traceback: [],
})
: value;
const data = encoder.encode(dataString);
return data;
} else {
return value;
}

// no change
return value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const ErrorOutputMimeType = 'application/vnd.code.notebook.error';

export interface NotebookCellOutputItem {
readonly mime: string;
readonly value: Uint8Array | unknown;
readonly data: Uint8Array;
readonly metadata?: { [key: string]: any };
}

Expand Down
7 changes: 3 additions & 4 deletions src/dotnet-interactive-vscode/common/tests/unit/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export function createKernelTransportConfig(kernelTransportCreator: (notebookUri
const defaultClientMapperConfig = {
kernelTransportCreator: defaultKernelTransportCreator,
createErrorOutput: (message: string, outputId?: string) => {
const errorItem = {
const errorItem: vscodeLike.NotebookCellOutputItem = {
mime: 'application/vnd.code.notebook.error',
value: encoder.encode(JSON.stringify({
data: encoder.encode(JSON.stringify({
name: 'Error',
message,
})),
Expand All @@ -64,9 +64,8 @@ export function decodeNotebookCellOutputs(outputs: vscodeLike.NotebookCellOutput
...o, outputs: o.outputs.map(oi => {
let result = {
...oi,
decodedValue: JSON.parse(decoder.decode(<Uint8Array>oi.value))
decodedData: JSON.parse(decoder.decode(oi.data))
};
delete result.value;
return result;
})
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ export function vsCodeCellOutputToContractCellOutput(output: vscode.NotebookCell
const errorOutputItem = errorOutputItems[0];
const error: NotebookCellErrorOutput = {
errorName: 'Error',
errorValue: '' + errorOutputItem.value,
errorValue: '' + errorOutputItem.data,
stackTrace: [],
};
return error;
} else {
//otherwise build the mime=>value dictionary
const data: { [key: string]: any } = {};
for (const outputItem of output.outputs) {
data[outputItem.mime] = outputItem.value;
data[outputItem.mime] = outputItem.data;
}

const cellOutput: NotebookCellDisplayOutput = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export async function updateCellLanguages(document: vscode.NotebookDocument): Pr
}

async function updateCellOutputs(executionTask: vscode.NotebookCellExecutionTask, cell: vscode.NotebookCell, outputs: Array<vscodeLike.NotebookCellOutput>): Promise<void> {
const reshapedOutputs = outputs.map(o => new vscode.NotebookCellOutput(o.outputs.map(oi => generateVsCodeNotebookCellOutputItem(oi.mime, oi.value))));
const reshapedOutputs = outputs.map(o => new vscode.NotebookCellOutput(o.outputs.map(oi => generateVsCodeNotebookCellOutputItem(oi.data, oi.mime))));
await executionTask.replaceOutput(reshapedOutputs, cell.index);
}

Expand All @@ -211,9 +211,9 @@ export function endExecution(cell: vscode.NotebookCell, success: boolean) {
}
}

function generateVsCodeNotebookCellOutputItem(mimeType: string, value: unknown): vscode.NotebookCellOutputItem {
const displayValue = reshapeOutputValueForVsCode(mimeType, value);
return new vscode.NotebookCellOutputItem(mimeType, displayValue);
function generateVsCodeNotebookCellOutputItem(data: Uint8Array, mime: string): vscode.NotebookCellOutputItem {
const displayData = reshapeOutputValueForVsCode(data, mime);
return new vscode.NotebookCellOutputItem(displayData, mime);
}

async function updateDocumentKernelspecMetadata(document: vscode.NotebookDocument): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,18 @@ function contractCellOutputToVsCodeCellOutput(output: contracts.NotebookCellOutp
const outputItems: Array<vscode.NotebookCellOutputItem> = [];
if (utilities.isDisplayOutput(output)) {
for (const mimeKey in output.data) {
outputItems.push(generateVsCodeNotebookCellOutputItem(mimeKey, output.data[mimeKey]));
outputItems.push(generateVsCodeNotebookCellOutputItem(output.data[mimeKey], mimeKey));
}
} else if (utilities.isErrorOutput(output)) {
outputItems.push(generateVsCodeNotebookCellOutputItem(vscodeLike.ErrorOutputMimeType, output.errorValue));
outputItems.push(generateVsCodeNotebookCellOutputItem(output.errorValue, vscodeLike.ErrorOutputMimeType));
} else if (utilities.isTextOutput(output)) {
outputItems.push(generateVsCodeNotebookCellOutputItem('text/plain', output.text));
outputItems.push(generateVsCodeNotebookCellOutputItem(output.text, 'text/plain'));
}

return new vscode.NotebookCellOutput(outputItems);
}

function generateVsCodeNotebookCellOutputItem(mimeType: string, value: unknown): vscode.NotebookCellOutputItem {
const displayValue = utilities.reshapeOutputValueForVsCode(mimeType, value);
return new vscode.NotebookCellOutputItem(mimeType, displayValue);
function generateVsCodeNotebookCellOutputItem(value: Uint8Array | string, mime: string): vscode.NotebookCellOutputItem {
const displayValue = utilities.reshapeOutputValueForVsCode(value, mime);
return new vscode.NotebookCellOutputItem(displayValue, mime);
}
Loading

0 comments on commit 5769974

Please sign in to comment.