Skip to content

Commit

Permalink
add max width on display images
Browse files Browse the repository at this point in the history
  • Loading branch information
giannisdoukas committed Jul 21, 2020
1 parent 3e9dca9 commit 869415f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 7 additions & 5 deletions cwlkernel/kernel_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import subprocess
import traceback
import xml.etree.ElementTree as ET
from collections import OrderedDict
from copy import deepcopy
from io import StringIO
Expand Down Expand Up @@ -255,7 +256,7 @@ def display_data_image(kernel: CWLKernel, data_name: str):
mime = 'image/svg+xml'
else:
raise ValueError(f'unsupported type {result}')
image = f"""<image src="data:{mime}; base64, {image}" alt="{result}">"""
image = f"""<image src="data:{mime}; base64, {image}" alt="{result}" style="max-width: 100%">"""
kernel.send_response(
kernel.iopub_socket,
'display_data',
Expand Down Expand Up @@ -393,15 +394,16 @@ def visualize_graph(kernel: CWLKernel, tool_id: str):
cwltool_main(['--print-rdf', os.path.abspath(path)], stdout=rdf_stream, logger_handler=handler)
cwl_viewer = CWLViewer(rdf_stream.getvalue())
(dot_object,) = pydot.graph_from_dot_data(cwl_viewer.dot())
image = dot_object.create('dot', 'svg')

ET.register_namespace('', 'http://www.w3.org/2000/svg')
image_xml = ET.fromstring(dot_object.create('dot', 'svg').decode())
image_container = f'<div style="max-width: 100%;">{ET.tostring(image_xml, method="html").decode()}</div>'
kernel.send_response(
kernel.iopub_socket,
'display_data',
{
'data': {
"image/svg+xml": image.decode(),
"text/plain": image.decode()
"text/html": image_container,
"text/plain": image_container
},
'metadata': {},
},
Expand Down
9 changes: 8 additions & 1 deletion tests/test_CWLKernel_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tarfile
import tempfile
import unittest
import xml.etree.ElementTree as ET
from io import StringIO
from pathlib import Path

Expand Down Expand Up @@ -720,9 +721,15 @@ def test_view_magic_command(self):
)

self.assertIn(
'image/svg+xml',
'text/html',
responses[-1][0][2]['data'])

# should not raise an exception
tree = ET.fromstring(responses[-1][0][2]['data']['text/html'])
self.assertEqual(len(tree.findall("./{http://www.w3.org/2000/svg}svg")), 1)



def test_scatter_tool(self):
kernel = CWLKernel()
# cancel send_response
Expand Down

0 comments on commit 869415f

Please sign in to comment.