Skip to content

Commit 4fa78e2

Browse files
committed
docs and metadata changes.
1 parent 3a4d579 commit 4fa78e2

File tree

9 files changed

+735
-376
lines changed

9 files changed

+735
-376
lines changed

build/esbuild/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ async function buildAll() {
327327
build(
328328
path.join(extensionFolder, 'src', 'webviews', 'webview-side', 'dataframe-renderer', 'index.ts'),
329329
path.join(extensionFolder, 'dist', 'webviews', 'webview-side', 'dataframeRenderer', 'dataframeRenderer.js'),
330-
{ target: 'web', watch: watchAll }
330+
{ target: 'web', watch: isWatchMode }
331331
),
332332
build(
333333
path.join(extensionFolder, 'src', 'webviews', 'webview-side', 'variable-view', 'index.tsx'),

src/kernels/execution/cellExecutionMessageHandler.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ export class CellExecutionMessageHandler implements IDisposable {
644644
}
645645
// Use cell metadata id if available (from Deepnote blocks), otherwise fall back to cell's internal id
646646
const cellId = getCellId(this.cell);
647-
const cellOutput = cellOutputToVSCCellOutput(output, this.cell.index, cellId);
647+
const cellMetadata = this.cell.metadata || {};
648+
const cellOutput = cellOutputToVSCCellOutput(output, this.cell.index, cellId, cellMetadata);
648649
const displayId =
649650
'transient' in output &&
650651
typeof output.transient === 'object' &&
@@ -1015,14 +1016,16 @@ export class CellExecutionMessageHandler implements IDisposable {
10151016
if (this.lastUsedStreamOutput?.stream === msg.content.name) {
10161017
// Use cell metadata id if available (from Deepnote blocks), otherwise fall back to cell's internal id
10171018
const cellId = getCellId(this.cell);
1019+
const cellMetadata = this.cell.metadata || {};
10181020
const output = cellOutputToVSCCellOutput(
10191021
{
10201022
output_type: 'stream',
10211023
name: msg.content.name,
10221024
text: msg.content.text
10231025
},
10241026
this.cell.index,
1025-
cellId
1027+
cellId,
1028+
cellMetadata
10261029
);
10271030
traceCellMessage(this.cell, `Append output items '${msg.content.text.substring(0, 100)}`);
10281031
task?.appendOutputItems(output.items, this.lastUsedStreamOutput.output).then(noop, noop);
@@ -1031,14 +1034,16 @@ export class CellExecutionMessageHandler implements IDisposable {
10311034
const text = concatMultilineString(msg.content.text);
10321035
// Use cell metadata id if available (from Deepnote blocks), otherwise fall back to cell's internal id
10331036
const cellId = getCellId(this.cell);
1037+
const cellMetadata = this.cell.metadata || {};
10341038
const output = cellOutputToVSCCellOutput(
10351039
{
10361040
output_type: 'stream',
10371041
name: msg.content.name,
10381042
text
10391043
},
10401044
this.cell.index,
1041-
cellId
1045+
cellId,
1046+
cellMetadata
10421047
);
10431048
this.lastUsedStreamOutput = { output, stream: msg.content.name };
10441049
traceCellMessage(this.cell, `Replace output with '${text.substring(0, 100)}'`);
@@ -1048,14 +1053,16 @@ export class CellExecutionMessageHandler implements IDisposable {
10481053
const text = formatStreamText(concatMultilineString(msg.content.text));
10491054
// Use cell metadata id if available (from Deepnote blocks), otherwise fall back to cell's internal id
10501055
const cellId = getCellId(this.cell);
1056+
const cellMetadata = this.cell.metadata || {};
10511057
const output = cellOutputToVSCCellOutput(
10521058
{
10531059
output_type: 'stream',
10541060
name: msg.content.name,
10551061
text
10561062
},
10571063
this.cell.index,
1058-
cellId
1064+
cellId,
1065+
cellMetadata
10591066
);
10601067
this.lastUsedStreamOutput = { output, stream: msg.content.name };
10611068
traceCellMessage(this.cell, `Append new output '${text.substring(0, 100)}'`);
@@ -1176,14 +1183,16 @@ export class CellExecutionMessageHandler implements IDisposable {
11761183
);
11771184
// Use cell metadata id if available (from Deepnote blocks), otherwise fall back to cell's internal id
11781185
const cellId = getCellId(outputToBeUpdated.cell);
1186+
const cellMetadata = outputToBeUpdated.cell.metadata || {};
11791187
const newOutput = cellOutputToVSCCellOutput(
11801188
{
11811189
...output,
11821190
data: msg.content.data,
11831191
metadata: msg.content.metadata
11841192
} as nbformat.IDisplayData,
11851193
outputToBeUpdated.cell.index,
1186-
cellId
1194+
cellId,
1195+
cellMetadata
11871196
);
11881197
// If there was no output and still no output, then nothing to do.
11891198
if (outputToBeUpdated.outputItems.length === 0 && newOutput.items.length === 0) {

src/kernels/execution/helpers.ts

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ export function traceCellMessage(cell: NotebookCell, message: string | (() => st
117117

118118
const cellOutputMappers = new Map<
119119
nbformat.OutputType,
120-
(output: nbformat.IOutput, cellIndex?: number, cellId?: string) => NotebookCellOutput
120+
(
121+
output: nbformat.IOutput,
122+
cellIndex?: number,
123+
cellId?: string,
124+
cellMetadata?: Record<string, unknown>
125+
) => NotebookCellOutput
121126
>();
122127
// eslint-disable-next-line @typescript-eslint/no-explicit-any
123128
cellOutputMappers.set('display_data', translateDisplayDataOutput as any);
@@ -132,7 +137,8 @@ cellOutputMappers.set('update_display_data', translateDisplayDataOutput as any);
132137
export function cellOutputToVSCCellOutput(
133138
output: nbformat.IOutput,
134139
cellIndex?: number,
135-
cellId?: string
140+
cellId?: string,
141+
cellMetadata?: Record<string, unknown>
136142
): NotebookCellOutput {
137143
/**
138144
* Stream, `application/x.notebook.stream`
@@ -160,29 +166,39 @@ export function cellOutputToVSCCellOutput(
160166
const fn = cellOutputMappers.get(output.output_type as nbformat.OutputType);
161167
let result: NotebookCellOutput;
162168
if (fn) {
163-
result = fn(output, cellIndex, cellId);
169+
result = fn(output, cellIndex, cellId, cellMetadata);
164170
} else {
165171
logger.warn(`Unable to translate cell from ${output.output_type} to NotebookCellData for VS Code.`);
166172
// eslint-disable-next-line @typescript-eslint/no-explicit-any
167-
result = translateDisplayDataOutput(output as any, cellIndex, cellId);
173+
result = translateDisplayDataOutput(output as any, cellIndex, cellId, cellMetadata);
168174
}
169175
return result;
170176
}
171177

172-
function getOutputMetadata(output: nbformat.IOutput, cellIndex?: number, cellId?: string): CellOutputMetadata {
173-
// Add on transient data if we have any. This should be removed by our save functions elsewhere.
178+
function getOutputMetadata(
179+
output: nbformat.IOutput,
180+
cellIndex?: number,
181+
cellId?: string,
182+
cellMetadata?: Record<string, unknown>
183+
): CellOutputMetadata {
184+
// Merge in order: cellId, cellMetadata, cellIndex, then output-specific metadata (output metadata wins conflicts)
174185
const metadata: CellOutputMetadata = {
175186
outputType: output.output_type
176187
};
177188

178-
if (cellIndex !== undefined) {
179-
metadata.cellIndex = cellIndex;
180-
}
181-
182189
if (cellId) {
183190
metadata.cellId = cellId;
184191
}
185192

193+
// Merge cell metadata next (block-level metadata from Deepnote)
194+
if (cellMetadata) {
195+
Object.assign(metadata, cellMetadata);
196+
}
197+
198+
if (cellIndex !== undefined) {
199+
metadata.cellIndex = cellIndex;
200+
}
201+
186202
if (output.transient) {
187203
// eslint-disable-next-line @typescript-eslint/no-explicit-any
188204
metadata.transient = output.transient as any;
@@ -193,7 +209,10 @@ function getOutputMetadata(output: nbformat.IOutput, cellIndex?: number, cellId?
193209
case 'execute_result':
194210
case 'update_display_data': {
195211
metadata.executionCount = output.execution_count;
196-
metadata.metadata = output.metadata ? JSON.parse(JSON.stringify(output.metadata)) : {};
212+
// Output metadata is merged last so it overrides block metadata
213+
if (output.metadata) {
214+
Object.assign(metadata, output.metadata);
215+
}
197216
break;
198217
}
199218
default:
@@ -218,7 +237,8 @@ export function getNotebookCellOutputMetadata(output: {
218237
function translateDisplayDataOutput(
219238
output: nbformat.IDisplayData | nbformat.IDisplayUpdate | nbformat.IExecuteResult,
220239
cellIndex?: number,
221-
cellId?: string
240+
cellId?: string,
241+
cellMetadata?: Record<string, unknown>
222242
): NotebookCellOutput {
223243
// Metadata could be as follows:
224244
// We'll have metadata specific to each mime type as well as generic metadata.
@@ -237,7 +257,7 @@ function translateDisplayDataOutput(
237257
}
238258
}
239259
*/
240-
const metadata = getOutputMetadata(output, cellIndex, cellId);
260+
const metadata = getOutputMetadata(output, cellIndex, cellId, cellMetadata);
241261
// If we have SVG or PNG, then add special metadata to indicate whether to display `open plot`
242262
if ('image/svg+xml' in output.data || 'image/png' in output.data) {
243263
metadata.__displayOpenPlotIcon = true;
@@ -253,10 +273,15 @@ function translateDisplayDataOutput(
253273
return new NotebookCellOutput(sortOutputItemsBasedOnDisplayOrder(items), metadata);
254274
}
255275

256-
function translateStreamOutput(output: nbformat.IStream, cellIndex?: number, cellId?: string): NotebookCellOutput {
276+
function translateStreamOutput(
277+
output: nbformat.IStream,
278+
cellIndex?: number,
279+
cellId?: string,
280+
cellMetadata?: Record<string, unknown>
281+
): NotebookCellOutput {
257282
const value = concatMultilineString(output.text);
258283
const factoryFn = output.name === 'stderr' ? NotebookCellOutputItem.stderr : NotebookCellOutputItem.stdout;
259-
return new NotebookCellOutput([factoryFn(value)], getOutputMetadata(output, cellIndex, cellId));
284+
return new NotebookCellOutput([factoryFn(value)], getOutputMetadata(output, cellIndex, cellId, cellMetadata));
260285
}
261286

262287
// Output stream can only have stderr or stdout so just check the first output. Undefined if no outputs
@@ -568,7 +593,12 @@ export function translateCellDisplayOutput(output: NotebookCellOutput): JupyterO
568593
* As we're displaying the error in the statusbar, we don't want this dup error in output.
569594
* Hence remove this.
570595
*/
571-
function translateErrorOutput(output?: nbformat.IError, cellIndex?: number, cellId?: string): NotebookCellOutput {
596+
function translateErrorOutput(
597+
output?: nbformat.IError,
598+
cellIndex?: number,
599+
cellId?: string,
600+
cellMetadata?: Record<string, unknown>
601+
): NotebookCellOutput {
572602
output = output || { output_type: 'error', ename: '', evalue: '', traceback: [] };
573603
return new NotebookCellOutput(
574604
[
@@ -578,7 +608,7 @@ function translateErrorOutput(output?: nbformat.IError, cellIndex?: number, cell
578608
stack: (output?.traceback || []).join('\n')
579609
})
580610
],
581-
{ ...getOutputMetadata(output, cellIndex, cellId), originalError: output }
611+
{ ...getOutputMetadata(output, cellIndex, cellId, cellMetadata), originalError: output }
582612
);
583613
}
584614

0 commit comments

Comments
 (0)