I am able to access a cell model and its corresponding widget.
How can I get the corresponding output model? I would like to know the output expression (“b”) and its corresponding value (“3”). (My aim is to use this information for building a DAG of the “simple” cells, having a single named output.)
As a workaround, I could get the output value “3” from the div element of the widget dom e.g. with
let div = cellWidget.outputArea.node.children[0]
extractOutputValueFromDiv(div)
However, there should be a more elegant way, I guess?
For example, I would expect cellModel.outputs
to include the output information I am looking for, but it seems to be empty (?).
=> What is the recommended way to access the output for a given cell model?
- Related:
https://github.com/jupyter/notebook/issues/1175
https://discourse.jupyter.org/t/dag-based-notebooks/11173
- Part of the code I have so far:
function __observeNotebook(app){
let notebook = __tryToGetNotebook(app);
if(notebook){
let model = notebook.model;
let cellModels = model.cells
cellModels.changed.connect(__cellsChanged, this);
for(let cellIndex=0; cellIndex < cellModels.length; cellIndex++){
let cellModel = cellModels.get(cellIndex);
__observeCell(cellModel, notebook);
}
}
}
function __observeCell(cellModel, notebook){
cellModel.contentChanged.connect(cellModel => __cellContentChanged(cellModel, notebook), this);
cellModel.stateChanged.connect(__cellStateChanged, this);
}
function __cellsChanged(cellModels, change){
console.log("Cells changed:")
console.log("type: " + change.type);
console.log("oldIndex: " + change.oldIndex);
console.log("newIndex: " + change.newIndex);
console.log("oldValues: " + change.oldValues);
console.log("newValues: " + change.newValues);
if(change.type == "add"){
var newCellModel = cellModels.get(change.newIndex);
__observeCell(newCellModel);
}
}
function __cellContentChanged(cellModel, notebook){
let id = cellModel.id
console.log("Content of cell " + id + " changed");
let currentText = cellModel.value.text;
console.log(currentText);
let cellWidget = notebook.widgets.find(widget=>{
return widget.model.id == id;
});
let outputArea = cellWidget.outputArea;
let children = outputArea.node.children;
if(children.length==1){
let output = children[0];
console.log(output);
}
}
function __cellStateChanged(cellModel, change){
let currentText = cellModel.value.text;
console.log("State of cell " + cellModel.id + " changed:");
console.log("name: " + change.name);
console.log("old value: " + change.oldValue);
console.log("new value: " + change.newValue);
}
function __tryToGetNotebookCell(app){
var notebook = __tryToGetNotebook(app);
return notebook
?notebook.activeCell
:null;
}
function __tryToGetNotebook(app){
var notebookPanel = __getFirstVisibleNotebookPanel(app);
return notebookPanel
?notebookPanel.content
:null;
}
function __getFirstVisibleNotebookPanel(app){
var mainWidgets = app.shell.widgets('main');
var widget = mainWidgets.next();
while(widget){
var type = widget.sessionContext.type;
if(type == 'notebook'){ //other wigets might be of type DocumentWidget
if (widget.isVisible){
return widget;
}
}
widget = mainWidgets.next();
}
return null;
}