jupyterlab-pioneer 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2023, Educational Technology Collective
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # JupyterLab Pioneer
2
+
3
+ [![PyPI](https://img.shields.io/pypi/v/jupyterlab-pioneer.svg)](https://pypi.org/project/jupyterlab-pioneer)
4
+ [![npm](https://img.shields.io/npm/v/jupyterlab-pioneer.svg)](https://www.npmjs.com/package/jupyterlab-pioneer)
5
+
6
+ A JupyterLab extension for generating and exporting JupyterLab event telemetry data.
7
+
8
+ ## Get started
9
+
10
+ ### Run the extension with docker compose
11
+ ```bash
12
+ # enter the configuration_examples directory and run
13
+ docker compose -p jupyterlab-pioneer up --build
14
+ ```
15
+ A JupyterLab application with the extension installed and configured will run on localhost:8888.
16
+
17
+ ### Or install the extension and configure it manually
18
+
19
+ To install the extension, execute:
20
+
21
+ ```bash
22
+ pip install jupyterlab-pioneer
23
+ ```
24
+ Before starting Jupyter Lab, users need to write their own configuration files (or use the provided configuration examples) and **place them in the correct directory**.
25
+
26
+ Examples of configurations are [here](#configurations).
27
+
28
+ ## Configurations
29
+ ### Overview
30
+ The configuration file controls the activated events and data exporters.
31
+
32
+ To add a data exporter, users should assign a callable function along with function arguments when configuring `exporters`. This extension provides 4 [default exporters](https://github.com/educational-technology-collective/jupyterlab-pioneer/blob/b7eda5f4b286c7b0f7aa50df716b2795f180cc6e/jupyterlab_pioneer/handlers.py#L9), `command_line_exporter`, `console_exporter`, `file_exporter` and `remote_exporter`. Users can import default exporters or write customized exporters in the configuration file.
33
+
34
+ ### Configuration file name & path
35
+
36
+ Jupyter Server expects the configuration file to be named after the extension’s name like so: **`jupyter_{extension name defined in application.py}_config.py`**. So, the configuration file name for this extension is `jupyter_jupyterlab_pioneer_config.py`.
37
+
38
+ Jupyter Server looks for an extension’s config file in a set of specific paths. **The configuration file should be saved into one of the config directories provided by `jupyter --path`.**
39
+
40
+ Check jupyter server [doc](https://jupyter-server.readthedocs.io/en/latest/operators/configuring-extensions.html) for more details.
41
+ ### Syntax
42
+
43
+ `activateEvents`: An array of the ids of the events. Only valid events (1. has an id associated with the event class, and 2. the event id is included in `activatedEvents`) will be activated.
44
+
45
+ `logNotebookContentEvents`: An array of the ids of the events. The extension will export the entire notebook content only for valid events (1. has an id associated with the event class, and 2. the event id is included in `logNotebookContentEvents`).
46
+
47
+ `exporters`: An array of exporters. Each exporter should have the following structure:
48
+ ```python
49
+ {
50
+ exporter: # a callable exporter function. Need to contain 'path' for file_exporter, 'url' for remote_exporter.
51
+ args: # arguments passed to the exporter function
52
+ }
53
+ ```
54
+ **The configuration file should be saved into one of the config directories provided by `jupyter --path`.**
55
+
56
+ ### Example
57
+ [jupyter_jupyterlab_pioneer_config.py](https://github.com/educational-technology-collective/jupyterlab-pioneer/blob/main/configuration_examples/jupyter_jupyterlab_pioneer_config.py)
58
+
59
+ ## Uninstall
60
+
61
+ To remove the extension, execute:
62
+
63
+ ```bash
64
+ pip uninstall jupyterlab_pioneer
65
+ ```
66
+
67
+ ## Troubleshoot
68
+
69
+ If you are seeing the frontend extension, but it is not working, check
70
+ that the server extension is enabled:
71
+
72
+ ```bash
73
+ jupyter server extension list
74
+ ```
75
+
76
+ If the server extension is installed and enabled, but you are not seeing
77
+ the frontend extension, check the frontend extension is installed:
78
+
79
+ ```bash
80
+ jupyter labextension list
81
+ ```
82
+
83
+ ## Contributing
84
+
85
+ ### Development install
86
+
87
+ Note: You will need NodeJS to build the extension package.
88
+
89
+ The `jlpm` command is JupyterLab's pinned version of
90
+ [yarn](https://yarnpkg.com/) that is installed with JupyterLab. You may use
91
+ `yarn` or `npm` in lieu of `jlpm` below.
92
+
93
+ ```bash
94
+ # Clone the repo to your local environment
95
+ # Change directory to the jupyterlab_pioneer directory
96
+ # Install package in development mode
97
+ pip install -e "."
98
+ # Link your development version of the extension with JupyterLab
99
+ jupyter labextension develop . --overwrite
100
+ # Server extension must be manually installed in develop mode
101
+ jupyter server extension enable jupyterlab_pioneer
102
+ # Rebuild extension Typescript source after making changes
103
+ jlpm build
104
+ ```
105
+
106
+ You can watch the source directory and run JupyterLab at the same time in different terminals to watch for changes in the extension's source and automatically rebuild the extension.
107
+
108
+ ```bash
109
+ # Watch the source directory in one terminal, automatically rebuilding when needed
110
+ jlpm watch
111
+ # Run JupyterLab in another terminal
112
+ jupyter lab
113
+ ```
114
+
115
+ With the watch command running, every saved change will immediately be built locally and available in your running JupyterLab. Refresh JupyterLab to load the change in your browser (you may need to wait several seconds for the extension to be rebuilt).
116
+
117
+ By default, the `jlpm build` command generates the source maps for this extension to make it easier to debug using the browser dev tools. To also generate source maps for the JupyterLab core extensions, you can run the following command:
118
+
119
+ ```bash
120
+ jupyter lab build --minimize=False
121
+ ```
122
+
123
+ ### Development uninstall
124
+
125
+ ```bash
126
+ # Server extension must be manually disabled in develop mode
127
+ jupyter server extension disable jupyterlab_pioneer
128
+ pip uninstall jupyterlab_pioneer
129
+ ```
130
+
131
+ In development mode, you will also need to remove the symlink created by `jupyter labextension develop`
132
+ command. To find its location, you can run `jupyter labextension list` to figure out where the `labextensions`
133
+ folder is located. Then you can remove the symlink named `jupyterlab-pioneer` within that folder.
134
+
135
+ ### Packaging the extension
136
+
137
+ See [RELEASE](RELEASE.md)
@@ -0,0 +1,53 @@
1
+ import { NotebookPanel } from '@jupyterlab/notebook';
2
+ import { ITelemetryRouter } from './router';
3
+ export declare class NotebookOpenEventProducer {
4
+ static id: string;
5
+ private produced;
6
+ listen(_: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): Promise<void>;
7
+ }
8
+ export declare class NotebookScrollProducer {
9
+ static id: string;
10
+ private timeout;
11
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
12
+ }
13
+ export declare class NotebookVisibleEventProducer {
14
+ static id: string;
15
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
16
+ }
17
+ export declare class NotebookHiddenEventProducer {
18
+ static id: string;
19
+ listen(_: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
20
+ }
21
+ export declare class ClipboardCopyEventProducer {
22
+ static id: string;
23
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
24
+ }
25
+ export declare class ClipboardCutEventProducer {
26
+ static id: string;
27
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
28
+ }
29
+ export declare class ClipboardPasteEventProducer {
30
+ static id: string;
31
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
32
+ }
33
+ export declare class ActiveCellChangeEventProducer {
34
+ static id: string;
35
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
36
+ }
37
+ export declare class NotebookSaveEventProducer {
38
+ static id: string;
39
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
40
+ }
41
+ export declare class CellExecuteEventProducer {
42
+ static id: string;
43
+ listen(_: any, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
44
+ }
45
+ export declare class CellAddEventProducer {
46
+ static id: string;
47
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
48
+ }
49
+ export declare class CellRemoveEventProducer {
50
+ static id: string;
51
+ listen(notebookPanel: NotebookPanel, router: ITelemetryRouter, logNotebookContentEvent: boolean): void;
52
+ }
53
+ export declare const producerCollection: (typeof NotebookVisibleEventProducer)[];
package/lib/events.js ADDED
@@ -0,0 +1,283 @@
1
+ import { NotebookActions } from '@jupyterlab/notebook';
2
+ import { requestAPI } from './handler';
3
+ class NotebookOpenEventProducer {
4
+ constructor() {
5
+ this.produced = false;
6
+ }
7
+ async listen(_, router, logNotebookContentEvent) {
8
+ if (!this.produced) {
9
+ const event = {
10
+ eventName: NotebookOpenEventProducer.id,
11
+ eventTime: Date.now(),
12
+ eventInfo: {
13
+ environ: await requestAPI('environ')
14
+ }
15
+ };
16
+ await router.publishEvent(event, logNotebookContentEvent);
17
+ this.produced = true;
18
+ }
19
+ }
20
+ }
21
+ NotebookOpenEventProducer.id = 'NotebookOpenEvent';
22
+ export { NotebookOpenEventProducer };
23
+ const getVisibleCells = (notebookPanel) => {
24
+ const visibleCells = [];
25
+ for (let index = 0; index < notebookPanel.content.widgets.length; index++) {
26
+ const cell = notebookPanel.content.widgets[index];
27
+ const cellTop = cell.node.offsetTop;
28
+ const cellBottom = cell.node.offsetTop + cell.node.offsetHeight;
29
+ const viewTop = notebookPanel.content.node.scrollTop;
30
+ const viewBottom = notebookPanel.content.node.scrollTop +
31
+ notebookPanel.content.node.clientHeight;
32
+ if (cellTop <= viewBottom && cellBottom >= viewTop) {
33
+ visibleCells.push({
34
+ id: cell.model.id,
35
+ index: index
36
+ });
37
+ }
38
+ }
39
+ return visibleCells;
40
+ };
41
+ class NotebookScrollProducer {
42
+ constructor() {
43
+ this.timeout = 0;
44
+ }
45
+ listen(notebookPanel, router, logNotebookContentEvent) {
46
+ notebookPanel.content.node.addEventListener('scroll', async (e) => {
47
+ e.stopPropagation();
48
+ clearTimeout(this.timeout);
49
+ await new Promise(resolve => (this.timeout = window.setTimeout(resolve, 1500))); // wait 1.5 seconds before preceding
50
+ const event = {
51
+ eventName: NotebookScrollProducer.id,
52
+ eventTime: Date.now(),
53
+ eventInfo: {
54
+ cells: getVisibleCells(notebookPanel)
55
+ }
56
+ };
57
+ await router.publishEvent(event, logNotebookContentEvent);
58
+ });
59
+ }
60
+ }
61
+ NotebookScrollProducer.id = 'NotebookScrollEvent';
62
+ export { NotebookScrollProducer };
63
+ class NotebookVisibleEventProducer {
64
+ listen(notebookPanel, router, logNotebookContentEvent) {
65
+ document.addEventListener('visibilitychange', async () => {
66
+ if (document.visibilityState === 'visible') {
67
+ const event = {
68
+ eventName: NotebookVisibleEventProducer.id,
69
+ eventTime: Date.now(),
70
+ eventInfo: {
71
+ cells: getVisibleCells(notebookPanel)
72
+ }
73
+ };
74
+ await router.publishEvent(event, logNotebookContentEvent);
75
+ }
76
+ });
77
+ }
78
+ }
79
+ NotebookVisibleEventProducer.id = 'NotebookVisibleEvent';
80
+ export { NotebookVisibleEventProducer };
81
+ class NotebookHiddenEventProducer {
82
+ listen(_, router, logNotebookContentEvent) {
83
+ document.addEventListener('visibilitychange', async (e) => {
84
+ if (document.visibilityState === 'hidden') {
85
+ const event = {
86
+ eventName: NotebookHiddenEventProducer.id,
87
+ eventTime: Date.now()
88
+ };
89
+ await router.publishEvent(event, logNotebookContentEvent);
90
+ }
91
+ });
92
+ }
93
+ }
94
+ NotebookHiddenEventProducer.id = 'NotebookHiddenEvent';
95
+ export { NotebookHiddenEventProducer };
96
+ class ClipboardCopyEventProducer {
97
+ listen(notebookPanel, router, logNotebookContentEvent) {
98
+ notebookPanel.node.addEventListener('copy', async () => {
99
+ var _a, _b;
100
+ const cell = {
101
+ id: (_a = notebookPanel.content.activeCell) === null || _a === void 0 ? void 0 : _a.model.id,
102
+ index: notebookPanel.content.widgets.findIndex(value => value === notebookPanel.content.activeCell)
103
+ };
104
+ const text = (_b = document.getSelection()) === null || _b === void 0 ? void 0 : _b.toString();
105
+ const event = {
106
+ eventName: ClipboardCopyEventProducer.id,
107
+ eventTime: Date.now(),
108
+ eventInfo: {
109
+ cells: [cell],
110
+ selection: text
111
+ }
112
+ };
113
+ await router.publishEvent(event, logNotebookContentEvent);
114
+ });
115
+ }
116
+ }
117
+ ClipboardCopyEventProducer.id = 'ClipboardCopyEvent';
118
+ export { ClipboardCopyEventProducer };
119
+ class ClipboardCutEventProducer {
120
+ listen(notebookPanel, router, logNotebookContentEvent) {
121
+ notebookPanel.node.addEventListener('cut', async () => {
122
+ var _a, _b;
123
+ const cell = {
124
+ id: (_a = notebookPanel.content.activeCell) === null || _a === void 0 ? void 0 : _a.model.id,
125
+ index: notebookPanel.content.widgets.findIndex(value => value === notebookPanel.content.activeCell)
126
+ };
127
+ const text = (_b = document.getSelection()) === null || _b === void 0 ? void 0 : _b.toString();
128
+ const event = {
129
+ eventName: ClipboardCutEventProducer.id,
130
+ eventTime: Date.now(),
131
+ eventInfo: {
132
+ cells: [cell],
133
+ selection: text
134
+ }
135
+ };
136
+ await router.publishEvent(event, logNotebookContentEvent);
137
+ });
138
+ }
139
+ }
140
+ ClipboardCutEventProducer.id = 'ClipboardCutEvent';
141
+ export { ClipboardCutEventProducer };
142
+ class ClipboardPasteEventProducer {
143
+ listen(notebookPanel, router, logNotebookContentEvent) {
144
+ notebookPanel.node.addEventListener('paste', async (e) => {
145
+ var _a;
146
+ const cell = {
147
+ id: (_a = notebookPanel.content.activeCell) === null || _a === void 0 ? void 0 : _a.model.id,
148
+ index: notebookPanel.content.widgets.findIndex(value => value === notebookPanel.content.activeCell)
149
+ };
150
+ const text = (e.clipboardData || window.clipboardData).getData('text');
151
+ const event = {
152
+ eventName: ClipboardPasteEventProducer.id,
153
+ eventTime: Date.now(),
154
+ eventInfo: {
155
+ cells: [cell],
156
+ selection: text
157
+ }
158
+ };
159
+ await router.publishEvent(event, logNotebookContentEvent);
160
+ });
161
+ }
162
+ }
163
+ ClipboardPasteEventProducer.id = 'ClipboardPasteEvent';
164
+ export { ClipboardPasteEventProducer };
165
+ class ActiveCellChangeEventProducer {
166
+ listen(notebookPanel, router, logNotebookContentEvent) {
167
+ notebookPanel.content.activeCellChanged.connect(async (_, cell) => {
168
+ if (cell && notebookPanel.content.widgets) {
169
+ const activatedCell = {
170
+ id: cell === null || cell === void 0 ? void 0 : cell.model.id,
171
+ index: notebookPanel.content.widgets.findIndex(value => value === cell)
172
+ };
173
+ const event = {
174
+ eventName: ActiveCellChangeEventProducer.id,
175
+ eventTime: Date.now(),
176
+ eventInfo: {
177
+ cells: [activatedCell] // activated cell
178
+ }
179
+ };
180
+ await router.publishEvent(event, logNotebookContentEvent);
181
+ }
182
+ });
183
+ }
184
+ }
185
+ ActiveCellChangeEventProducer.id = 'ActiveCellChangeEvent';
186
+ export { ActiveCellChangeEventProducer };
187
+ class NotebookSaveEventProducer {
188
+ listen(notebookPanel, router, logNotebookContentEvent) {
189
+ notebookPanel.context.saveState.connect(async (_, saveState) => {
190
+ if (saveState.match('completed')) {
191
+ const event = {
192
+ eventName: NotebookSaveEventProducer.id,
193
+ eventTime: Date.now()
194
+ };
195
+ await router.publishEvent(event, logNotebookContentEvent);
196
+ }
197
+ });
198
+ }
199
+ }
200
+ NotebookSaveEventProducer.id = 'NotebookSaveEvent';
201
+ export { NotebookSaveEventProducer };
202
+ class CellExecuteEventProducer {
203
+ listen(_, router, logNotebookContentEvent) {
204
+ NotebookActions.executed.connect(async (_, args) => {
205
+ const executedCell = {
206
+ id: args.cell.model.id,
207
+ index: args.notebook.widgets.findIndex(value => value == args.cell)
208
+ };
209
+ const event = {
210
+ eventName: CellExecuteEventProducer.id,
211
+ eventTime: Date.now(),
212
+ eventInfo: {
213
+ cells: [executedCell],
214
+ success: args.success,
215
+ kernelError: args.success ? null : args.error
216
+ }
217
+ };
218
+ await router.publishEvent(event, logNotebookContentEvent);
219
+ });
220
+ }
221
+ }
222
+ CellExecuteEventProducer.id = 'CellExecuteEvent';
223
+ export { CellExecuteEventProducer };
224
+ class CellAddEventProducer {
225
+ listen(notebookPanel, router, logNotebookContentEvent) {
226
+ var _a;
227
+ (_a = notebookPanel.content.model) === null || _a === void 0 ? void 0 : _a.cells.changed.connect(async (_, args) => {
228
+ if (args.type === 'add') {
229
+ const addedCell = {
230
+ id: args.newValues[0].id,
231
+ index: args.newIndex
232
+ };
233
+ const event = {
234
+ eventName: CellAddEventProducer.id,
235
+ eventTime: Date.now(),
236
+ eventInfo: {
237
+ cells: [addedCell]
238
+ }
239
+ };
240
+ await router.publishEvent(event, logNotebookContentEvent);
241
+ }
242
+ });
243
+ }
244
+ }
245
+ CellAddEventProducer.id = 'CellAddEvent';
246
+ export { CellAddEventProducer };
247
+ class CellRemoveEventProducer {
248
+ listen(notebookPanel, router, logNotebookContentEvent) {
249
+ var _a;
250
+ (_a = notebookPanel.content.model) === null || _a === void 0 ? void 0 : _a.cells.changed.connect(async (_, args) => {
251
+ if (args.type === 'remove') {
252
+ const removedCell = {
253
+ newIndex: args.newIndex,
254
+ oldIndex: args.oldIndex
255
+ };
256
+ const event = {
257
+ eventName: CellRemoveEventProducer.id,
258
+ eventTime: Date.now(),
259
+ eventInfo: {
260
+ cells: [removedCell]
261
+ }
262
+ };
263
+ await router.publishEvent(event, logNotebookContentEvent);
264
+ }
265
+ });
266
+ }
267
+ }
268
+ CellRemoveEventProducer.id = 'CellRemoveEvent';
269
+ export { CellRemoveEventProducer };
270
+ export const producerCollection = [
271
+ NotebookOpenEventProducer,
272
+ NotebookScrollProducer,
273
+ NotebookVisibleEventProducer,
274
+ NotebookHiddenEventProducer,
275
+ ClipboardCopyEventProducer,
276
+ ClipboardCutEventProducer,
277
+ ClipboardPasteEventProducer,
278
+ ActiveCellChangeEventProducer,
279
+ NotebookSaveEventProducer,
280
+ CellExecuteEventProducer,
281
+ CellAddEventProducer,
282
+ CellRemoveEventProducer
283
+ ];
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Call the API extension
3
+ *
4
+ * @param endPoint API REST end point for the extension
5
+ * @param init Initial values for the request
6
+ * @returns The response body interpreted as JSON
7
+ */
8
+ export declare function requestAPI<T>(endPoint?: string, init?: RequestInit): Promise<T>;
package/lib/handler.js ADDED
@@ -0,0 +1,35 @@
1
+ import { URLExt } from '@jupyterlab/coreutils';
2
+ import { ServerConnection } from '@jupyterlab/services';
3
+ /**
4
+ * Call the API extension
5
+ *
6
+ * @param endPoint API REST end point for the extension
7
+ * @param init Initial values for the request
8
+ * @returns The response body interpreted as JSON
9
+ */
10
+ export async function requestAPI(endPoint = '', init = {}) {
11
+ // Make request to Jupyter API
12
+ const settings = ServerConnection.makeSettings();
13
+ const requestUrl = URLExt.join(settings.baseUrl, 'jupyterlab-pioneer', // API Namespace
14
+ endPoint);
15
+ let response;
16
+ try {
17
+ response = await ServerConnection.makeRequest(requestUrl, init, settings);
18
+ }
19
+ catch (error) {
20
+ throw new ServerConnection.NetworkError(error);
21
+ }
22
+ let data = await response.text();
23
+ if (data.length > 0) {
24
+ try {
25
+ data = JSON.parse(data);
26
+ }
27
+ catch (error) {
28
+ console.log('Not a JSON response body.', response);
29
+ }
30
+ }
31
+ if (!response.ok) {
32
+ throw new ServerConnection.ResponseError(response, data.message || data);
33
+ }
34
+ return data;
35
+ }
package/lib/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { JupyterFrontEndPlugin } from '@jupyterlab/application';
2
+ declare const plugin: JupyterFrontEndPlugin<void>;
3
+ export default plugin;
package/lib/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import { INotebookTracker } from '@jupyterlab/notebook';
2
+ import { TelemetryRouter } from './router';
3
+ import { requestAPI } from './handler';
4
+ import { producerCollection } from './events';
5
+ const PLUGIN_ID = 'jupyterlab-pioneer:plugin';
6
+ const plugin = {
7
+ id: PLUGIN_ID,
8
+ autoStart: true,
9
+ requires: [INotebookTracker],
10
+ activate: async (app, notebookTracker) => {
11
+ const version = await requestAPI('version');
12
+ console.log(`${PLUGIN_ID}: ${version}`);
13
+ const config = await requestAPI('config');
14
+ const telemetryRouter = new TelemetryRouter();
15
+ notebookTracker.widgetAdded.connect(async (_, notebookPanel) => {
16
+ await notebookPanel.sessionContext.ready; // wait until session id is created
17
+ await telemetryRouter.loadNotebookPanel(notebookPanel);
18
+ producerCollection.forEach(producer => {
19
+ if (config.activeEvents.includes(producer.id)) {
20
+ new producer().listen(notebookPanel, telemetryRouter, config.logNotebookContentEvents.includes(producer.id));
21
+ }
22
+ });
23
+ });
24
+ }
25
+ };
26
+ export default plugin;
@@ -0,0 +1,22 @@
1
+ import { NotebookPanel } from '@jupyterlab/notebook';
2
+ export interface ITelemetryRouter {
3
+ loadNotebookPanel(notebookPanel: NotebookPanel): void;
4
+ publishEvent(eventDetail: Object, logNotebookContent?: Boolean): void;
5
+ }
6
+ export declare class TelemetryRouter implements ITelemetryRouter {
7
+ private sessionID?;
8
+ private notebookPanel?;
9
+ /**
10
+ * Load notebookPanel.
11
+ *
12
+ * @param {NotebookPanel} notebookPanel
13
+ */
14
+ loadNotebookPanel(notebookPanel: NotebookPanel): Promise<void>;
15
+ /**
16
+ * Send event data to exporters defined in the configuration file.
17
+ *
18
+ * @param {Object} eventDetail An object containing event details
19
+ * @param {Boolean} logNotebookContent A boolean indicating whether to log the entire notebook or not
20
+ */
21
+ publishEvent(eventDetail: Object, logNotebookContent?: Boolean): Promise<void>;
22
+ }
package/lib/router.js ADDED
@@ -0,0 +1,45 @@
1
+ import { requestAPI } from './handler';
2
+ export class TelemetryRouter {
3
+ /**
4
+ * Load notebookPanel.
5
+ *
6
+ * @param {NotebookPanel} notebookPanel
7
+ */
8
+ async loadNotebookPanel(notebookPanel) {
9
+ this.notebookPanel = notebookPanel;
10
+ }
11
+ /**
12
+ * Send event data to exporters defined in the configuration file.
13
+ *
14
+ * @param {Object} eventDetail An object containing event details
15
+ * @param {Boolean} logNotebookContent A boolean indicating whether to log the entire notebook or not
16
+ */
17
+ async publishEvent(eventDetail, logNotebookContent) {
18
+ var _a, _b, _c, _d, _e, _f, _g;
19
+ if (!this.notebookPanel) {
20
+ throw Error('router needs to load notebookPanel first.');
21
+ }
22
+ // Check if session id received is equal to the stored session id
23
+ if (!this.sessionID ||
24
+ this.sessionID !== ((_b = (_a = this.notebookPanel) === null || _a === void 0 ? void 0 : _a.sessionContext.session) === null || _b === void 0 ? void 0 : _b.id)) {
25
+ this.sessionID = (_d = (_c = this.notebookPanel) === null || _c === void 0 ? void 0 : _c.sessionContext.session) === null || _d === void 0 ? void 0 : _d.id;
26
+ }
27
+ // Construct data
28
+ const requestBody = {
29
+ eventDetail: eventDetail,
30
+ notebookState: {
31
+ sessionID: this.sessionID,
32
+ notebookPath: (_e = this.notebookPanel) === null || _e === void 0 ? void 0 : _e.context.path,
33
+ notebookContent: logNotebookContent
34
+ ? (_g = (_f = this.notebookPanel) === null || _f === void 0 ? void 0 : _f.model) === null || _g === void 0 ? void 0 : _g.toJSON()
35
+ : null // decide whether to log the entire notebook
36
+ }
37
+ };
38
+ // Send data to exporters
39
+ const response = await requestAPI('export', {
40
+ method: 'POST',
41
+ body: JSON.stringify(requestBody)
42
+ });
43
+ console.log(response);
44
+ }
45
+ }
package/package.json ADDED
@@ -0,0 +1,199 @@
1
+ {
2
+ "name": "jupyterlab-pioneer",
3
+ "version": "0.1.2",
4
+ "description": "A JupyterLab extension.",
5
+ "keywords": [
6
+ "jupyter",
7
+ "jupyterlab",
8
+ "jupyterlab-extension"
9
+ ],
10
+ "homepage": "https://github.com/educational-technology-collective/jupyterlab-pioneer",
11
+ "bugs": {
12
+ "url": "https://github.com/educational-technology-collective/jupyterlab-pioneer/issues"
13
+ },
14
+ "license": "BSD-3-Clause",
15
+ "author": {
16
+ "name": "Educational Technology Collective",
17
+ "email": "etc-jupyterlab-telemetry@umich.edu"
18
+ },
19
+ "files": [
20
+ "lib/**/*.{d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",
21
+ "style/**/*.{css,js,eot,gif,html,jpg,json,png,svg,woff2,ttf}"
22
+ ],
23
+ "main": "lib/index.js",
24
+ "types": "lib/index.d.ts",
25
+ "style": "style/index.css",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/educational-technology-collective/jupyterlab-pioneer.git"
29
+ },
30
+ "scripts": {
31
+ "build": "jlpm build:lib && jlpm build:labextension:dev",
32
+ "build:prod": "jlpm clean && jlpm build:lib:prod && jlpm build:labextension",
33
+ "build:labextension": "jupyter labextension build .",
34
+ "build:labextension:dev": "jupyter labextension build --development True .",
35
+ "build:lib": "tsc --sourceMap",
36
+ "build:lib:prod": "tsc",
37
+ "clean": "jlpm clean:lib",
38
+ "clean:lib": "rimraf lib tsconfig.tsbuildinfo",
39
+ "clean:lintcache": "rimraf .eslintcache .stylelintcache",
40
+ "clean:labextension": "rimraf jupyterlab_pioneer/labextension jupyterlab_pioneer/_version.py",
41
+ "clean:all": "jlpm clean:lib && jlpm clean:labextension && jlpm clean:lintcache",
42
+ "eslint": "jlpm eslint:check --fix",
43
+ "eslint:check": "eslint . --cache --ext .ts,.tsx",
44
+ "install:extension": "jlpm build",
45
+ "lint": "jlpm stylelint && jlpm prettier && jlpm eslint",
46
+ "lint:check": "jlpm stylelint:check && jlpm prettier:check && jlpm eslint:check",
47
+ "prettier": "jlpm prettier:base --write --list-different",
48
+ "prettier:base": "prettier \"**/*{.ts,.tsx,.js,.jsx,.css,.json,.md}\"",
49
+ "prettier:check": "jlpm prettier:base --check",
50
+ "stylelint": "jlpm stylelint:check --fix",
51
+ "stylelint:check": "stylelint --cache \"style/**/*.css\"",
52
+ "watch": "run-p watch:src watch:labextension",
53
+ "watch:src": "tsc -w --sourceMap",
54
+ "watch:labextension": "jupyter labextension watch ."
55
+ },
56
+ "dependencies": {
57
+ "@jupyterlab/application": "^4.0.0",
58
+ "@jupyterlab/coreutils": "^6.0.0",
59
+ "@jupyterlab/notebook": "^4.0.5",
60
+ "@jupyterlab/services": "^7.0.0"
61
+ },
62
+ "devDependencies": {
63
+ "@jupyterlab/builder": "^4.0.0",
64
+ "@types/json-schema": "^7.0.11",
65
+ "@types/react": "^18.0.26",
66
+ "@types/react-addons-linked-state-mixin": "^0.14.22",
67
+ "@typescript-eslint/eslint-plugin": "^6.1.0",
68
+ "@typescript-eslint/parser": "^6.1.0",
69
+ "css-loader": "^6.7.1",
70
+ "eslint": "^8.36.0",
71
+ "eslint-config-prettier": "^8.8.0",
72
+ "eslint-plugin-prettier": "^5.0.0",
73
+ "mkdirp": "^1.0.3",
74
+ "npm-run-all": "^4.1.5",
75
+ "prettier": "^3.0.0",
76
+ "rimraf": "^5.0.1",
77
+ "source-map-loader": "^1.0.2",
78
+ "style-loader": "^3.3.1",
79
+ "stylelint": "^15.10.1",
80
+ "stylelint-config-recommended": "^13.0.0",
81
+ "stylelint-config-standard": "^34.0.0",
82
+ "stylelint-csstree-validator": "^3.0.0",
83
+ "stylelint-prettier": "^4.0.0",
84
+ "typescript": "~5.0.2",
85
+ "yjs": "^13.5.0"
86
+ },
87
+ "sideEffects": [
88
+ "style/*.css",
89
+ "style/index.js"
90
+ ],
91
+ "styleModule": "style/index.js",
92
+ "publishConfig": {
93
+ "access": "public"
94
+ },
95
+ "jupyterlab": {
96
+ "discovery": {
97
+ "server": {
98
+ "managers": [
99
+ "pip"
100
+ ],
101
+ "base": {
102
+ "name": "jupyterlab_pioneer"
103
+ }
104
+ }
105
+ },
106
+ "extension": true,
107
+ "outputDir": "jupyterlab_pioneer/labextension"
108
+ },
109
+ "eslintIgnore": [
110
+ "node_modules",
111
+ "dist",
112
+ "coverage",
113
+ "**/*.d.ts"
114
+ ],
115
+ "eslintConfig": {
116
+ "extends": [
117
+ "eslint:recommended",
118
+ "plugin:@typescript-eslint/eslint-recommended",
119
+ "plugin:@typescript-eslint/recommended",
120
+ "plugin:prettier/recommended"
121
+ ],
122
+ "parser": "@typescript-eslint/parser",
123
+ "parserOptions": {
124
+ "project": "tsconfig.json",
125
+ "sourceType": "module"
126
+ },
127
+ "plugins": [
128
+ "@typescript-eslint"
129
+ ],
130
+ "rules": {
131
+ "@typescript-eslint/naming-convention": [
132
+ "error",
133
+ {
134
+ "selector": "interface",
135
+ "format": [
136
+ "PascalCase"
137
+ ],
138
+ "custom": {
139
+ "regex": "^I[A-Z]",
140
+ "match": true
141
+ }
142
+ }
143
+ ],
144
+ "@typescript-eslint/no-unused-vars": [
145
+ "warn",
146
+ {
147
+ "args": "none"
148
+ }
149
+ ],
150
+ "@typescript-eslint/no-explicit-any": "off",
151
+ "@typescript-eslint/no-namespace": "off",
152
+ "@typescript-eslint/no-use-before-define": "off",
153
+ "@typescript-eslint/quotes": [
154
+ "error",
155
+ "single",
156
+ {
157
+ "avoidEscape": true,
158
+ "allowTemplateLiterals": false
159
+ }
160
+ ],
161
+ "curly": [
162
+ "error",
163
+ "all"
164
+ ],
165
+ "eqeqeq": "error",
166
+ "prefer-arrow-callback": "error"
167
+ }
168
+ },
169
+ "prettier": {
170
+ "singleQuote": true,
171
+ "trailingComma": "none",
172
+ "arrowParens": "avoid",
173
+ "endOfLine": "auto",
174
+ "overrides": [
175
+ {
176
+ "files": "package.json",
177
+ "options": {
178
+ "tabWidth": 4
179
+ }
180
+ }
181
+ ]
182
+ },
183
+ "stylelint": {
184
+ "extends": [
185
+ "stylelint-config-recommended",
186
+ "stylelint-config-standard",
187
+ "stylelint-prettier/recommended"
188
+ ],
189
+ "plugins": [
190
+ "stylelint-csstree-validator"
191
+ ],
192
+ "rules": {
193
+ "csstree/validator": true,
194
+ "property-no-vendor-prefix": null,
195
+ "selector-no-vendor-prefix": null,
196
+ "value-no-vendor-prefix": null
197
+ }
198
+ }
199
+ }
package/style/base.css ADDED
@@ -0,0 +1,5 @@
1
+ /*
2
+ See the JupyterLab Developer Guide for useful CSS Patterns:
3
+
4
+ https://jupyterlab.readthedocs.io/en/stable/developer/css.html
5
+ */
@@ -0,0 +1 @@
1
+ @import url('base.css');
package/style/index.js ADDED
@@ -0,0 +1 @@
1
+ import './base.css';