|
|
|
@ -14,7 +14,8 @@ __version__ = '0.2'
|
|
|
|
|
|
|
|
|
|
version_pat = re.compile(r'version (\d+(\.\d+)+)')
|
|
|
|
|
|
|
|
|
|
_TEXT_SAVED = "bash_kernel: saved image data to:"
|
|
|
|
|
_TEXT_SAVED_IMAGE = "bash_kernel: saved image data to:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BashKernel(Kernel):
|
|
|
|
|
implementation = 'bash_kernel'
|
|
|
|
@ -60,7 +61,7 @@ class BashKernel(Kernel):
|
|
|
|
|
cat > $TMPFILE
|
|
|
|
|
echo "%s $TMPFILE" >&2
|
|
|
|
|
}
|
|
|
|
|
""" % _TEXT_SAVED
|
|
|
|
|
""" % _TEXT_SAVED_IMAGE
|
|
|
|
|
self.bashwrapper.run_command(bash_rc)
|
|
|
|
|
|
|
|
|
|
def do_execute(self, code, silent, store_history=True,
|
|
|
|
@ -90,8 +91,13 @@ class BashKernel(Kernel):
|
|
|
|
|
|
|
|
|
|
# Send images, if any
|
|
|
|
|
for filename in image_filenames:
|
|
|
|
|
self.send_response(self.iopub_socket, 'display_data',
|
|
|
|
|
display_data(filename))
|
|
|
|
|
try:
|
|
|
|
|
data = display_data(filename)
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
message = {'name': 'stdout', 'text': str(e)}
|
|
|
|
|
self.send_response(self.iopub_socket, 'stream', message)
|
|
|
|
|
else:
|
|
|
|
|
self.send_response(self.iopub_socket, 'display_data', data)
|
|
|
|
|
|
|
|
|
|
if interrupted:
|
|
|
|
|
return {'status': 'abort', 'execution_count': self.execution_count}
|
|
|
|
@ -139,9 +145,12 @@ class BashKernel(Kernel):
|
|
|
|
|
def display_data(filename):
|
|
|
|
|
with open(filename, 'rb') as f:
|
|
|
|
|
image = f.read()
|
|
|
|
|
unlink(filename)
|
|
|
|
|
|
|
|
|
|
image_type = imghdr.what(None, image)
|
|
|
|
|
if image_type:
|
|
|
|
|
if image_type is None:
|
|
|
|
|
raise ValueError("Not a valid image: %s" % image)
|
|
|
|
|
|
|
|
|
|
image_data = urllib.parse.quote(base64.b64encode(image))
|
|
|
|
|
content = {
|
|
|
|
|
'source': 'kernel',
|
|
|
|
@ -149,15 +158,6 @@ def display_data(filename):
|
|
|
|
|
'image/' + image_type: image_data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
content = {
|
|
|
|
|
'source': 'kernel',
|
|
|
|
|
'data': {
|
|
|
|
|
'text': "Not a valid image."
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlink(filename)
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -166,7 +166,7 @@ def extract_image_filenames(output):
|
|
|
|
|
image_filenames = []
|
|
|
|
|
|
|
|
|
|
for line in output.split("\n"):
|
|
|
|
|
if line.startswith(_TEXT_SAVED):
|
|
|
|
|
if line.startswith(_TEXT_SAVED_IMAGE):
|
|
|
|
|
filename = line.rstrip().split(": ")[-1]
|
|
|
|
|
image_filenames.append(filename)
|
|
|
|
|
else:
|
|
|
|
|