Skip to content

Commit

Permalink
Added option to hide url text from the images (#26)
Browse files Browse the repository at this point in the history
* 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 <karol.zak@hotmail.com>
  • Loading branch information
Maarten-vr and karolzak authored Dec 11, 2020
1 parent 7b88cac commit b0ccb78
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion ipyplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 14 additions & 2 deletions ipyplot/_html_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 += '</div>'
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 += '<h4 style="font-size: 9px; padding-left: 10px; padding-right: 10px; width: 95%%; word-wrap: break-word; white-space: normal;">%s</h4>' % (image) # NOQA E501
if show_url:
img_html += '<h4 style="font-size: 9px; padding-left: 10px; padding-right: 10px; width: 95%%; word-wrap: break-word; white-space: normal;">%s</h4>' % (image) # NOQA E501
if not force_b64:
use_b64 = False
img_html += '<img src="%s"/>' % image
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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],
Expand Down
12 changes: 12 additions & 0 deletions ipyplot/_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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.
Expand Down Expand Up @@ -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)
39 changes: 33 additions & 6 deletions notebooks/gear-images-examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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) "
]
},
{
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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",
Expand All @@ -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": {
Expand All @@ -587,7 +614,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.7.0"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit b0ccb78

Please # to comment.