@@ -3,6 +3,10 @@ import { KernelMessage } from '@jupyterlab/services';
3
3
import { ServiceContainer } from './services/ServiceContainer' ;
4
4
5
5
const 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" ;
6
10
7
11
export class EmbeddedKernel extends BaseKernel {
8
12
@@ -76,6 +80,56 @@ export class EmbeddedKernel extends BaseKernel {
76
80
await this . serviceContainer . deviceService . disconnect ( ) ;
77
81
await this . serviceContainer . deviceService . connect ( this . outputResponse . bind ( this ) ) ;
78
82
}
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 . getTransport ( ) ) {
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
+ }
79
133
80
134
try {
81
135
console . log ( "[Kernel] executeRequest - Checking transport" ) ;
@@ -97,7 +151,7 @@ export class EmbeddedKernel extends BaseKernel {
97
151
console . log ( "[Kernel] executeRequest - Executing command via ConsoleService" ) ;
98
152
// Execute the command and handle the output
99
153
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 ) ;
101
155
this . stream ( content ) ;
102
156
} ) ;
103
157
0 commit comments