Skip to content
This repository has been archived by the owner on Dec 29, 2017. It is now read-only.

Commit

Permalink
Merge pull request #11 from MatthewTurk/master
Browse files Browse the repository at this point in the history
Update to v2 for PNaCl, add in directory picker
  • Loading branch information
KesterTong committed Jun 30, 2014
2 parents 57fed90 + 4e23cf6 commit 38a9eb4
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 11 deletions.
1 change: 1 addition & 0 deletions chrome/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Kernel.prototype.start = function() {
addParam('PS_EXIT_MESSAGE', 'exited');
addParam('TERM', 'xterm-256color');
addParam('NACL_DATA_URL', '/');
addParam('ZEROPY_PACKAGE', 'zeropy_20140520.tar.gz')

var eventTypes = {'message': ['data'],
'progress': ['loaded', 'total'],
Expand Down
33 changes: 33 additions & 0 deletions chrome/launch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,37 @@ var launchNotebookWindow = function(params) {
'height': 600
}
});
window.requestFileSystem = window.requestFileSystem ||
window.webkitRequestFileSystem;
window.terms = [];
window.fileSystems = {};
function newHTML5Volume(persistent, sizeInMegabytes) {
if (persistent == true) {
fsType = PERSISTENT;
name = 'persistent';
} else {
fsType = TEMPORARY;
name = 'temporary';
}
function errorFn(e) {
console.log("Binding " + name + " failed: " + e);
}
function registerFileSystem(fs) {
console.log("Bound " + name);
window.fileSystems[name] = fs;
}
function grantedFileSystem(grantedBytes) {
window.requestFileSystem(fsType,
grantedBytes,
registerFileSystem,
errorFn);
}
navigator.webkitPersistentStorage.requestQuota(
sizeInMegabytes*1024*1024,
grantedFileSystem,
errorFn
)
}
newHTML5Volume(true, 8192);

};
2 changes: 2 additions & 0 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"udp-send-to:*:*"]
},
"webview",
{"fileSystem" : ["write", "directory", "retainEntries"]},
"unlimitedStorage",
"storage",
"clipboardRead",
"clipboardWrite",
Expand Down
23 changes: 16 additions & 7 deletions chrome/pnacl/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@

"""A simple shell that uses the IPython messaging system."""

import time
import json
import logging
import sys
sys_stdout = sys.stdout
sys_stderr = sys.stderr

def emit(s):
print >> sys_stderr, "EMITTING: %s" % (s)
time.sleep(1)

import IPython
from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
Expand All @@ -17,7 +24,7 @@
from IPython.config.configurable import Configurable

# module defined in shell.cc for communicating via pepper API
import ppmessage
from pyppapi import nacl_instance

def sendMessage(socket_name, msg_type, parent_header=None, content=None):
if parent_header is None:
Expand All @@ -27,9 +34,11 @@ def sendMessage(socket_name, msg_type, parent_header=None, content=None):
msg = {
'header': {'msg_type': msg_type},
'parent_header': parent_header,
'content': content
'content': content,
'msg_type': msg_type,
}
ppmessage._PostJSONMessage(socket_name, json.dumps(msg))
nacl_instance.send_raw_object({'stream': socket_name,
'json': json.dumps(msg)})

class MsgOutStream(object):
"""Class to overrides stderr and stdout."""
Expand Down Expand Up @@ -58,13 +67,10 @@ def writelines(self, sequence):
# override sys.stdout and sys.stderr to broadcast on iopub
stdout_stream = MsgOutStream('stdout')
stderr_stream = MsgOutStream('stderr')
sys_stdout = sys.stdout
sys_stderr = sys.stderr
sys.stdout = stdout_stream
sys.stderr = stderr_stream



class PepperShellDisplayHook(DisplayHook):
parent_header = Dict({})

Expand Down Expand Up @@ -161,7 +167,10 @@ def __init__(self):

while 1:
sendMessage('iopub', 'status', content={'execution_state': 'idle'})
msg = json.loads(ppmessage._AcquireJSONMessageWait())
msg = None
while msg is None:
msg = nacl_instance.wait_for_message()
msg = json.loads(msg['json'])
sendMessage('iopub', 'status', content={'execution_state': 'busy'})

if not 'header' in msg:
Expand Down
2 changes: 1 addition & 1 deletion chrome/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ window.onload = function() {
chrome.identity.getAuthToken({'interactive': true }, function(token) {
if (token) {
// Remove this cached token and re-authenticate
chrome.identity.removeCachedAuthToken(token, authenticate);
chrome.identity.removeCachedAuthToken({'token': token}, authenticate);
} else {
// There is no cached token, so prompt for authentication
authenticate();
Expand Down
9 changes: 8 additions & 1 deletion chrome/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var notebookPath = '/static/v2/notebook.html';
var notebookUrl = chrome.runtime.getURL(notebookPath);
var serverOrigin = notebookUrl.substr(0, notebookUrl.length -
notebookPath.length);

var tokenRefreshInterval = 10 * 60 * 1000; // 10 minutes

var webview = document.getElementById('webview');
Expand All @@ -25,6 +24,14 @@ window.addEventListener('message', function(message) {
kernel.start();
} else if (message.data === 'restart_kernel') {
kernel.restart();
} else if (message.data == 'pick_file') {
chrome.fileSystem.chooseEntry({type: 'openDirectory'}, function(theEntry) {
if (!theEntry) {
return;
}
kernel.handleMessage({'filesystem_name': theEntry.fullPath,
'filesystem_resource': theEntry.filesystem});
});
} else if (message.data && message.data.json) {
kernel.handleMessage(message.data);
}
Expand Down
4 changes: 4 additions & 0 deletions ipython_patch/services/kernels/js/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,10 @@ var IPython = (function (IPython) {
} else if (execution_state === 'dead') {
this.stop_channels();
$([IPython.events]).trigger('status_dead.Kernel', {kernel: this});
} else if (execution_state == 'nacl_ready') {
$([IPython.events]).trigger(IPythonInterface.KERNEL_STARTED_EVENT,
{kernel: this});
this.running = true;
}
};

Expand Down
9 changes: 9 additions & 0 deletions static/frontend/js/filepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,12 @@ colab.filepicker.selectFileAndReload = function() {
};
colab.filepicker.selectFile(cb);
};


/**
* Selects a file from the local filesystem and posts a message directly.
*/
colab.filepicker.selectLocalFile = function() {
// use local storage to retain access to this file
colab.globalKernel.kernel_window.postMessage('pick_file', colab.globalKernel.kernel_origin);
}
4 changes: 4 additions & 0 deletions static/frontend/js/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ colab.createMenubar = function(document, permissions) {
colab.drive.openDriveViewer();
break;

case 'openlocalfs-menuitem':
colab.filepicker.selectLocalFile();
break;

case 'clear-outputs-menuitem':
colab.globalNotebook.clearOutputs();
break;
Expand Down
4 changes: 2 additions & 2 deletions static/frontend/js/pnacl_kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ colab.PNaClKernel.prototype.handleMessage_ = function (message) {
if (typeof data == 'string' || data instanceof String) {
console.log('nacl>' + data.substring(tty_prefix.length));
} else if (data.stream == 'iopub') {
that._handle_iopub_reply({data: data.json});
that._handle_iopub_message({data: data.json});
} else if (data.stream == 'shell') {
that._handle_shell_reply({data: data.json});
}
Expand All @@ -109,4 +109,4 @@ colab.PNaClKernel.prototype.handleMessage_ = function (message) {
that.running = false;
$([IPython.events]).trigger('status_dead.Kernel', {kernel: that});
}
};
};
2 changes: 2 additions & 0 deletions static/frontend/notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
<div class="goog-menuitem goog-menuitem" id="open-menuitem">Open...</div>
<div class="goog-menuitem goog-menuitem" id="new-menuitem">New Notebook</div>
<div class="goog-menuseparator goog-menuitem-disabled"></div>
<div class="goog-menuitem" id="openlocalfs-menuitem">Open Local Directory</div>
<div class="goog-menuseparator goog-menuitem-disabled"></div>
<div class="goog-menuitem" id="share-menuitem">Share...</div>
<div class="goog-menuitem" id="save-menuitem">Save and Checkpoint</div>
<div class="goog-menuitem" id="viewindrive-menuitem">View in Drive...</div>
Expand Down

0 comments on commit 38a9eb4

Please sign in to comment.