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

Fix issues with rendering of nested HTML widgets #16358

Merged
merged 1 commit into from
Jan 6, 2025
Merged
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
70 changes: 41 additions & 29 deletions src/webviews/webview-side/ipywidgets/kernel/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { IInteractiveWindowMapping, IPyWidgetMessages, InteractiveWindowMessages
import { WIDGET_MIMETYPE, WIDGET_STATE_MIMETYPE } from '../../../../platform/common/constants';
import { NotebookMetadata } from '../../../../platform/common/utils';
import { noop } from '../../../../platform/common/utils/misc';
import type { RendererContext } from 'vscode-notebook-renderer';
import type { OutputItem, RendererContext } from 'vscode-notebook-renderer';
import { renderersAndMimetypes } from './mimeTypes';
import { base64ToUint8Array } from '../../../../platform/common/utils/string';

Expand Down Expand Up @@ -246,35 +246,47 @@ export class WidgetManager implements IIPyWidgetManager, IMessageHandler {
const context = (globalThis as any).jupyter_vscode_rendererContext as RendererContext<any>;
const renderer = await context.getRenderer(rendererId);
const isImage = mime.toLowerCase().startsWith('image/') && !mime.toLowerCase().includes('svg');
renderer?.renderOutputItem(
{
id: new Date().getTime().toString(), // Not used except when saving plots, but with nested outputs, thats not possible.
metadata,
text: () => {
return JSON.stringify(data[mime]);
const renderOutputItem = renderer?.renderOutputItem as
| undefined
| ((outputItem: OutputItem, element: HTMLElement, signal: AbortSignal) => void);
if (renderOutputItem) {
renderOutputItem(
{
id: new Date().getTime().toString(), // Not used except when saving plots, but with nested outputs, thats not possible.
metadata,
text: () => {
if (
(mime.startsWith('text/') || mime.startsWith('image/svg+xml')) &&
typeof data[mime] === 'string'
) {
return data[mime] as string;
}
return JSON.stringify(data[mime]);
},
json: () => {
return data[mime];
},
blob() {
if (isImage) {
const bytes = base64ToUint8Array(data[mime] as string);
return new Blob([bytes], { type: mime });
} else {
throw new Error(`Not able to get blob for ${mime}.`);
}
},
data() {
if (isImage) {
return base64ToUint8Array(data[mime] as string);
} else {
throw new Error(`Not able to get blob for ${mime}.`);
}
},
mime
},
json: () => {
return data[mime];
},
blob() {
if (isImage) {
const bytes = base64ToUint8Array(data[mime] as string);
return new Blob([bytes], { type: mime });
} else {
throw new Error(`Not able to get blob for ${mime}.`);
}
},
data() {
if (isImage) {
return base64ToUint8Array(data[mime] as string);
} else {
throw new Error(`Not able to get blob for ${mime}.`);
}
},
mime
},
node
);
node,
new AbortController().signal
);
}
});
});
});
Expand Down
Loading