Skip to content

Commit

Permalink
set max-width on visualizing images
Browse files Browse the repository at this point in the history
set max-width on visualising images
  • Loading branch information
giannisdoukas committed Jul 21, 2020
2 parents 592981f + 4f22d58 commit 4c4a18f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cwlkernel/cwlrepository/CWLComponent.py
Expand Up @@ -4,7 +4,7 @@
from io import StringIO
from typing import Dict, List, Union, Optional

import yaml
from ruamel import yaml


class WorkflowComponent(ABC):
Expand Down
35 changes: 22 additions & 13 deletions cwlkernel/kernel_magics.py
Expand Up @@ -3,6 +3,8 @@
import os
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 @@ -254,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 @@ -324,14 +326,20 @@ def data(kernel: CWLKernel, *args):
def github_import(kernel: CWLKernel, url: str):
cwl_factory = WorkflowComponentFactory()
for cwl_file in kernel._github_resolver.resolve(url):
with open(cwl_file) as f:
file_data = f.read()
cwl_component = cwl_factory.get_workflow_component(file_data)
cwl_component._id = os.path.splitext(os.path.basename(cwl_file))[0]
relative_dir = Path(os.path.relpath(cwl_file, kernel._github_resolver._local_root_directory.as_posix()))
kernel.workflow_repository.register_tool(cwl_component, relative_dir)
kernel.send_response(kernel.iopub_socket, 'stream',
{'name': 'stdout', 'text': f"tool '{cwl_component.id}' registered\n"})
try:
with open(cwl_file) as f:
file_data = f.read()
cwl_component = cwl_factory.get_workflow_component(file_data)
cwl_component._id = os.path.splitext(os.path.basename(cwl_file))[0]
relative_dir = Path(os.path.relpath(cwl_file, kernel._github_resolver._local_root_directory.as_posix()))
kernel.workflow_repository.register_tool(cwl_component, relative_dir)
kernel.send_response(kernel.iopub_socket, 'stream',
{'name': 'stdout', 'text': f"tool '{cwl_component.id}' registered\n"})
except Exception as e:
kernel.send_error_response(f'Error on loading tool "{cwl_file}"\n')
stacktrace_error = StringIO()
traceback.print_exc(file=stacktrace_error)
kernel.send_error_response(f'Error: {e}\n{stacktrace_error.getvalue()}')


@CWLKernel.register_magic('viewTool')
Expand Down Expand Up @@ -386,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: 7 additions & 2 deletions tests/test_CWLKernel_magics.py
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 @@ -448,7 +449,7 @@ def test_githubImport_without_id(self):
)

@unittest.skipIf("TRAVIS_IGNORE_DOCKER" in os.environ and os.environ["TRAVIS_IGNORE_DOCKER"] == "true",
"Skipping this test on Travis CI.")
"Skipping this test on Travis CI.")
def test_githubImport_walk_paths(self):
when(requests) \
.get(
Expand Down Expand Up @@ -720,9 +721,13 @@ 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 4c4a18f

Please sign in to comment.