Skip to content

Commit 5a994fe

Browse files
Fix broken Vega/Altair plot rendering. (#47)
Co-authored-by: Don Jayamanne <[email protected]> Co-authored-by: Don Jayamanne <[email protected]>
1 parent 2e3c255 commit 5a994fe

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

build/webpack/webpack.client.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = {
3535
memoryLimit: 9096
3636
}),
3737
new DefinePlugin({
38-
scriptUrl: 'import.meta.url'
38+
scriptUrl: 'import.meta.url',
39+
'process.env': '{}' // utils references `process.env.xxx`
3940
}),
4041
new StringReplacePlugin(),
4142
...common.getDefaultPlugins('extension')
@@ -50,7 +51,7 @@ module.exports = {
5051
fallback: {
5152
fs: false,
5253
path: require.resolve('path-browserify'),
53-
util: require.resolve('util')
54+
util: require.resolve('util') // vega uses `util.promisify` (we need something that works outside node)
5455
},
5556
extensions: ['.ts', '.tsx', '.js', '.json', '.svg']
5657
},

src/client/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function renderOutput(outputItem: OutputItem, element: HTMLElement, ctx: Rendere
5252
function convertVSCodeOutputToExecuteResultOrDisplayData(
5353
outputItem: OutputItem
5454
): nbformat.IExecuteResult | nbformat.IDisplayData {
55-
const isImage = outputItem.mime.toLowerCase().startsWith('image/') && !outputItem.mime.toLowerCase().includes('svg');
55+
const isImage =
56+
outputItem.mime.toLowerCase().startsWith('image/') && !outputItem.mime.toLowerCase().includes('svg');
5657
// We add a metadata item `__isJson` to tell us whether the data is of type JSON or not.
5758
const isJson = (outputItem.metadata as Record<string, unknown>)?.__isJson === true;
5859
const value = isImage ? outputItem.blob() : isJson ? outputItem.json() : outputItem.text();

src/client/render.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,20 @@ export class CellOutput extends React.Component<ICellOutputProps> {
137137
);
138138
}
139139
private renderOutput(data: nbformat.MultilineString | JSONObject, mimeType?: string) {
140-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unused-vars, no-unused-vars
141-
const Transform = getTransform(this.props.mimeType!);
140+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/no-explicit-any
141+
const Transform: any = getTransform(this.props.mimeType!);
142+
const vegaPlot = mimeType && isVegaPlot(mimeType);
142143
const divStyle: React.CSSProperties = {
143-
backgroundColor: mimeType && isAltairPlot(mimeType) ? 'white' : undefined
144+
backgroundColor: vegaPlot ? 'white' : undefined
144145
};
146+
if (vegaPlot) {
147+
// Vega library expects data to be passed as serialized JSON instead of a native
148+
// JS object.
149+
data = typeof data === 'string' ? data : JSON.stringify(data);
150+
}
145151
return (
146152
<div style={divStyle}>
147-
<Transform data={data} />
153+
<Transform data={data} onError={console.error} />
148154
</div>
149155
);
150156
}
@@ -155,6 +161,6 @@ export class CellOutput extends React.Component<ICellOutputProps> {
155161
}
156162
}
157163

158-
function isAltairPlot(mimeType: string) {
164+
function isVegaPlot(mimeType: string) {
159165
return mimeType.includes('application/vnd.vega');
160166
}

0 commit comments

Comments
 (0)