Created
October 8, 2024 15:54
-
-
Save iandesj/de3bad984125379f50bc611c20690cb2 to your computer and use it in GitHub Desktop.
Extension example for cell execution events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
JupyterFrontEnd, | |
JupyterFrontEndPlugin | |
} from '@jupyterlab/application'; | |
import { NotebookActions } from '@jupyterlab/notebook'; | |
/** | |
* Initialization data for the constellate_lab_usage extension. | |
*/ | |
const plugin: JupyterFrontEndPlugin<void> = { | |
id: 'constellate_lab_usage:plugin', | |
description: | |
'A JupyterLab extension to collect usage via listening on cell execution commands.', | |
autoStart: true, | |
activate: (app: JupyterFrontEnd) => { | |
console.log('JupyterLab extension constellate_lab_usage is activated!'); | |
NotebookActions.executed.connect((_, args) => { | |
console.log('Cell executed:', args); | |
// create an empty promise that resolves after 3 seconds | |
const constellateSessionId = '4ade0740-fb8d-40ab-bea7-5148fbda4d8e'; | |
fetch('https://api-test.constellate.org/constellate/v1/log/', { | |
method: 'POST', | |
// credentials: 'include', | |
// mode: 'cors', | |
headers: { | |
'Content-Type': 'application/json', | |
Authorization: `UUID ${constellateSessionId}` | |
}, | |
body: JSON.stringify({ | |
generic_type: 'lab-usage', | |
payload: JSON.stringify({ | |
notebook: args.notebook.title.label, | |
cell_id: args.cell.id, | |
cell_type: args.cell.model.type | |
}) | |
}) | |
}) | |
.then(response => { | |
console.log('Log response:', response); | |
}) | |
.catch(error => { | |
console.error('Log error:', error); | |
}); | |
}); | |
} | |
}; | |
export default plugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment