/
index.ts
138 lines (125 loc) · 4.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
LabShell
} from '@jupyterlab/application';
//import {CellList} from '@jupyterlab/notebook'; //gets list from ISharedNotebook
import { Cell } from '@jupyterlab/cells';
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
/**
* The plugin registration information.
*/
// https://jupyterlab.readthedocs.io/en/stable/api/index.html
// https://jupyterlab.readthedocs.io/en/3.3.x/api/interfaces/notebook.inotebooktracker.html
const plugin: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab-examples/toolbar-button:plugin',
description:
'A JupyterLab extension adding a button to the Notebook toolbar.',
requires: [INotebookTracker],
autoStart: true,
activate: (app: JupyterFrontEnd, notebookTracker: INotebookTracker) => {
const { commands } = app;
//labshell via https://discourse.jupyter.org/t/jupyterlab-4-iterating-over-all-cells-in-a-notebook/20033/2
const labShell = app.shell as LabShell;
labShell.currentChanged.connect(() => {
const notebook = app.shell.currentWidget as NotebookPanel;
const cellList = notebook.content.model.cells;
let i=1;
for (const cell of cellList) {
console.log("a cell of type", cell.type, i)
i=i+1;
console.log('METADATA: ', cell.metadata)
}
});
// if we can get a list of cells, then update on render?
// https://jupyterlab.readthedocs.io/en/stable/api/classes/cells.Cell-1.html#ready
notebookTracker.currentChanged.connect((tracker, panel) => {
//console.log(panel);
if (!panel) {
return;
}
console.log("fired on currentChanged")
console.log("this is the panel", panel)
const nb = notebookTracker.currentWidget.content
nb.widgets.forEach((cell: Cell) => {
console.log("try",cell.model.type)
})
console.log("panel widgets length",panel.content.widgets.length)
// Iterate over all cells in the notebook and display metadata for each cell
// NO - this is only length 1?
console.log("tracker widgets length",tracker.currentWidget.content.widgets.length)
panel.content.widgets.forEach(cell=>{
console.log("panel cell is", cell)
if ( cell !== null) {
//this doesn't work wrt metadata
console.log(cell.model.type)
if (cell.model.type === 'code' || cell.model.type === 'markdown') {
//console.log("tags",cell.model?.getMetadata('tags'))
let tagList = cell.model.getMetadata("tags") as string[] ?? [];
console.log("model here is", cell.model);
console.log("metadata tags here is", tagList);
if (tagList?.includes('activity')) {
cell.node.classList.add('iou-activity-node');
}
//console.log(cell.model.type)
//console.log(cell.model.metadata)
//console.log(cell.model.metadata.get('tags'))
//console.log(cell.model.metadata.get('tags').includes('activity'))
//co
}
}
})
});
// TO DO - if the notebook tracker points to current cell
// then we should be able to get the current cell.
const createEmpinkenCommand = (label: string, type: string) => {
//this works wrt metadata
const caption = `Execute empinken ${type} Command`;
return {
label,
caption,
execute: () => {
let activeCell = notebookTracker.activeCell;
console.log(label, type, caption)
console.log(activeCell)
const nodeclass = 'iou-' + type + "-node";
if ( activeCell !== null) {
let tagList = activeCell.model.getMetadata("tags") as string[] ?? [];
console.log("cell metadata was", tagList, "; checking for", type);
if (tagList.includes(type)) {
// ...then remove it
const index = tagList.indexOf(type, 0);
if (index > -1) {
tagList.splice(index, 1);
}
activeCell.model.setMetadata("tags", tagList)
// Remove class
activeCell.node.classList.remove(nodeclass)
// cell.node.classList exists
} else {
tagList.push(type)
activeCell.model.setMetadata("tags", tagList)
activeCell.node.classList.add(nodeclass)
}
console.log("cell metadata now is", tagList);
}
}
};
};
const command_a = 'ouseful-empinken:activity';
const command_l = 'ouseful-empinken:learner';
// Add commands
commands.addCommand(command_a, createEmpinkenCommand('A', 'activity'));
commands.addCommand(command_l, createEmpinkenCommand('L', 'learner'));
// Call the command execution
//commands.execute(command_a, { origin: 'init' }).catch(reason => {
// console.error(
// `An error occurred during the execution of empinken-A.\n${reason}`
// );
//});
}
};
/**
* Export the plugin as default.
*/
export default plugin;