Commit 3473cf87 authored by SOLIMAN Sylvain's avatar SOLIMAN Sylvain
Browse files

display images (untested)

parent 1ca053b8
# very much inspired form the example bash_kernel
import base64
import imghdr
import os
def display_data_for_image(filename):
with open(filename, 'rb') as f:
image =
image_type = imghdr.what(None, image)
if image_type is None:
raise ValueError("Not a valid image: %s" % image)
image_data = base64.b64encode(image).decode('ascii')
content = {
'data': {
'image/' + image_type: image_data
'metadata': {}
return content
def extract_image_filenames(output):
output_lines = []
image_filenames = []
for line in output.split("\n"):
if line.startswith('<img src="'):
filename = line.rstrip().split('"')[1]
output = "\n".join(output_lines)
return image_filenames, output
......@@ -3,6 +3,8 @@ from subprocess import check_output
from ipykernel.kernelbase import Kernel
from pexpect import replwrap
from .images import extract_image_filenames, display_data_for_image
class BiochamKernel(Kernel):
implementation = 'Biocham'
......@@ -20,19 +22,32 @@ class BiochamKernel(Kernel):
Kernel.__init__(self, **kwargs)
def _start_biocham(self):
self.biocham = replwrap.REPLWrapper( 'biocham4 --jupyter', 'biocham: ', None)
self.biocham = replwrap.REPLWrapper(
'biocham4 --jupyter',
'biocham: ',
def do_execute(self, code, silent, store_history=True,
user_expressions=None, allow_stdin=False):
output = self.biocham.run_command(code)
image_filenames, output = extract_image_filenames(output)
if not silent:
stream_content = {'name': 'stdout', 'text': output}
self.send_response(self.iopub_socket, 'stream', stream_content)
for filename in image_filenames:
data = display_data_for_image(filename)
except ValueError as e:
message = {'name': 'stdout', 'text': str(e)}
self.send_response(self.iopub_socket, 'stream', message)
self.send_response(self.iopub_socket, 'display_data', data)
return {'status': 'ok',
# The base class increments the execution count
'execution_count': self.execution_count,
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment