8000 Merge pull request #10 from brenns10/master · Nikki-Gu/jupyter-notify@bac1a9d · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Merge pull request ShopRunner#10 from brenns10/master
Browse files Browse the repository at this point in the history
Add require_interaction option to make notification sticky.
  • Loading branch information
mdagost authored Aug 18, 2017
2 parents 33c20be + bbfb808 commit bac1a9d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ To test the extension, try
import time
time.sleep(5)
8000 ```

## Options

You may specify options while loading the magic:

```python
import jupyternotify
ip = get_ipython()
ip.register_magics(jupyternotify.JupyterNotifyMagics(
ip,
option_name="option_value"
))
```

The following options exist:
- `require_interaction` - Boolean, default False. When this is true,
notifications will remain on screen until dismissed. This feature is currently
only available in Google Chrome.
13 changes: 5 additions & 8 deletions jupyternotify/js/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,29 @@ $(document).ready(
// been sent and avoid duplicates on page reload
var notifiedDiv = document.createElement("div")
notifiedDiv.id = "%(notification_uuid)s"
element.append(notifiedDiv)
element.append(notifiedDiv)
}

// only send notifications if the pageload is complete; this will
// help stop extra notifications when a saved notebook is loaded,
// which during testing gives us state "interactive", not "complete"
if (document.readyState === 'complete') {
// check for the div that signifies that the notification
// was already sent
if (document.getElementById("%(notification_uuid)s") === null) {
var notificationPayload = {
icon: "/static/base/images/favicon.ico",
body: "Cell Execution Has Finished!!",
}
var notificationPayload = %(options)s;
if (Notification.permission !== 'denied') {
if (Notification.permission !== 'granted') {
Notification.requestPermission(function (permission) {
if(!('permission' in Notification)) {
Notification.permission = permission
}
if (Notification.permission === 'granted') {
if (Notification.permission === 'granted') {
var notification = new Notification("Jupyter Notebook", notificationPayload)
appendUniqueDiv()
}
})
} else if (Notification.permission === 'granted') {
} else if (Notification.permission === 'granted') {
var notification = new Notification("Jupyter Notebook", notificationPayload)
appendUniqueDiv()
}
Expand Down
13 changes: 11 additions & 2 deletions jupyternotify/jupyternotify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# see https://ipython.org/ipython-doc/3/config/custommagics.html
# for more details on the implementation here
import json
import uuid

from IPython.core.getipython import get_ipython
Expand All @@ -10,11 +11,16 @@

@magics_class
class JupyterNotifyMagics(Magics):
def __init__(self, shell):
def __init__(self, shell, require_interaction=False):
super(JupyterNotifyMagics, self).__init__(shell)
with open(resource_filename("jupyternotify", "js/init.js")) as jsFile:
jsString = jsFile.read()
display(Javascript(jsString))
self.options = json.dumps({
"requireInteraction": require_interaction,
"body": "Cell Execution Has Finished!!",
"icon": "/static/base/images/favicon.ico",
})

@cell_magic
def notify(self, line, cell):
Expand All @@ -27,7 +33,10 @@ def notify(self, line, cell):
# display our browser notification using javascript
with open(resource_filename("jupyternotify", "js/notify.js")) as jsFile:
jsString = jsFile.read()
display(Javascript(jsString % {"notification_uuid": notification_uuid}))
display(Javascript(jsString % {
"notification_uuid": notification_uuid,
"options": self.options,
}))

# finally, if we generated an exception, print the traceback
if output.error_in_exec is not None:
Expand Down

0 comments on commit bac1a9d

Please sign in to comment.
0