Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
srill fixing issues with plotly
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Jun 17, 2017
1 parent 769a49d commit 7af9e9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion _unittests/ut_helpgen/test_notebooks_gallery_bug.py
Expand Up @@ -44,7 +44,7 @@ def test_notebook_gallery_bug(self):
temp = get_temp_folder(__file__, "temp_gallery_bug")
fold = os.path.normpath(os.path.join(
temp, "..", "notebooks_js"))
assert os.path.exists(fold)
self.assertTrue(os.path.exists(fold))

file = os.path.join(temp, "all_notebooks.rst")
build_notebooks_gallery(fold, file, fLOG=fLOG)
Expand Down
30 changes: 21 additions & 9 deletions src/pyquickhelper/ipythonhelper/notebook_runner.py
Expand Up @@ -667,10 +667,13 @@ def _check_thumbnail_tuple(self, b):
if not isinstance(b[0], str):
raise TypeError(
"str expected for svg, not {0}".format(type(b[0])))
elif b[1] == "vnd.plotly.v1+html":
# Don't know how to extract a snippet out of this.
pass
else:
if not isinstance(b[0], bytes):
raise TypeError(
"bytes expected for images, not {0}".format(type(b[0])))
"bytes expected for images, not {0}\n{1}".format(type(b[0]), b))
return b

def create_picture_from(self, text, format, asbytes=True, context=None):
Expand Down Expand Up @@ -826,6 +829,8 @@ def cell_image(self, cell, image_from_text=False):
.format(kind, output["output_type"], output, cell))
if len(results) > 0:
res = self._merge_images(results)
if res[0] is None:
return None
self._check_thumbnail_tuple(res)
return res
else:
Expand Down Expand Up @@ -886,7 +891,7 @@ def cell_height(self, cell):
nbl += 10
else:
raise NotImplementedError("cell type: '{0}'\nk='{1}'\nv='{2}'\nCELL:\n{3}".format(kind,
k, v, cell))
k, v, cell))
elif output["output_type"] == "stream":
v = output["text"]
nbl += len(v.split("\n"))
Expand Down Expand Up @@ -1159,7 +1164,7 @@ def get_thumbnail(self, max_width=200, max_height=200):
cells.reverse()
for cell in cells:
c = self.cell_image(cell, False)
if c is not None and len(c) > 0 and len(c[0]) > 0:
if c is not None and len(c) > 0 and len(c[0]) > 0 and c[1] != "vnd.plotly.v1+html":
self._check_thumbnail_tuple(c)
images.append(c)
if len(images) == 0:
Expand Down Expand Up @@ -1190,7 +1195,9 @@ def get_thumbnail(self, max_width=200, max_height=200):
image = images[0]

# zoom
if image[1] != "svg":
if image[1] == "vnd.plotly.v1+html":
return None
elif image[1] != "svg":
img = self._scale_image(
image[0], image[1], max_width=max_width, max_height=max_height)
return img
Expand Down Expand Up @@ -1263,7 +1270,8 @@ def _merge_images(self, results):
if len(formats_counts) == 1:
format = results[0][1]
else:
items = sorted(((v, k) for k, v in formats_counts.items()), False)
items = sorted(((v, k)
for k, v in formats_counts.items()), reverse=False)
for it in items:
format = it
break
Expand Down Expand Up @@ -1293,7 +1301,11 @@ def _merge_images(self, results):
dy -= img.size[1] * over
new_im.paste(img, (0, max(int(dy), 0)))

image_buffer = BytesIO()
new_im.save(image_buffer, "PNG")
b = image_buffer.getvalue(), "png"
return b
if max(dx, dy) > 0:
image_buffer = BytesIO()
new_im.save(image_buffer, "PNG")
b = image_buffer.getvalue(), "png"
return b
else:
b = None, "png"
return b

0 comments on commit 7af9e9b

Please sign in to comment.