Last active
May 18, 2019 00:30
-
-
Save M-Anwar/b57a97ccc9f5ec2af503f94da282b384 to your computer and use it in GitHub Desktop.
Display Pandas Dataframe with Image Columns Inline within Jupyter Notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: