-
Notifications
You must be signed in to change notification settings - Fork 37
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
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 || {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly the naming seems odd. metadata.metadata feels vague to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is casting to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' | ||
}; | ||
} |
There was a problem hiding this comment.
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.