Skip to content

Update for new NotebookCellOutput changes #35

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

Merged
merged 3 commits into from
Mar 17, 2021
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"@types/react-dom": "^16.9.8",
"@types/uuid": "^8.0.0",
"@types/vscode": "^1.45.0",
"@types/vscode-notebook-renderer": "^1.48.0",
"@types/vscode-notebook-renderer": "^1.55.0",
"@typescript-eslint/eslint-plugin": "^3.1.0",
"@typescript-eslint/parser": "^3.1.0",
"cache-loader": "^4.1.0",
Expand Down
89 changes: 55 additions & 34 deletions src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,80 @@ const notebookApi = acquireNotebookRendererApi(JupyterNotebookRenderer);

notebookApi.onDidCreateOutput(renderOutput);

// Copy of vscode-notebook-renderer old types as of 1.48
// Keep these so we can support both the old interface and the new interface
// Interface change here: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/51675/files
interface OldNotebookCellOutputMetadata {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
custom?: { [key: string]: any };
}
interface OldNotebookOutput {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: { [mimeType: string]: any };
metadata?: OldNotebookCellOutputMetadata;
}
interface OldNotebookOutputEventParams {
element: HTMLElement;
outputId: string;
output: OldNotebookOutput;
mimeType: string;
}

/**
* Called from renderer to render output.
* This will be exposed as a public method on window for renderer to render output.
*/
function renderOutput(request: NotebookOutputEventParams) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mimeString = request.mime || (request as any).mimeType;
try {
console.error('request', request);
console.log('request', request);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why these were errors, so I converted them.

const output = convertVSCodeOutputToExecutResultOrDisplayData(request);
console.log(`Rendering mimeType ${request.mimeType}`, output);
console.error('request output', output);
console.log(`Rendering mimeType ${mimeString}`, output);
console.log('request output', output);

ReactDOM.render(React.createElement(CellOutput, { mimeType: request.mimeType, output }, null), request.element);
ReactDOM.render(React.createElement(CellOutput, { mimeType: mimeString, output }, null), request.element);
} catch (ex) {
console.error(`Failed to render mime type ${request.mimeType}`, ex);
console.error(`Failed to render mime type ${mimeString}`, ex);
}
}

function convertVSCodeOutputToExecutResultOrDisplayData(
request: NotebookOutputEventParams
): nbformat.IExecuteResult | nbformat.IDisplayData {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const metadata: Record<string, any> = {};
// Send metadata only for the mimeType we are interested in.
const customMetadata = request.output.metadata?.custom;
if (customMetadata) {
// Support for Old API
if (customMetadata[request.mimeType]) {
metadata[request.mimeType] = customMetadata[request.mimeType];
}
if (customMetadata.needs_background) {
metadata.needs_background = customMetadata.needs_background;
}
if (customMetadata.unconfined) {
metadata.unconfined = customMetadata.unconfined;
}
if ('mime' in request) {
// New API
return {
data: {
[request.mime]: request.value
},
metadata: request.metadata || {},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metadata is the bit that I want to check in particular when I look at the old API with stable. The new layout is a bit odd. There is a top-level metadata and then another unlabeled metadata just below that. I think it's the old custom metadata as it has needs_background in it.

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly the naming seems odd. metadata.metadata feels vague to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give VS Code that feedback?

execution_count: null,
output_type: request.metadata?.outputType || 'execute_result'
};
} else {
// New API.
// Old API
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const outputMetadata = request.output.metadata as Record<string, any> | undefined;
if (outputMetadata && outputMetadata[request.mimeType] && outputMetadata[request.mimeType].metadata) {
const metadata: Record<string, any> = {};

const oldRequest = (request as unknown) as OldNotebookOutputEventParams;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is casting to unknown better practice?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was what I was reading. (Full confession, I didn't actually know this beforehand, and looked it up after a hint from a TS compiler warning). Here is Anders commenting on the addition of unknown. Basically a typesafe any so that you can use it safely as an intermediate with casting.
microsoft/TypeScript#24439

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, thanks :)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const outputMetadata = oldRequest.output.metadata as Record<string, any> | undefined;
if (outputMetadata && outputMetadata[oldRequest.mimeType] && outputMetadata[oldRequest.mimeType].metadata) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Object.assign(metadata, outputMetadata[request.mimeType].metadata);
if (request.mimeType in outputMetadata[request.mimeType].metadata) {
Object.assign(metadata, outputMetadata[request.mimeType].metadata[request.mimeType]);
Object.assign(metadata, outputMetadata[oldRequest.mimeType].metadata);
if (oldRequest.mimeType in outputMetadata[oldRequest.mimeType].metadata) {
Object.assign(metadata, outputMetadata[oldRequest.mimeType].metadata[oldRequest.mimeType]);
}
}

return {
data: {
[oldRequest.mimeType]: oldRequest.output.data[oldRequest.mimeType]
},
metadata,
execution_count: null,
output_type: oldRequest.output.metadata?.custom?.vscode?.outputType || 'execute_result'
};
}
return {
data: {
[request.mimeType]: request.output.data[request.mimeType]
},
metadata,
execution_count: null,
output_type: request.output.metadata?.custom?.vscode?.outputType || 'execute_result'
};
}