Skip to content

Instantly share code, notes, and snippets.

@M-Anwar
Last active May 18, 2019 00:30
Show Gist options
  • Save M-Anwar/b57a97ccc9f5ec2af503f94da282b384 to your computer and use it in GitHub Desktop.
Save M-Anwar/b57a97ccc9f5ec2af503f94da282b384 to your computer and use it in GitHub Desktop.
Display Pandas Dataframe with Image Columns Inline within Jupyter Notebooks
from IPython.core import display as ICD
from IPython.display import HTML
from PIL import Image
from io import BytesIO
from IPython.display import HTML
import pandas as pd
import base64
###
# Pandas Dataframe Visualization Library
#
# Takes a dataframe and renders out any image links directly within the notebook.
# The visualiztion supports both external http resources as well as internal image files.
###
### UTIL FUNCTIONS ###
def get_thumbnail(path, image_size):
i = Image.open(path)
i.thumbnail((image_size, image_size), Image.LANCZOS)
return i
def image_base64(im, img_width):
if isinstance(im, str):
im = get_thumbnail(im, img_width)
with BytesIO() as buffer:
im.save(buffer, 'jpeg')
return base64.b64encode(buffer.getvalue()).decode()
### IMAGE FORMATTERS ###
def file_image_formatter(img_width):
""" Formatter for rendering out local image files """
def img_formatter(im):
return '<img src="data:image/jpeg;base64,{}">'.format(image_base64(im,img_width))
return img_formatter
def html_image_formatter(img_width):
""" Formatter for rendering out remote http images """
def img_formatter(im):
return '<img src="{}" width="{}">'.format(im, img_width)
return img_formatter
### LIBRARY FUNCTIONS ###
def display(df, image_cols=None, image_formatter = html_image_formatter, image_width=200):
"""
Displays a Dataframe with any image columns rendered out to the notebook.
Keyword Arguments:
df -- Pandas Dataframe to render out
image_cols -- A dict, list, string or none with the image column(s) within the dataframe.
The string provides a single column. The list provides multiple image columns.
The dict provides multiple columns in the key with a image_formatter in the value.
If none, will simply render out the dataframe, can be called multiple times to
render many to the notebook.
image_formatter -- The image_formatter to use when rendering the dataframe (not used if dict in image_cols)
Default Value: html_image_formatter
image_width -- The image width to render out (height is scaled to aspect ratio of image).
Default Value: 200
"""
if not image_cols:
ICD.display(df)
return
pd.set_option('display.max_colwidth', -1)
if(type(image_cols)==list):
fmters = { col: image_formatter(image_width) for col in image_cols }
elif(type(image_cols)==dict):
fmters = { col: fmter for (col,fmter) in image_cols.items() }
else:
fmters = { image_cols: image_formatter(image_width) }
ICD.display(HTML(df.to_html(formatters=fmters, escape=False)))
pd.reset_option('max_colwidth')
@M-Anwar
Copy link
Author

M-Anwar commented Nov 29, 2018

Usage:

from pandaViz import display, show, file_image_formatter, http_image_formatter

display(data, "image_url")                         # DataFrame with image columns in `image_url` (http link)
display(data, "image_url", file_image_formatter)   # DataFrame with image columns in `image_url` (local filesystem path)
display(data, ["image_url", "image_url2"])         # DataFrame with multiple image columns
display(data,                                      # DataFrame with both http links and local filesystem images
     {
        "image_url": http_image_formatter, 
        "image_url2": file_image_formatter
    }
)
display(data, "image_url", image_width=300)        # Control the size of the displayed images.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment