diff --git a/ipyplot/_html_helpers.py b/ipyplot/_html_helpers.py index adc8803..569a2a3 100644 --- a/ipyplot/_html_helpers.py +++ b/ipyplot/_html_helpers.py @@ -8,6 +8,7 @@ import numpy as np import shortuuid from numpy import str_ +import urllib from ._img_helpers import _img_to_base64 @@ -228,14 +229,14 @@ def _display_html(html: str): display(HTML(_create_html_viewer(html))) return display(HTML(html)) - def _create_img( image: str or object, label: str or int, width: int, grid_style_uuid: str, custom_text: str = None, - force_b64: bool = False): + force_b64: bool = False, + inline_svg: bool = True): """Helper function to generate HTML code for displaying images along with corresponding texts. Parameters @@ -256,7 +257,10 @@ def _create_img( Do mind that using b64 conversion vs reading directly from filepath will be slower. You might need to set this to `True` in environments like Google colab. Defaults to False. - + inline_svg : bool, optional + You can force inlining svg images instead of reading them directly from filepaths with HTML. + Defaults to False. + Returns ------- str @@ -270,10 +274,13 @@ def _create_img( img_html += '

%s

' % str(custom_text) # NOQA E501 use_b64 = True + inline_svg = (inline_svg and image[-4:]==".svg") + if inline_svg: + use_b64 = False # if image is a string (URL) display its URL if type(image) is str or type(image) is str_: img_html += '

%s

' % (image) # NOQA E501 - if not force_b64: + if not force_b64 and not inline_svg: use_b64 = False img_html += '' % image elif "http" in image: @@ -285,6 +292,10 @@ def _create_img( if use_b64: img_html += '' % _img_to_base64(image, width) # NOQA E501 + if inline_svg: + string = urllib.parse.quote(open(image,'r').read()) + img_html += f'' + html = """
@@ -301,7 +312,6 @@ def _create_img( """ % {'0': grid_style_uuid, '1': img_uuid, '2': label, '3': img_html} # NOQA E501 return html - def _create_imgs_grid( images: Sequence[object], labels: Sequence[str or int],