From b0ccb7848d18666f59b473cabdb1348389374855 Mon Sep 17 00:00:00 2001 From: Maarten-vr <55130010+Maarten-vr@users.noreply.github.com> Date: Fri, 11 Dec 2020 14:21:46 +0100 Subject: [PATCH] Added option to hide url text from the images (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added option to remove url_text from the images * updated docs in notebook examples * upd bump2version cfg * upd version 1.1.0->1.1.1 Co-authored-by: Karol Żak --- .bumpversion.cfg | 4 +-- ipyplot/__init__.py | 2 +- ipyplot/_html_helpers.py | 16 ++++++++++-- ipyplot/_plotting.py | 12 +++++++++ notebooks/gear-images-examples.ipynb | 39 +++++++++++++++++++++++----- setup.py | 2 +- 6 files changed, 63 insertions(+), 12 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index ab32af2..86aa7c2 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,6 +1,6 @@ [bumpversion] -current_version = 1.1.0 -commit = True +current_version = 1.1.1 +commit = False tag = True [bumpversion:file:setup.py] diff --git a/ipyplot/__init__.py b/ipyplot/__init__.py index 786eefb..21454ef 100644 --- a/ipyplot/__init__.py +++ b/ipyplot/__init__.py @@ -16,7 +16,7 @@ from ._plotting import plot_images, plot_class_tabs, plot_class_representations __name__ = "IPyPlot" -__version__ = "1.1.0" +__version__ = "1.1.1" if 'google.colab' in _sys.modules: # pragma: no cover print( diff --git a/ipyplot/_html_helpers.py b/ipyplot/_html_helpers.py index adc8803..6841134 100644 --- a/ipyplot/_html_helpers.py +++ b/ipyplot/_html_helpers.py @@ -24,6 +24,7 @@ def _create_tabs( max_imgs_per_tab: int = 30, img_width: int = 150, zoom_scale: float = 2.5, + show_url: bool = True, force_b64: bool = False, tabs_order: Sequence[str or int] = None): """ @@ -53,6 +54,8 @@ def _create_tabs( Scale for zoom-in-on-click feature. Best to keep between 1.0~5.0. Defaults to 2.5. + show_url : bool, optional + Defines if the urls are displayed as text above the images. force_b64 : bool, optional You can force conversion of images to base64 instead of reading them directly from filepaths with HTML. Do mind that using b64 conversion vs reading directly from filepath will be slower. @@ -134,6 +137,7 @@ def _create_tabs( img_width=img_width, zoom_scale=zoom_scale, custom_texts=custom_texts[tab_imgs_mask] if custom_texts is not None else None, # NOQA E501 + show_url=show_url, force_b64=force_b64) html += '' @@ -235,6 +239,7 @@ def _create_img( width: int, grid_style_uuid: str, custom_text: str = None, + show_url: bool = True, force_b64: bool = False): """Helper function to generate HTML code for displaying images along with corresponding texts. @@ -251,6 +256,8 @@ def _create_img( custom_text : str, optional Additional text to be displayed above the image but below the label name. Defaults to None. + show_url : bool, optional + Defines if the urls are displayed as text above the images. force_b64 : bool, optional You can force conversion of images to base64 instead of reading them directly from filepaths with HTML. Do mind that using b64 conversion vs reading directly from filepath will be slower. @@ -272,7 +279,8 @@ def _create_img( use_b64 = True # 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 show_url: + img_html += '

%s

' % (image) # NOQA E501 if not force_b64: use_b64 = False img_html += '' % image @@ -309,6 +317,7 @@ def _create_imgs_grid( max_images: int = 30, img_width: int = 150, zoom_scale: float = 2.5, + show_url: bool = True, force_b64: bool = False): """ Creates HTML code for displaying images provided in `images` param in grid-like layout. @@ -337,6 +346,8 @@ def _create_imgs_grid( Scale for zoom-in-on-click feature. Best to keep between 1.0~5.0. Defaults to 2.5. + show_url : bool, optional + Defines if the urls are displayed as text above the images. force_b64 : bool, optional You can force conversion of images to base64 instead of reading them directly from filepaths with HTML. Do mind that using b64 conversion vs reading directly from filepath will be slower. @@ -360,7 +371,8 @@ def _create_imgs_grid( _create_img( x, width=img_width, label=y, grid_style_uuid=grid_style_uuid, - custom_text=text, force_b64=force_b64 + custom_text=text, show_url=show_url, + force_b64=force_b64 ) for x, y, text in zip( images[:max_images], labels[:max_images], diff --git a/ipyplot/_plotting.py b/ipyplot/_plotting.py index e060c1f..70c8b1c 100644 --- a/ipyplot/_plotting.py +++ b/ipyplot/_plotting.py @@ -18,6 +18,7 @@ def plot_class_tabs( max_imgs_per_tab: int = 30, img_width: int = 150, zoom_scale: float = 2.5, + show_url: bool = True, force_b64: bool = False, tabs_order: Sequence[str or int] = None): """ @@ -48,6 +49,8 @@ def plot_class_tabs( Scale for zoom-in-on-click feature. Best to keep between 1.0~5.0. Defaults to 2.5. + show_url : bool, optional + Defines if the urls are displayed as text above the images. force_b64 : bool, optional You can force conversion of images to base64 instead of reading them directly from filepaths with HTML. Do mind that using b64 conversion vs reading directly from filepath will be slower. @@ -75,6 +78,7 @@ def plot_class_tabs( max_imgs_per_tab=max_imgs_per_tab, img_width=img_width, zoom_scale=zoom_scale, + show_url=show_url, force_b64=force_b64, tabs_order=tabs_order) @@ -88,6 +92,7 @@ def plot_images( max_images: int = 30, img_width: int = 150, zoom_scale: float = 2.5, + show_url: bool = True, force_b64: bool = False): """ Simply displays images provided in `images` param in grid-like layout. @@ -118,6 +123,8 @@ def plot_images( Scale for zoom-in-on-click feature. Best to keep between 1.0~5.0. Defaults to 2.5. + show_url : bool, optional + Defines if the urls are displayed as text above the images. force_b64 : bool, optional You can force conversion of images to base64 instead of reading them directly from filepaths with HTML. Do mind that using b64 conversion vs reading directly from filepath will be slower. @@ -141,6 +148,7 @@ def plot_images( max_images=max_images, img_width=img_width, zoom_scale=zoom_scale, + show_url=show_url, force_b64=force_b64) _display_html(html) @@ -151,6 +159,7 @@ def plot_class_representations( labels: Sequence[str or int], img_width: int = 150, zoom_scale: float = 2.5, + show_url: bool = True, force_b64: bool = False, ignore_labels: Sequence[str or int] = None, labels_order: Sequence[str or int] = None): @@ -176,6 +185,8 @@ def plot_class_representations( Scale for zoom-in-on-click feature. Best to keep between 1.0~5.0. Defaults to 2.5. + show_url : bool, optional + Defines if the urls are displayed as text above the images. force_b64 : bool, optional You can force conversion of images to base64 instead of reading them directly from filepaths with HTML. Do mind that using b64 conversion vs reading directly from filepath will be slower. @@ -208,4 +219,5 @@ def plot_class_representations( max_images=len(images), img_width=img_width, zoom_scale=zoom_scale, + show_url=show_url, force_b64=force_b64) diff --git a/notebooks/gear-images-examples.ipynb b/notebooks/gear-images-examples.ipynb index 75b8006..1118701 100644 --- a/notebooks/gear-images-examples.ipynb +++ b/notebooks/gear-images-examples.ipynb @@ -18,7 +18,9 @@ " 5.1. [Ordering](#5.1-Ordering) \n", " 5.2. [Filtering](#5.2-Filtering) \n", " 5.3. [Ignoring labels](#5.3-Ignoring-labels) \n", - "6. [Force convert images to base64 strings](#6.-Force-convert-images-to-base64-strings) " + "6. [Additional plotting options](#6.-Additional-plotting-options) \n", + " 6.1. [Hiding image URLs](#6.1-Hiding-image-URLs) \n", + "7. [Force convert images to base64 strings](#7.-Force-convert-images-to-base64-strings) " ] }, { @@ -187,7 +189,7 @@ "end_time": "2020-10-20T19:38:11.226414Z", "start_time": "2020-10-20T19:38:11.165605Z" }, - "scrolled": false + "scrolled": true }, "outputs": [], "source": [ @@ -548,7 +550,32 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# 6. Force convert images to base64 strings\n", + "# 6. Additional plotting options\n", + "[[back to the top](#Table-of-content:)]\n", + "\n", + "## 6.1 Hiding image URLs\n", + "[[back to the top](#Table-of-content:)]\n", + "\n", + "By default IPyPlot will display URLs of plotted images. \n", + "If you don't want to show the URL on top of the image, you can set `show_url` to `False`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ipyplot.plot_class_representations(\n", + " images, labels, img_width=100, show_url=False\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 7. Force convert images to base64 strings\n", "[[back to the top](#Table-of-content:)]\n", "\n", "Since IPyPlot is using HTML under the hood, by design all the images provided as URL strings are just injected into HTML `img` elements as they are. For all the other image types (numpy.ndarray, PIL.Image) IPyPlot is performing a conversion to base64 strings which are then injected into HTML `img` elements. \n", @@ -573,9 +600,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:py36tf2gpu]", + "display_name": "Python [conda env:ipyplot_env] *", "language": "python", - "name": "conda-env-py36tf2gpu-py" + "name": "conda-env-ipyplot_env-py" }, "language_info": { "codemirror_mode": { @@ -587,7 +614,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.7.0" } }, "nbformat": 4, diff --git a/setup.py b/setup.py index c7da86d..d97224e 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="ipyplot", - version="1.1.0", + version="1.1.1", description="Simple package that leverages IPython and HTML for more efficient, reach and interactive plotting of images in Jupyter Notebooks", long_description=long_description, long_description_content_type="text/markdown", # This is important!