@@ -3,6 +3,10 @@ import { KernelMessage } from '@jupyterlab/services';
33import { ServiceContainer } from './services/ServiceContainer' ;
44
55const reconnectString : string = "%connect%"
6+ const saveString : string = "%save%" ;
7+ const bootFileName : string = "boot.py" ;
8+ const bootSavePrefix : string = `with open(${ bootFileName } , 'w') as f:\n f.write('''\n` ;
9+ const bootSaveSuffix : string = "\n''')\n" ;
610
711export class EmbeddedKernel extends BaseKernel {
812
@@ -76,6 +80,56 @@ export class EmbeddedKernel extends BaseKernel {
7680 await this . serviceContainer . deviceService . disconnect ( ) ;
7781 await this . serviceContainer . deviceService . connect ( this . outputResponse . bind ( this ) ) ;
7882 }
83+ if ( code . includes ( saveString ) ) {
84+ // Burn the code after the save string into the boot.py file
85+ this . outputResponse ( "Save command detected, saving code to boot.py..." ) ;
86+ const codeToSave = code . split ( saveString ) [ 1 ] . trim ( ) ;
87+ if ( ! codeToSave ) {
88+ return {
89+ status : 'error' ,
90+ execution_count : this . executionCount ,
91+ ename : 'ValueError' ,
92+ evalue : 'No code provided to save' ,
93+ traceback : [ 'Please provide code to save after the %save% command' ]
94+ } ;
95+ }
96+ // Burn the code by adding it to the boot.py file. Can call executeRequest with the code to save
97+ // but append the code to write it to the boot.py file at the start of the code.
98+ const bootCode = `# This code is automatically generated by the Embedded Kernel\n${ codeToSave } ` ;
99+ const bootCodeWithSave = `${ bootSavePrefix } ${ bootCode } ${ bootSaveSuffix } ` ;
100+
101+ // Execute the command to save the boot.py file
102+ if ( ! this . serviceContainer . deviceService . transport ) {
103+ console . log ( "[Kernel] executeRequest - No transport available for saving boot.py" ) ;
104+ return {
105+ status : 'error' ,
106+ execution_count : this . executionCount ,
107+ ename : 'TransportError' ,
108+ evalue : 'No transport available to save boot.py' ,
109+ traceback : [ 'Please connect a device first' ]
110+ } ;
111+ }
112+ const result = await this . serviceContainer . consoleService . executeCommand ( bootCodeWithSave , ( content ) => {
113+ console . log ( "[Kernel] executeRequest - Streaming output:" , content ) ;
114+ this . stream ( content ) ;
115+ } ) ;
116+ if ( ! result . success ) {
117+ console . log ( "[Kernel] executeRequest - Command execution failed:" , result . error ) ;
118+ return {
119+ status : 'error' ,
120+ execution_count : this . executionCount ,
121+ ename : 'ExecutionError' ,
122+ evalue : result . error || 'Unknown error' ,
123+ traceback : [ result . error || 'Unknown error' ]
124+ } ;
125+ }
126+ console . log ( "[Kernel] executeRequest - Code saved to boot.py successfully" ) ;
127+ return {
128+ status : 'ok' ,
129+ execution_count : this . executionCount ,
130+ user_expressions : { } ,
131+ } ;
132+ }
79133
80134 try {
81135 console . log ( "[Kernel] executeRequest - Checking transport" ) ;
@@ -97,7 +151,7 @@ export class EmbeddedKernel extends BaseKernel {
97151 console . log ( "[Kernel] executeRequest - Executing command via ConsoleService" ) ;
98152 // Execute the command and handle the output
99153 const result = await this . serviceContainer . consoleService . executeCommand ( code , ( content ) => {
100- console . log ( "[Kernel] executeRequest - Streaming output:" , content . text . substring ( 0 , 50 ) + ( content . text . length > 50 ? '...' : '' ) ) ;
154+ console . log ( "[Kernel] executeRequest - Streaming output:" , content ) ;
101155 this . stream ( content ) ;
102156 } ) ;
103157
0 commit comments