Categories
Data Engineering Neo4j Pandas

Neo4j + Pandas = Inline Image

Sometimes, we experience image URL in Neo4j data, and as a Data Engineer / Data Scientist, we would like to see the image. The query result set in Neo4j doesn’t has the capabilities to display.

Lets assume, we have a dataset, containing the URL of images from amazon.com as an attribute.

Jupyter notebook, CSV and loader script can be found at Github

My choice of python library to interact with Neo4j is py2neo. As always, pandas is most popular library for any data engineer / data scientist. To display inline images in Pandas dataframe, we need to import HTML method from IPython.display module.

import pandas as pd
from py2neo.database import Graph
import requests
import sys
from IPython.display import HTML

Connect to the graph database using

# initialize Graph context
graph = Graph('bolt://<ip_address>:7687', auth=('neo4j', '<password>'), name="<db_name>")

Create a dataframe

books = pd.DataFrame(graph.run("match (b:book) return b.isbn,b.title,b.yop,b.url").to_table(),columns=['isbn','title','yop','url']) 

books.head()

A small code-snippet function is created to get the relative path of the URL.

def path_to_image_html(path):
    return '<img src="'+ path + '"  >'

Now, we will utilized the HTML method from IPython to display the dataframe with images.

pd.set_option('display.max_colwidth', None)
display(HTML(books.to_html(escape=False ,formatters=dict(url=path_to_image_html))))

The resulting dataframe with images directly grabbed from the internet, not from local storage.

 126 total views,  1 views today