1+ const handle_output = function ( data ) {
2+ // data is the object passed to the callback from the kernel execution
3+ switch ( data . msg_type ) {
4+ case 'error' :
5+ GEPPETTO . CommandController . log ( "ERROR while executing a Python command:" ) ;
6+ GEPPETTO . CommandController . log ( data . content . evalue . trim ( ) ) ;
7+ console . error ( "ERROR while executing a Python command:" ) ;
8+ console . error ( data . content . traceback ) ;
9+ GEPPETTO . trigger ( GEPPETTO . Events . Error_while_exec_python_command , data . content ) ;
10+ GEPPETTO . trigger ( GEPPETTO . Events . Hide_spinner ) ;
11+ break ;
12+ case 'execute_result' :
13+ GEPPETTO . CommandController . log ( data . content . data [ 'text/plain' ] . trim ( ) , true ) ;
14+ try {
15+ var response = JSON . parse ( data . content . data [ 'text/plain' ] . replace ( / ^ ' ( .* ) ' $ / , '$1' ) ) ;
16+ } catch ( error ) {
17+ var response = data . content . data [ 'text/plain' ] . replace ( / ^ ' ( .* ) ' $ / , '$1' ) ;
18+ }
19+ GEPPETTO . trigger ( GEPPETTO . Events . Receive_Python_Message , { id : data . parent_header . msg_id , type : data . msg_type , response : response } ) ;
20+ break ;
21+ case "display_data" :
22+ // FIXME
23+ break ;
24+ default :
25+ GEPPETTO . CommandController . log ( data . content . text . trim ( ) , true ) ;
26+ }
27+ } ;
28+
29+ const execPythonMessage = function ( command , callback = handle_output ) {
30+ GEPPETTO . CommandController . log ( 'Executing Python command: ' + command , true ) ;
31+ var kernel = IPython . notebook . kernel ;
32+ var messageID = kernel . execute ( command , { iopub : { output : callback } } , { silent : false , stop_on_error : true , store_history : true } ) ;
33+
34+ return new Promise ( ( resolve , reject ) =>
35+ GEPPETTO . on ( GEPPETTO . Events . Receive_Python_Message , function ( data ) {
36+ if ( data . id == messageID ) {
37+ resolve ( data . response ) ;
38+ }
39+ } )
40+ ) ;
41+ } ;
42+
43+ const evalPythonMessage = function ( command , parameters , parse = true ) {
44+ var parametersString = '' ;
45+ if ( parameters ) {
46+ if ( parameters . length > 0 ) {
47+ parametersString = "(" + parameters . map ( parameter => "utils.convertToPython('" + JSON . stringify ( parameter ) + "')" ) . join ( "," ) + ")" ;
48+ } else {
49+ parametersString = '()' ;
50+ }
51+ }
52+
53+ var finalCommand = command + parametersString ;
54+ if ( parse ) {
55+ finalCommand = 'utils.convertToJS(' + finalCommand + ')'
56+ }
57+ return execPythonMessage ( finalCommand , handle_output ) ;
58+
59+ } ;
60+
61+ export { execPythonMessage , evalPythonMessage }
0 commit comments