Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for a few mimetimes from display_data messages (text/plain, text/html, image/png). #403

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion kernel_gateway/notebook_http/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def finish_future(self, future, result_accumulator):
"""
if result_accumulator['error']:
future.set_exception(CodeExecutionError(result_accumulator['error']))
elif len(result_accumulator['display']) > 0:
out = []
for result in result_accumulator['display']:
if 'image/png' in result:
out.append(
'<img alt="%s" src="data:image/png;base64,%s" />'
% (result.get('text/plain', ''), result['image/png'])
)
continue
if 'text/html' in result:
out.append(result['text/html'])
if 'text/plain' in result:
out.append(result['text/plain'])
future.set_result('\n'.join(out))

elif len(result_accumulator['stream']) > 0:
future.set_result(''.join(result_accumulator['stream']))
elif result_accumulator['result']:
Expand Down Expand Up @@ -113,6 +128,9 @@ def on_recv(self, result_accumulator, future, parent_header, msg):
# Store the execute result
elif msg['header']['msg_type'] == 'execute_result':
result_accumulator['result'] = msg['content']['data']
# Accumulate display data
elif msg['header']['msg_type'] == 'display_data':
result_accumulator['display'].append(msg['content']['data'])
# Accumulate the stream messages
elif msg['header']['msg_type'] == 'stream':
# Only take stream output if it is on stdout or if the kernel
Expand Down Expand Up @@ -152,7 +170,13 @@ def execute_code(self, kernel_client, kernel_id, source_code):
If the kernel returns any error
"""
future = Future()
result_accumulator = {'stream' : [], 'error' : None, 'result' : None}

result_accumulator = {
'display': [],
'stream': [],
'error': None,
'result': None,
}
parent_header = kernel_client.execute(source_code)
on_recv_func = partial(self.on_recv, result_accumulator, future, parent_header)
self.kernel_pool.on_recv(kernel_id, on_recv_func)
Expand Down