Skip to content

Instantly share code, notes, and snippets.

@iandesj
Created October 8, 2024 15:54
Show Gist options
  • Save iandesj/de3bad984125379f50bc611c20690cb2 to your computer and use it in GitHub Desktop.
Save iandesj/de3bad984125379f50bc611c20690cb2 to your computer and use it in GitHub Desktop.
Extension example for cell execution events
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