Skip to content

Commit 7878345

Browse files
committed
feat: Pass block metadata to cell outputs for renderer to access
1 parent 62297c2 commit 7878345

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export const OUTPUT_BLOCK_METADATA_KEY = 'blockMetadata';
2+
13
export const CHART_BIG_NUMBER_MIME_TYPE = 'application/vnd.deepnote.chart.big-number+json';

src/notebooks/deepnote/deepnoteDataConverter.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { addPocketToCellMetadata, createBlockFromPocket } from './pocket';
88
import { TextBlockConverter } from './converters/textBlockConverter';
99
import { MarkdownBlockConverter } from './converters/markdownBlockConverter';
1010
import { ChartBigNumberBlockConverter } from './converters/chartBigNumberBlockConverter';
11-
import { CHART_BIG_NUMBER_MIME_TYPE } from './deepnoteConstants';
11+
import { CHART_BIG_NUMBER_MIME_TYPE, OUTPUT_BLOCK_METADATA_KEY } from './deepnoteConstants';
1212

1313
/**
1414
* Utility class for converting between Deepnote block structures and VS Code notebook cells.
@@ -57,7 +57,7 @@ export class DeepnoteDataConverter {
5757
// The pocket is a place to tuck away Deepnote-specific fields for later.
5858
addPocketToCellMetadata(cell);
5959

60-
cell.outputs = this.transformOutputsForVsCode(block.type, block.outputs || []);
60+
cell.outputs = this.transformOutputsForVsCode(block.type, block.metadata, block.outputs || []);
6161

6262
return cell;
6363
});
@@ -198,6 +198,14 @@ export class DeepnoteDataConverter {
198198

199199
if (Object.keys(restMetadata).length > 0) {
200200
(deepnoteOutput as DeepnoteOutput & { metadata?: Record<string, unknown> }).metadata = restMetadata;
201+
202+
if (
203+
deepnoteOutput.metadata != null &&
204+
typeof deepnoteOutput.metadata === 'object' &&
205+
OUTPUT_BLOCK_METADATA_KEY in deepnoteOutput.metadata
206+
) {
207+
delete deepnoteOutput.metadata[OUTPUT_BLOCK_METADATA_KEY];
208+
}
201209
}
202210
}
203211

@@ -207,6 +215,7 @@ export class DeepnoteDataConverter {
207215

208216
private transformOutputsForVsCode(
209217
blockType: DeepnoteBlock['type'],
218+
blockMetadata: DeepnoteBlock['metadata'],
210219
outputs: DeepnoteOutput[]
211220
): NotebookCellOutput[] {
212221
return outputs.map((output) => {
@@ -294,7 +303,9 @@ export class DeepnoteDataConverter {
294303
}
295304

296305
// Preserve metadata and execution_count
297-
const metadata: Record<string, unknown> = {};
306+
const metadata: Record<string, unknown> = {
307+
blockMetadata
308+
};
298309

299310
if (output.execution_count !== undefined) {
300311
metadata.executionCount = output.execution_count;

src/notebooks/deepnote/deepnoteSchemas.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { z } from 'zod';
22

3+
import { OUTPUT_BLOCK_METADATA_KEY } from './deepnoteConstants';
4+
35
export const DeepnoteChartBigNumberOutputSchema = z.object({
46
title: z.string().nullish(),
57
value: z.string().nullish(),
@@ -43,5 +45,11 @@ export const DeepnoteBigNumberMetadataSchema = z.object({
4345
.transform((val) => val ?? null)
4446
});
4547

48+
export function getDeepnoteBlockMetadataSchema<T extends z.ZodTypeAny>(schema: T) {
49+
return z.object({
50+
[OUTPUT_BLOCK_METADATA_KEY]: schema
51+
});
52+
}
53+
4654
export type DeepnoteChartBigNumberOutput = z.infer<typeof DeepnoteChartBigNumberOutputSchema>;
4755
export type DeepnoteBigNumberMetadata = z.infer<typeof DeepnoteBigNumberMetadataSchema>;

src/webviews/webview-side/chart-big-number-renderer/ChartBigNumberOutputRenderer.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ export function ChartBigNumberOutputRenderer({
118118
return 'deepnote-comparison-positive';
119119
}, [changeDirection, metadata.deepnote_big_number_comparison_format]);
120120

121+
const showComparison =
122+
metadata.deepnote_big_number_comparison_enabled === true &&
123+
metadata.deepnote_big_number_comparison_type != null;
124+
121125
return (
122126
<div className="deepnote-big-number-container">
123127
<div className="deepnote-big-number-card">
@@ -128,18 +132,20 @@ export function ChartBigNumberOutputRenderer({
128132
<div>
129133
<p className="deepnote-big-number-value">{value}</p>
130134
</div>
131-
<div className="deepnote-big-number-comparison">
132-
<div>
133-
<p className={`deepnote-comparison-text ${comparisonClassName}`}>
134-
{formattedComparisonValue}
135-
</p>
136-
</div>
137-
{output.comparisonTitle != null ? (
135+
{showComparison ? (
136+
<div className="deepnote-big-number-comparison">
138137
<div>
139-
<p className="deepnote-comparison-title">{output.comparisonTitle}</p>
138+
<p className={`deepnote-comparison-text ${comparisonClassName}`}>
139+
{formattedComparisonValue}
140+
</p>
140141
</div>
141-
) : null}
142-
</div>
142+
{output.comparisonTitle != null ? (
143+
<div>
144+
<p className="deepnote-comparison-title">{output.comparisonTitle}</p>
145+
</div>
146+
) : null}
147+
</div>
148+
) : null}
143149
</div>
144150
</div>
145151
</div>

src/webviews/webview-side/chart-big-number-renderer/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type { ActivationFunction, OutputItem, RendererContext } from 'vscode-not
88
import { ChartBigNumberOutputRenderer } from './ChartBigNumberOutputRenderer';
99
import {
1010
DeepnoteBigNumberMetadataSchema,
11-
DeepnoteChartBigNumberOutputSchema
11+
DeepnoteChartBigNumberOutputSchema,
12+
getDeepnoteBlockMetadataSchema
1213
} from '../../../notebooks/deepnote/deepnoteSchemas';
1314

1415
export const activate: ActivationFunction = (_context: RendererContext<unknown>) => {
@@ -17,15 +18,20 @@ export const activate: ActivationFunction = (_context: RendererContext<unknown>)
1718
try {
1819
// Remove single quotes from start and end of string if present
1920
const data = JSON.parse(outputItem.text().replace(/^'|'$/g, ''));
20-
const metadata = DeepnoteBigNumberMetadataSchema.parse(outputItem.metadata);
21+
const { blockMetadata } = getDeepnoteBlockMetadataSchema(DeepnoteBigNumberMetadataSchema).parse(
22+
outputItem.metadata
23+
);
2124

2225
const chartBigNumberOutput = DeepnoteChartBigNumberOutputSchema.parse(data);
2326

2427
const root = document.createElement('div');
2528
element.appendChild(root);
2629

2730
ReactDOM.render(
28-
React.createElement(ChartBigNumberOutputRenderer, { output: chartBigNumberOutput, metadata }),
31+
React.createElement(ChartBigNumberOutputRenderer, {
32+
output: chartBigNumberOutput,
33+
metadata: blockMetadata
34+
}),
2935
root
3036
);
3137
} catch (error) {

0 commit comments

Comments
 (0)