如何从JupyterLab扩展激活方法访问笔记本?

时间:2020-10-30 14:57:14

标签: javascript jupyter jupyter-lab

前一段时间,我为JupyterNotebook写了一个扩展名。现在,我想将其修改为JupyterLab。

为了访问我使用的当前打开的笔记本:

var notebook = Jupyter.notebook;
var firstCell = notebook.get_cells()[0];

=> Jupyterlab的对应代码是什么?

module.exports = [{
    id: 'jupyterlab_workspace_module',
    autoStart: true,
    activate: function(app) {
       //app.notebook is not defined
       var notebook = getNotebook(app); // <= how to implement this?
    }
}];

1 个答案:

答案 0 :(得分:0)

var notebookPanel = __getFirstVisibleNotebookPanel(app);
var notebook = notebookPanel.content;
var cell = notebook.activeCell;

function __getFirstVisibleNotebookPanel(app){
    var mainWidgets = app.shell.widgets('main');
    var widget = mainWidgets.next();
    while(widget){
        var type = widget.constructor.name;
        if(type == 'NotebookPanel'){  //other wigets might be of type DocumentWidget
            if (widget.isVisible){
                return widget;
            }
        }
        widget = mainWidgets.next();
    }
    return null;
}