-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
119 lines (102 loc) · 3.12 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
import {
IDisposable, DisposableDelegate
} from '@phosphor/disposable';
import {
JupyterLab, JupyterLabPlugin
} from '@jupyterlab/application';
import {
Clipboard, Dialog, showDialog, ToolbarButton
} from '@jupyterlab/apputils';
import {
DocumentRegistry
} from '@jupyterlab/docregistry';
import {
NotebookPanel, INotebookModel
} from '@jupyterlab/notebook';
import {
getImageSpec, createShareLink
} from './actions';
const INITIAL_NETWORK_RETRY = 2; // ms
/**
* The plugin registration information.
*/
const plugin: JupyterLabPlugin<void> = {
activate,
id: 'my-extension-name:buttonPlugin',
autoStart: true
};
/**
* A notebook widget extension that adds a button to the toolbar.
*/
export
class ButtonExtension implements DocumentRegistry.IWidgetExtension<NotebookPanel, INotebookModel> {
/**
* Create a new extension object.
*/
createNew(panel: NotebookPanel, context: DocumentRegistry.IContext<INotebookModel>): IDisposable {
let callback = () => {
let networkRetry = INITIAL_NETWORK_RETRY;
// NotebookActions.runAll(panel.content, context.session);
const imageSpecPromise = getImageSpec();
imageSpecPromise.then(imageSpec => {
networkRetry = INITIAL_NETWORK_RETRY;
let path = context.path;
// TODO Properly obtain these from JupyterFrontEnd.
const hubHost = '';
const hubPrefix = '/';
const createShareLinkPromise = createShareLink(
hubHost, hubPrefix, path, imageSpec);
createShareLinkPromise.then(shareLink => {
networkRetry = INITIAL_NETWORK_RETRY;
showDialog({
title: "Shareable Link",
body: `For the next hour, any other user on this JupyterHub who has this link will be able to fetch a copy of your latest saved version of ${path}.`,
buttons: [
Dialog.okButton({
label: 'Copy Link',
}),
Dialog.cancelButton({ label: 'Dismiss' }),
]
}).then(result => result.button.accept ?
Clipboard.copyToSystem(shareLink) : null);
});
createShareLinkPromise.catch((reason) => {
if (reason.status === 404) {
console.log('Is the jupyter-share-link hub service running?');
}
setTimeout(() => {
networkRetry *= 2;
}, networkRetry);
});
});
imageSpecPromise.catch((reason) => {
if (reason.status === 404) {
console.log('Is the jupyter_expose_image_spec serverextension running?');
}
setTimeout(() => {
networkRetry *= 2;
}, networkRetry);
});
};
let button = new ToolbarButton({
className: 'createShareLink',
iconClassName: 'fa fa-send',
onClick: callback,
tooltip: 'Create Shareable Link'
});
panel.toolbar.insertItem(0, 'runAll', button);
return new DisposableDelegate(() => {
button.dispose();
});
}
}
/**
* Activate the extension.
*/
function activate(app: JupyterLab) {
app.docRegistry.addWidgetExtension('Notebook', new ButtonExtension());
};
/**
* Export the plugin as default.
*/
export default plugin;