Skip to content

Commit 3e14861

Browse files
committed
feat: Enhance Deepnote notebook functionality with SQL metadata support
- Added a new schema for Deepnote SQL metadata to facilitate better handling of SQL-related input blocks. - Updated the Deepnote notebook command listener to utilize the new SQL metadata, improving variable name management and default metadata setup for new cells. - Enhanced the cell insertion logic to ensure proper selection and editing of newly created cells. Signed-off-by: Tomas Kislan <tomas@kislan.sk>
1 parent 6afab36 commit 3e14861

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

src/notebooks/deepnote/deepnoteNotebookCommandListener.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
NotebookCellKind,
77
NotebookEdit,
88
NotebookRange,
9-
NotebookCell
9+
NotebookCell,
10+
NotebookEditorRevealType
1011
} from 'vscode';
1112
import z from 'zod';
1213

@@ -25,7 +26,8 @@ import {
2526
DeepnoteDateInputMetadataSchema,
2627
DeepnoteDateRangeInputMetadataSchema,
2728
DeepnoteFileInputMetadataSchema,
28-
DeepnoteButtonMetadataSchema
29+
DeepnoteButtonMetadataSchema,
30+
DeepnoteSqlMetadata
2931
} from './deepnoteSchemas';
3032

3133
type InputBlockType =
@@ -88,12 +90,17 @@ export function getNextDeepnoteVariableName(cells: NotebookCell[], prefix: 'df'
8890
acc.push(contentValue);
8991
}
9092

91-
const parsedMetadataValue = z.string().safeParse(cell.metadata.__deepnotePocket?.variableName);
92-
93+
const parsedMetadataValue = z.string().safeParse(cell.metadata['deepnote_variable_name']);
9394
if (parsedMetadataValue.success) {
9495
acc.push(parsedMetadataValue.data);
9596
}
9697

98+
const parsedPocketMetadataValue = z.string().safeParse(cell.metadata.__deepnotePocket?.deepnote_variable_name);
99+
100+
if (parsedPocketMetadataValue.success) {
101+
acc.push(parsedPocketMetadataValue.data);
102+
}
103+
97104
return acc;
98105
}, []);
99106

@@ -175,6 +182,14 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
175182
}
176183
const document = editor.notebook;
177184
const selection = editor.selection;
185+
const cells = editor.notebook.getCells();
186+
const deepnoteVariableName = getNextDeepnoteVariableName(cells, 'df');
187+
188+
const defaultMetadata: DeepnoteSqlMetadata = {
189+
deepnote_variable_name: deepnoteVariableName,
190+
deepnote_return_variable_type: 'dataframe',
191+
sql_integration_id: ''
192+
};
178193

179194
// Determine the index where to insert the new cell (below current selection or at the end)
180195
const insertIndex = selection ? selection.end : document.cellCount;
@@ -185,14 +200,22 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
185200
const newCell = new NotebookCellData(NotebookCellKind.Code, '', 'sql');
186201
newCell.metadata = {
187202
__deepnotePocket: {
188-
type: 'sql'
189-
}
203+
type: 'sql',
204+
...defaultMetadata
205+
},
206+
...defaultMetadata
190207
};
191208
const nbEdit = NotebookEdit.insertCells(insertIndex, [newCell]);
192209
edit.set(document.uri, [nbEdit]);
193210
}).then(
194211
() => {
195-
editor.selection = new NotebookRange(insertIndex, insertIndex + 1);
212+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
213+
editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
214+
editor.selection = notebookRange;
215+
// Enter edit mode on the new cell
216+
commands
217+
.executeCommand('notebook.cell.edit')
218+
.then(undefined, (error) => logger.error('Error entering edit mode', error));
196219
},
197220
(error) => {
198221
logger.error('Error inserting SQL block', error);
@@ -231,7 +254,13 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
231254
edit.set(document.uri, [nbEdit]);
232255
}).then(
233256
() => {
234-
editor.selection = new NotebookRange(insertIndex, insertIndex + 1);
257+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
258+
editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
259+
editor.selection = notebookRange;
260+
// Enter edit mode on the new cell
261+
commands
262+
.executeCommand('notebook.cell.edit')
263+
.then(undefined, (error) => logger.error('Error entering edit mode', error));
235264
},
236265
(error) => {
237266
logger.error('Error inserting big number chart block', error);
@@ -273,7 +302,13 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
273302
edit.set(document.uri, [nbEdit]);
274303
}).then(
275304
() => {
276-
editor.selection = new NotebookRange(insertIndex, insertIndex + 1);
305+
const notebookRange = new NotebookRange(insertIndex, insertIndex + 1);
306+
editor.revealRange(notebookRange, NotebookEditorRevealType.Default);
307+
editor.selection = notebookRange;
308+
// Enter edit mode on the new cell
309+
commands
310+
.executeCommand('notebook.cell.edit')
311+
.then(undefined, (error) => logger.error('Error entering edit mode', error));
277312
},
278313
(error) => {
279314
logger.error('Error inserting input block', error);

src/notebooks/deepnote/deepnoteSchemas.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ export const DeepnoteBigNumberMetadataSchema = z.object({
4343
.transform((val) => val ?? false)
4444
});
4545

46+
export const DeepnoteSqlMetadataSchema = z.object({
47+
deepnote_variable_name: z
48+
.string()
49+
.nullish()
50+
.transform((val) => val ?? ''),
51+
deepnote_return_variable_type: z
52+
.enum(['dataframe', 'query_preview'])
53+
.nullish()
54+
.transform((val) => val ?? 'dataframe'),
55+
sql_integration_id: z
56+
.string()
57+
.nullish()
58+
.transform((val) => val ?? '')
59+
});
60+
4661
// Base schema with common fields for all input types
4762
const DeepnoteBaseInputMetadataSchema = z.object({
4863
deepnote_variable_name: z
@@ -213,3 +228,4 @@ export const DeepnoteButtonMetadataSchema = DeepnoteBaseInputMetadataSchema.exte
213228

214229
export type DeepnoteChartBigNumberOutput = z.infer<typeof DeepnoteChartBigNumberOutputSchema>;
215230
export type DeepnoteBigNumberMetadata = z.infer<typeof DeepnoteBigNumberMetadataSchema>;
231+
export type DeepnoteSqlMetadata = z.infer<typeof DeepnoteSqlMetadataSchema>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function ChartBigNumberOutputRenderer({
2424
return 'NaN';
2525
}
2626

27-
return formatValue(parsedValue, metadata.deepnote_big_number_format ?? 'number');
27+
return formatValue(parsedValue, metadata.deepnote_big_number_format);
2828
}, [output.value, metadata.deepnote_big_number_format]);
2929

3030
const comparisonValue = useMemo(() => {

0 commit comments

Comments
 (0)