Skip to content

Commit

Permalink
Merge pull request #4 from mwakaba2/non-code-cell-prompt-issue
Browse files Browse the repository at this point in the history
Fix: don't display prompt for non-code cells
  • Loading branch information
mwakaba2 authored Mar 27, 2021
2 parents beee1e1 + b34f771 commit fd31529
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 50 deletions.
2 changes: 1 addition & 1 deletion binder/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ channels:
dependencies:
- jupyterlab=3
- pip:
- jupyterlab-notifications==0.1.3
- jupyterlab-notifications==0.1.4
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jupyterlab-notifications",
"version": "0.1.3",
"version": "0.1.4",
"description": "Jupyterlab extension to show notebook cell completion browser notifications",
"keywords": [
"jupyter",
Expand Down
95 changes: 60 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,49 @@ import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

import { NotebookActions } from '@jupyterlab/notebook';

import { IObservableJSON } from '@jupyterlab/observables';
import { ISettingRegistry } from '@jupyterlab/settingregistry';

import { checkBrowserNotificationSettings } from './settings';

/**
* Extracts Code Cell Start and End Time
*/
function extractExecutionMetadata(metadata: IObservableJSON): [Date, Date] {
const executionMetadata = Object.assign({}, metadata.get('execution') as any);
const cellStartTime = new Date(
executionMetadata['shell.execute_reply.started']
);
const cellEndTime = new Date(executionMetadata['shell.execute_reply']);
return [cellStartTime, cellEndTime];
}

/**
* Constructs notification message and displays it.
*/
function displayNotification(
cellDuration: string,
cellNumber: number,
reportCellNumber: boolean,
reportCellExecutionTime: boolean
): void {
const notificationPayload = {
icon: '/static/favicon.ico',
body: ''
};
let message = '';
if (reportCellNumber && reportCellExecutionTime) {
message = `Cell[${cellNumber}] Duration: ${cellDuration}`;
} else if (reportCellNumber) {
message = `Cell Number: ${cellNumber}`;
} else if (reportCellExecutionTime) {
message = `Cell Duration: ${cellDuration}`;
}
notificationPayload.body = message;
new Notification('Notebook Cell Completed!', notificationPayload);
}

const extension: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-notifications:plugin',
autoStart: true,
Expand Down Expand Up @@ -37,42 +73,31 @@ const extension: JupyterFrontEndPlugin<void> = {
NotebookActions.executed.connect((_, args) => {
if (enabled) {
const { cell, notebook } = args;
const codeCell = cell.model.type === 'code';
const nonEmptyCell = cell.model.value.text.length > 0;
const metadata = cell.model.metadata;
if (metadata.has('execution')) {
const executionMetadata = Object.assign(
{},
metadata.get('execution') as any
);
const cellStartTime = new Date(
executionMetadata['shell.execute_reply.started']
);
const cellEndTime = new Date(
executionMetadata['shell.execute_reply']
);
const diff = new Date(<any>cellEndTime - <any>cellStartTime);
if (diff.getSeconds() >= minimumCellExecutionTime) {
const notificationPayload = {
icon: '/static/favicon.ico',
body: ''
};
let message = '';
const cellDuration = diff.toISOString().substr(11, 8);
const cellNumber = notebook.activeCellIndex;
if (reportCellNumber && reportCellExecutionTime) {
message = `Cell[${cellNumber}] Duration: ${cellDuration}`;
} else if (reportCellNumber) {
message = `Cell Number: ${cellNumber}`;
} else if (reportCellExecutionTime) {
message = `Cell Duration: ${cellDuration}`;
if (codeCell && nonEmptyCell) {
if (metadata.has('execution')) {
const [cellStartTime, cellEndTime] = extractExecutionMetadata(
metadata
);
const diff = new Date(<any>cellEndTime - <any>cellStartTime);
if (diff.getSeconds() >= minimumCellExecutionTime) {
const cellDuration = diff.toISOString().substr(11, 8);
const cellNumber = notebook.activeCellIndex;
displayNotification(
cellDuration,
cellNumber,
reportCellNumber,
reportCellExecutionTime
);
}
notificationPayload.body = message;
new Notification('Notebook Cell Completed!', notificationPayload);
} else {
alert(
'Notebook Cell Timing needs to be enabled for Jupyterlab Notifications to work. ' +
'Please go to Settings -> Advanced Settings Editor -> Notebook and update setting to {"recordTiming": true}'
);
}
} else {
alert(
'Notebook Cell Timing needs to be enabled for Jupyterlab Notifications to work. ' +
'Please go to Settings -> Advanced Settings Editor -> Notebook and update setting to {"recordTiming": true}'
);
}
}
});
Expand Down
69 changes: 56 additions & 13 deletions tutorial/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"cells": [
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 13,
"metadata": {
"execution": {
"iopub.execute_input": "2021-03-19T03:37:14.667688Z",
"iopub.status.busy": "2021-03-19T03:37:14.667455Z",
"iopub.status.idle": "2021-03-19T03:37:14.671324Z",
"shell.execute_reply": "2021-03-19T03:37:14.670402Z",
"shell.execute_reply.started": "2021-03-19T03:37:14.667662Z"
"iopub.execute_input": "2021-03-27T03:06:18.533459Z",
"iopub.status.busy": "2021-03-27T03:06:18.533220Z",
"iopub.status.idle": "2021-03-27T03:06:18.537331Z",
"shell.execute_reply": "2021-03-27T03:06:18.536387Z",
"shell.execute_reply.started": "2021-03-27T03:06:18.533425Z"
},
"tags": []
},
Expand All @@ -28,14 +28,14 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {
"execution": {
"iopub.execute_input": "2021-03-19T03:37:16.906751Z",
"iopub.status.busy": "2021-03-19T03:37:16.906516Z",
"iopub.status.idle": "2021-03-19T03:37:20.035526Z",
"shell.execute_reply": "2021-03-19T03:37:20.034900Z",
"shell.execute_reply.started": "2021-03-19T03:37:16.906728Z"
"iopub.execute_input": "2021-03-27T03:06:19.213426Z",
"iopub.status.busy": "2021-03-27T03:06:19.213237Z",
"iopub.status.idle": "2021-03-27T03:06:22.337638Z",
"shell.execute_reply": "2021-03-27T03:06:22.336868Z",
"shell.execute_reply.started": "2021-03-27T03:06:19.213404Z"
},
"tags": []
},
Expand All @@ -53,6 +53,49 @@
"print(\"hello world!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Hello World"
]
},
{
"cell_type": "raw",
"metadata": {
"execution": {
"iopub.execute_input": "2021-03-27T02:29:48.597351Z",
"iopub.status.busy": "2021-03-27T02:29:48.597122Z",
"iopub.status.idle": "2021-03-27T02:29:48.602494Z",
"shell.execute_reply": "2021-03-27T02:29:48.600860Z",
"shell.execute_reply.started": "2021-03-27T02:29:48.597324Z"
}
},
"source": [
"<h1>Hello World</h1>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -77,7 +120,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.7.10"
}
},
"nbformat": 4,
Expand Down

0 comments on commit fd31529

Please sign in to comment.