1- function getDebugInformation ( ) {
1+ window . comlGetRunningCellIndex = function ( ) {
22 const runningCells = $ ( ".running" ) ;
33 if ( runningCells . length === 0 ) {
44 console . warn ( "No running cell" ) ;
55 return null ;
66 }
77 const cellIndex = Jupyter . notebook . get_cell_elements ( ) . index ( runningCells [ 0 ] ) ;
8- if ( cellIndex <= 0 ) {
9- console . warn ( "No previous cell") ;
8+ if ( cellIndex < 0 ) {
9+ console . error ( "Running cell not found in cell list. ") ;
1010 return null ;
1111 }
12- const cell = IPython . notebook . get_cell ( cellIndex - 1 ) ;
13- const cellDump = cell . toJSON ( ) ;
14- return cellDump ;
12+ return cellIndex ;
1513}
1614
17- IPython . CodeCell . prototype . native_handle_input_request = IPython . CodeCell . prototype . native_handle_input_request || IPython . CodeCell . prototype . _handle_input_request ;
18- IPython . CodeCell . prototype . _handle_input_request = function ( msg ) {
19- try {
20- // only apply the hack if the command is valid JSON
21- const command = JSON . parse ( msg . content . prompt ) ;
22- const kernel = IPython . notebook . kernel ;
23- if ( command [ "command" ] === "last_cell" ) {
24- kernel . send_input_reply ( JSON . stringify ( getDebugInformation ( ) ) ) ;
25- } else {
26- console . log ( "Not a command" , msg ) ;
15+ window . comlGetCurrentCell = function ( ) {
16+ const cell = comlGetRunningCellIndex ( ) ;
17+ if ( cell === null ) {
18+ return null ;
19+ }
20+ return IPython . notebook . get_cell ( cell ) ;
21+ }
22+
23+ window . comlGetLastCell = function ( ) {
24+ const cellIndex = comlGetRunningCellIndex ( ) ;
25+ if ( cellIndex === null ) {
26+ return null ;
27+ }
28+ return IPython . notebook . get_cell ( comlGetRunningCellIndex ( ) - 1 ) ;
29+ }
30+
31+ if ( window . IPython && IPython . CodeCell ) {
32+ window . IPythonAvailable = true ;
33+ IPython . CodeCell . prototype . native_handle_input_request = IPython . CodeCell . prototype . native_handle_input_request || IPython . CodeCell . prototype . _handle_input_request ;
34+ IPython . CodeCell . prototype . _handle_input_request = function ( msg ) {
35+ try {
36+ // only apply the hack if the command is valid JSON
37+ const command = JSON . parse ( msg . content . prompt ) ;
38+ const kernel = IPython . notebook . kernel ;
39+ if ( command [ "command" ] === "last_cell" ) {
40+ kernel . send_input_reply ( JSON . stringify ( comlGetLastCell ( ) . toJSON ( ) ) ) ;
41+ } else if ( command [ "command" ] === "running_cell" ) {
42+ kernel . send_input_reply ( JSON . stringify ( comlGetCurrentCell ( ) . toJSON ( ) ) ) ;
43+ } else {
44+ console . log ( "Not a command" , msg ) ;
45+ this . native_handle_input_request ( msg ) ;
46+ }
47+ } catch ( err ) {
48+ console . log ( "Not a command" , msg , err ) ;
2749 this . native_handle_input_request ( msg ) ;
2850 }
29- } catch ( err ) {
30- console . log ( "Not a command" , msg , err ) ;
31- this . native_handle_input_request ( msg ) ;
3251 }
33- }
52+ }
0 commit comments