Skip to content

Commit 6657f92

Browse files
author
minjk-bl
committed
Edit jupyterlab scroll issue
1 parent 5e51bcf commit 6657f92

File tree

3 files changed

+170
-36
lines changed

3 files changed

+170
-36
lines changed

visualpython/js/com/com_Kernel.js

Lines changed: 163 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,30 +250,12 @@ define([
250250
var kernelConnection = that.getLabKernel();
251251
if (kernelConnection) {
252252
var future = kernelConnection.requestExecute(codeObj);
253-
future.onIOPub = (msg) => {
253+
var onIOPub = function(msg) {
254254
const msgType = msg.header.msg_type;
255255
switch(msgType) {
256256
case 'status':
257-
// if(!isExpectingOutput){
258-
// if(msg.content.execution_state === 'idle'){
259-
// resolve();
260-
// }
261-
// }
262257
return;
263258
case 'execute_input':
264-
// var content = msg.content;
265-
// resolve({
266-
// result: content,
267-
// type: type,
268-
// msg: {
269-
// content: {
270-
// name: type,
271-
// data: {
272-
// [type]: content
273-
// }
274-
// }
275-
// }
276-
// });
277259
return;
278260
case 'stream':
279261
var content = msg.content;
@@ -303,10 +285,24 @@ define([
303285
reject({status: 'error', ename: 'Unknown stream type', evalue: message});
304286
}
305287
break;
306-
case 'error':
307-
//stderr does not yield output for all errors
308-
// var message = msg.content.ename + '\n' + msg.content.evalue;
309-
reject({status: 'error', ename: msg.content.ename, evalue: msg.content.evalue});
288+
case 'error':
289+
// check if it has a problem on restarting vp inner function
290+
// ex) "name '_vp_print' is not defined"
291+
if (msg.content.ename === 'NameError'
292+
&& msg.content.evalue.includes('_vp_')
293+
&& msg.content.evalue.includes('is not defined')) {
294+
// restart vp
295+
vpConfig.readKernelFunction().then(function() {
296+
// re-try executing code
297+
vpLog.display(VP_LOG_TYPE.LOG, 'Re-try executing code after restarting vp...');
298+
var future2 = kernelConnection.requestExecute(codeObj);
299+
future2.onIOPub = onIOPub;
300+
}).catch(function(err) {
301+
reject({status: 'error', ename: msg.content.ename, evalue: msg.content.evalue});
302+
});
303+
} else {
304+
reject({status: 'error', ename: msg.content.ename, evalue: msg.content.evalue});
305+
}
310306
break;
311307
case 'execute_result':
312308
var type = 'text';
@@ -383,7 +379,150 @@ define([
383379

384380
};
385381
return;
386-
};
382+
}
383+
future.onIOPub = onIOPub;
384+
// future.onIOPub = (msg) => {
385+
// const msgType = msg.header.msg_type;
386+
// switch(msgType) {
387+
// case 'status':
388+
// // if(!isExpectingOutput){
389+
// // if(msg.content.execution_state === 'idle'){
390+
// // resolve();
391+
// // }
392+
// // }
393+
// return;
394+
// case 'execute_input':
395+
// // var content = msg.content;
396+
// // resolve({
397+
// // result: content,
398+
// // type: type,
399+
// // msg: {
400+
// // content: {
401+
// // name: type,
402+
// // data: {
403+
// // [type]: content
404+
// // }
405+
// // }
406+
// // }
407+
// // });
408+
// return;
409+
// case 'stream':
410+
// var content = msg.content;
411+
// var type = content.name;
412+
// switch(type){
413+
// case 'stdout':
414+
// var message = content.text;
415+
// resolve({
416+
// result: message,
417+
// type: type,
418+
// msg: {
419+
// content: {
420+
// name: type,
421+
// data: {
422+
// [type]: message
423+
// }
424+
// }
425+
// }
426+
// });
427+
// break;
428+
// case 'stderr':
429+
// var message = content.text;
430+
// reject({status: 'stderr', ename: 'stderr', evalue: message});
431+
// break;
432+
// default:
433+
// var message = '[jupyterLabTerminal]: Unknown stream type ' + type;
434+
// reject({status: 'error', ename: 'Unknown stream type', evalue: message});
435+
// }
436+
// break;
437+
// case 'error':
438+
// //stderr does not yield output for all errors
439+
// // var message = msg.content.ename + '\n' + msg.content.evalue;
440+
// // check if it has a problem on restarting vp inner function
441+
// // ex) "name '_vp_print' is not defined"
442+
// if (msg.content.ename === 'NameError'
443+
// && msg.content.evalue.includes('_vp_')
444+
// && msg.content.evalue.includes('is not defined')) {
445+
// // restart vp
446+
// vpConfig.readKernelFunction();
447+
// }
448+
// reject({status: 'error', ename: msg.content.ename, evalue: msg.content.evalue});
449+
// break;
450+
// case 'execute_result':
451+
// var type = 'text';
452+
// if (msg.content) {
453+
// try {
454+
// if (msg.content['text']) {
455+
// result = String(msg.content['text']);
456+
// type = 'text';
457+
// } else if (msg.content.data) {
458+
// if (msg.content.data['image/png']) {
459+
// result = String(msg.content.data['image/png']);
460+
// type = 'image/png';
461+
// } else if (msg.content.data['text/plain']) {
462+
// result = String(msg.content.data['text/plain']);
463+
// type = 'text/plain';
464+
// } else if (msg.content.data['text/html']) {
465+
// result = String(msg.content.data['text/html']);
466+
// type = 'text/html';
467+
// }
468+
// }
469+
// resolve({result: result, type: type, msg: msg});
470+
// } catch(ex) {
471+
// reject(ex);
472+
// }
473+
// } else {
474+
// resolve({result: result, type: type, msg: msg});
475+
// }
476+
// break;
477+
// case 'display_data':
478+
// var type = 'text';
479+
// if (msg.content) {
480+
// try {
481+
// if (msg.content['text']) {
482+
// result = String(msg.content['text']);
483+
// type = 'text';
484+
// } else if (msg.content.data) {
485+
// if (msg.content.data['image/png']) {
486+
// result = String(msg.content.data['image/png']);
487+
// type = 'image/png';
488+
// } else if (msg.content.data['text/plain']) {
489+
// result = String(msg.content.data['text/plain']);
490+
// type = 'text/plain';
491+
// } else if (msg.content.data['text/html']) {
492+
// result = String(msg.content.data['text/html']);
493+
// type = 'text/html';
494+
// }
495+
// }
496+
// resolve({result: result, type: type, msg: msg});
497+
// } catch(ex) {
498+
// reject(ex);
499+
// }
500+
// } else {
501+
// resolve({result: result, type: type, msg: msg});
502+
// }
503+
// break;
504+
// case 'update_display_data':
505+
// var result = msg.content;
506+
// resolve({
507+
// result: result,
508+
// type: msgType,
509+
// msg: {
510+
// content: {
511+
// name: msgType,
512+
// data: {
513+
// [msgType]: result
514+
// }
515+
// }
516+
// }
517+
// });
518+
// break;
519+
// default:
520+
// var message = '[jupyterLabTerminal]: Unknown message type ' + msgType;
521+
// reject({status: 'error', ename: 'Unknown message type', evalue: message});
522+
523+
// };
524+
// return;
525+
// };
387526
}
388527

389528
}

visualpython/js/com/com_interface.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define([
3232
* @param {boolean} exec true(default) / false
3333
* @param {int} sigNum
3434
*/
35-
var insertCell = function(type, command, exec=true, sigText='') {
35+
var insertCell = async function(type, command, exec=true, sigText='') {
3636
// Add signature
3737
if (type == 'code') {
3838
if (sigText !== '') {
@@ -106,6 +106,7 @@ define([
106106
// CHROME: TODO:
107107
} else if (vpConfig.extensionType === 'lab') {
108108
var { NotebookActions } = require('@jupyterlab/notebook');
109+
var { signalToPromise } = require('@jupyterlab/coreutils');
109110
var notebookPanel = vpKernel.getLabNotebookPanel();
110111
if (notebookPanel && notebookPanel.sessionContext){
111112
var sessionContext = notebookPanel.sessionContext;
@@ -122,7 +123,6 @@ define([
122123
notebookModel.cells.insert(newCellIndex, cellModel);
123124
notebook.activeCellIndex = newCellIndex;
124125

125-
var cell = notebook.activeCell;
126126
if (exec == true) {
127127
try{
128128
NotebookActions.run(notebook, sessionContext);
@@ -131,10 +131,7 @@ define([
131131
}
132132
}
133133
// move to executed cell
134-
let activeCell = notebookPanel.content.activeCell;
135-
let activeCellTop = $(activeCell.node).position().top;
136-
// scroll to current cell top
137-
$(notebookPanel.layout.widgets[2].node).animate({scrollTop: activeCellTop},"fast");
134+
$(vpKernel.getLabNotebookPanel().content.activeCell.node)[0].scrollIntoView(true);
138135
} else if (sessionType === 'console') {
139136
var labConsole = notebookPanel.content;
140137
var widget = labConsole.widgets[0];
@@ -156,7 +153,6 @@ define([
156153
com_util.renderAlertModal('Visual Python only supports Notebook and Console type. Please use appropriate type of file to use it.');
157154
}
158155
}
159-
160156
com_util.renderSuccessMessage('Your code is successfully generated.');
161157
}
162158

@@ -268,7 +264,6 @@ define([
268264
var console = notebookPanel.content;
269265
var cellModel = console.contentFactory.createCell(type, {});
270266
cellModel.value.text = command;
271-
272267
}
273268
} else {
274269
// No session found
@@ -287,8 +282,8 @@ define([
287282
// LAB: TODO:
288283
let activeCell = notebookPanel.content.activeCell;
289284
let activeCellTop = $(activeCell.node).position().top;
290-
// scroll to current cell top
291-
$(notebookPanel.layout.widgets[2].node).animate({scrollTop: activeCellTop},"fast");
285+
// scroll to current cell
286+
$(vpKernel.getLabNotebookPanel().content.activeCell.node)[0].scrollIntoView(true);
292287
}
293288

294289
com_util.renderSuccessMessage('Your code is successfully generated.');

visualpython/python/pandasCommand.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Pandas Objects Command
33
"""
44
import pandas as _vp_pd
5-
import IPython
5+
import IPython as _vp_ipython
66
# LAB: prevent import error
7-
_ipython_version = IPython.version_info
7+
_ipython_version = _vp_ipython.version_info
88
if _ipython_version[0] < 7 or ( _ipython_version[0] == 7 and _ipython_version[1] <= 13 ):
99
from IPython.core.display import display
1010
else:

0 commit comments

Comments
 (0)