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 displaying image/png data. #406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
40 changes: 39 additions & 1 deletion kernel_gateway/notebook_http/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ def initialize(self, sources, response_sources, kernel_pool, kernel_name, kernel
self.response_sources = response_sources
self.kernel_language = kernel_language

def _accumulate_display(self, results):
"""Accumulates result chunks for "display" messages and prepares them as

in-line <img> tags. See
https://ipython.org/ipython-doc/3/development/messaging.html#display-data
for details on the display protocol.

Parameters
----------
results: list
A list of results containing display data.
"""
out = []
for result in results:
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"])
return out

def finish_future(self, future, result_accumulator):
"""Resolves the promise to respond to a HTTP request handled by a
kernel in the pool.
Expand All @@ -84,6 +110,10 @@ def finish_future(self, future, result_accumulator):
"""
if result_accumulator["error"]:
future.set_exception(CodeExecutionError(result_accumulator["error"]))
elif len(result_accumulator["display"]) > 0:
future.set_result(
"\n".join(self._accumulate_display(result_accumulator["display"]))
)
elif len(result_accumulator["stream"]) > 0:
future.set_result("".join(result_accumulator["stream"]))
elif result_accumulator["result"]:
Expand Down Expand Up @@ -123,6 +153,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 @@ -162,7 +195,12 @@ 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
Loading