@@ -32,6 +32,7 @@ import { KernelError } from '../errors/kernelError';
3232import { getCachedSysPrefix } from '../../platform/interpreter/helpers' ;
3333import { getCellMetadata } from '../../platform/common/utils' ;
3434import { NotebookCellExecutionState , notebookCellExecutions } from '../../platform/notebooks/cellExecutionStateService' ;
35+ import dedent from 'dedent' ;
3536
3637/**
3738 * Factory for CellExecution objects.
@@ -406,9 +407,21 @@ export class CellExecution implements ICellExecution, IDisposable {
406407 return this . completedSuccessfully ( ) ;
407408 }
408409
409- // Prepend initialization code
410- const prependCode = 'print("Hello world")' ;
411- code = prependCode + '\n' + code ;
410+ const tableState =
411+ 'deepnote_table_state' in this . cell . metadata ? this . cell . metadata . deepnote_table_state : undefined ;
412+
413+ if ( tableState ) {
414+ const tableStateAsJson = JSON . stringify ( tableState ) ;
415+
416+ code = dedent `
417+ if '_dntk' in globals():
418+ _dntk.dataframe_utils.configure_dataframe_formatter(${ escapePythonString ( tableStateAsJson ) } )
419+ else:
420+ _deepnote_current_table_attrs = ${ escapePythonString ( tableStateAsJson ) }
421+
422+ ${ code }
423+ ` ;
424+ }
412425
413426 // Generate metadata from our cell (some kernels expect this.)
414427 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -505,3 +518,11 @@ export class CellExecution implements ICellExecution, IDisposable {
505518 }
506519 }
507520}
521+
522+ function escapePythonString ( value : string ) : string {
523+ // We have to escape backslashes, single quotes, and newlines
524+ const escaped = value . replaceAll ( '\\' , '\\\\' ) . replaceAll ( "'" , "\\'" ) . replaceAll ( '\n' , '\\n' ) ;
525+
526+ // Wrap the escaped string in single quotes
527+ return `'${ escaped } '` ;
528+ }
0 commit comments