@@ -17,6 +17,7 @@ import { IExtensionSyncActivationService } from '../../platform/activation/types
1717import { IDisposableRegistry } from '../../platform/common/types' ;
1818import { Commands } from '../../platform/common/constants' ;
1919import { chainWithPendingUpdates } from '../../kernels/execution/notebookUpdater' ;
20+ import { WrappedError } from '../../platform/errors/types' ;
2021import {
2122 DeepnoteBigNumberMetadataSchema ,
2223 DeepnoteTextInputMetadataSchema ,
@@ -30,6 +31,7 @@ import {
3031 DeepnoteButtonMetadataSchema ,
3132 DeepnoteSqlMetadata
3233} from './deepnoteSchemas' ;
34+ import { DATAFRAME_SQL_INTEGRATION_ID } from '../../platform/notebooks/deepnote/integrationTypes' ;
3335
3436export type InputBlockType =
3537 | 'input-text'
@@ -75,7 +77,10 @@ export function getInputBlockMetadata(blockType: InputBlockType, variableName: s
7577
7678export function safeParseDeepnoteVariableNameFromContentJson ( content : string ) : string | undefined {
7779 try {
78- const variableNameResult = z . string ( ) . safeParse ( JSON . parse ( content ) [ 'deepnote_variable_name' ] ) ;
80+ const parsed = JSON . parse ( content ) ;
81+ // Chart blocks use 'variable' key, other blocks use 'deepnote_variable_name'
82+ const variableName = parsed [ 'variable' ] ?? parsed [ 'deepnote_variable_name' ] ;
83+ const variableNameResult = z . string ( ) . safeParse ( variableName ) ;
7984 return variableNameResult . success ? variableNameResult . data : undefined ;
8085 } catch ( error ) {
8186 logger . error ( 'Error parsing deepnote variable name from content JSON' , error ) ;
@@ -147,6 +152,7 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
147152 this . disposableRegistry . push (
148153 commands . registerCommand ( Commands . AddBigNumberChartBlock , ( ) => this . addBigNumberChartBlock ( ) )
149154 ) ;
155+ this . disposableRegistry . push ( commands . registerCommand ( Commands . AddChartBlock , ( ) => this . addChartBlock ( ) ) ) ;
150156 this . disposableRegistry . push (
151157 commands . registerCommand ( Commands . AddInputTextBlock , ( ) => this . addInputBlock ( 'input-text' ) )
152158 ) ;
@@ -189,7 +195,7 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
189195 const defaultMetadata : DeepnoteSqlMetadata = {
190196 deepnote_variable_name : deepnoteVariableName ,
191197 deepnote_return_variable_type : 'dataframe' ,
192- sql_integration_id : 'deepnote-dataframe-sql'
198+ sql_integration_id : DATAFRAME_SQL_INTEGRATION_ID
193199 } ;
194200
195201 // Determine the index where to insert the new cell (below current selection or at the end)
@@ -261,6 +267,61 @@ export class DeepnoteNotebookCommandListener implements IExtensionSyncActivation
261267 await commands . executeCommand ( 'notebook.cell.edit' ) ;
262268 }
263269
270+ public async addChartBlock ( ) : Promise < void > {
271+ const editor = window . activeNotebookEditor ;
272+
273+ if ( ! editor ) {
274+ throw new WrappedError ( l10n . t ( 'No active notebook editor found' ) ) ;
275+ }
276+
277+ const document = editor . notebook ;
278+ const selection = editor . selection ;
279+ const insertIndex = selection ? selection . end : document . cellCount ;
280+
281+ const defaultVisualizationSpec = {
282+ mark : 'line' ,
283+ $schema : 'https://vega.github.io/schema/vega-lite/v5.json' ,
284+ data : { values : [ ] } ,
285+ encoding : {
286+ x : { field : 'x' , type : 'quantitative' } ,
287+ y : { field : 'y' , type : 'quantitative' }
288+ }
289+ } ;
290+
291+ const cellContent = {
292+ variable : 'df_1' ,
293+ spec : defaultVisualizationSpec ,
294+ filters : [ ]
295+ } ;
296+
297+ const metadata = {
298+ __deepnotePocket : {
299+ type : 'visualization'
300+ }
301+ } ;
302+
303+ const result = await chainWithPendingUpdates ( document , ( edit ) => {
304+ const newCell = new NotebookCellData ( NotebookCellKind . Code , JSON . stringify ( cellContent , null , 2 ) , 'json' ) ;
305+
306+ newCell . metadata = metadata ;
307+
308+ const nbEdit = NotebookEdit . insertCells ( insertIndex , [ newCell ] ) ;
309+
310+ edit . set ( document . uri , [ nbEdit ] ) ;
311+ } ) ;
312+
313+ if ( result !== true ) {
314+ throw new WrappedError ( l10n . t ( 'Failed to insert chart block' ) ) ;
315+ }
316+
317+ const notebookRange = new NotebookRange ( insertIndex , insertIndex + 1 ) ;
318+
319+ editor . revealRange ( notebookRange , NotebookEditorRevealType . Default ) ;
320+ editor . selection = notebookRange ;
321+
322+ await commands . executeCommand ( 'notebook.cell.edit' ) ;
323+ }
324+
264325 public async addInputBlock ( blockType : InputBlockType ) : Promise < void > {
265326 const editor = window . activeNotebookEditor ;
266327 if ( ! editor ) {
0 commit comments