-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathciteExtension.ts
71 lines (64 loc) · 2.16 KB
/
citeExtension.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
import { IDisposable, DisposableDelegate } from '@lumino/disposable';
import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
} from '@jupyterlab/application';
import { ToolbarButton } from '@jupyterlab/apputils';
import { DocumentRegistry } from '@jupyterlab/docregistry';
import {
NotebookPanel,
INotebookModel,
} from '@jupyterlab/notebook';
function insertAtCursor(panel: NotebookPanel, text: string) {
console.log(panel)
const activeCell = panel.content.activeCell;
if (activeCell) {
const cursor = activeCell.editor.getCursorPosition();
const offset = activeCell.editor.getOffsetAt(cursor);
const editor = activeCell.editor;
activeCell.model.value.insert(offset, text);
const updatedPosition = editor.getPositionAt(offset + text.length);
if (updatedPosition) {
editor.setCursorPosition(updatedPosition);
}
}
}
export class ButtonExtension implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel>
{
createNew(
panel: NotebookPanel,
context: DocumentRegistry.IContext<INotebookModel>
): IDisposable {
const clearOutput = () => {
// task: use input dialog to get the query from user
// use https://jupyterlab.readthedocs.io/en/stable/api/index.html
const query = 'NCT04001244';
(
fetch(`https://clinicaltrials.gov/api/query/full_studies?expr=${query}&fmt=JSON`)
.then(response => response.json())
.then(data => {
console.log(data['FullStudiesResponse']['FullStudies'][0]['Study'])
insertAtCursor(panel, JSON.stringify(data['FullStudiesResponse']['FullStudies'][0]['Study']))
})
)
};
const button = new ToolbarButton({
className: 'cite-button',
label: 'Cite study',
onClick: clearOutput,
tooltip: 'Cite a study',
});
panel.toolbar.insertItem(10, 'clearOutputs', button);
return new DisposableDelegate(() => {
button.dispose();
});
}
}
const plugin: JupyterFrontEndPlugin<void> = {
id: 'hello-world:plugin',
autoStart: true,
activate: (app: JupyterFrontEnd) => {
app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());
},
};
export default plugin;