cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to show an image in a notebook using html?

PaulHernandez
New Contributor II

Hi everyone,

I just learning how to personalize the databricks notebooks and would like to show a logo in a cell.

I installed the databricks cli and was able to upload the image file to the dbfs:

0693f000007OoKMAA0

I try to display it like this:

displayHTML("<img src ='files/Omnetric_logo.jpg'>")

Is not working:

0693f000007OoKNAA0

If I used the full URI of the image, same result:

displayHTML("<img src ='dbfs:/FileStore/files/Omnetric_logo.jpg'>")

I also tried with a "images" folder instead of "files", again, same result.

Could you please help me with that. Any comment will be appreciated.

Best,

Paul

1 ACCEPTED SOLUTION

Accepted Solutions

lee
Contributor

Hi @Paul Hernandez, please try adding a preceding slash before the

files
path:

displayHTML("<img src ='/files/Omnetric_logo.jpg'>")

Just to add as a design pattern - I like to have any static content contained in a markdown block. Most notebooks/workshops I've seen produced by Databricks also do this. That is, you would use the

%md
magic command at the top of your block and allow markdown to render the html, along with any other markdown you like:

%md
<img src ='/files/Omnetric_logo.jpg'>
# Hello Omnetric
## Overview
Blah Blah Blah
- list item 1
- list item 2
## Details
Blah blah

View solution in original post

7 REPLIES 7

shyam_9
Valued Contributor
Valued Contributor

Hi @Paul Hernandez,

Please use IPython library as in below,

from IPython.display import Image 
from IPython.core.display import HTML 
Image(url= "https://docs.databricks.com/_images/landing-aws.png")

Hi @Shyamprasad Miryala​ , thanks for your input!

I gave you a point because your code is working and helped me workaround the issue.

However, I still would like to know why the approach using html and images stored in dbfs is not working, therefore I could not mark your solution as the answer.

Best, Paul

lee
Contributor

Hi @Paul Hernandez, please try adding a preceding slash before the

files
path:

displayHTML("<img src ='/files/Omnetric_logo.jpg'>")

Just to add as a design pattern - I like to have any static content contained in a markdown block. Most notebooks/workshops I've seen produced by Databricks also do this. That is, you would use the

%md
magic command at the top of your block and allow markdown to render the html, along with any other markdown you like:

%md
<img src ='/files/Omnetric_logo.jpg'>
# Hello Omnetric
## Overview
Blah Blah Blah
- list item 1
- list item 2
## Details
Blah blah

PaulHernandez
New Contributor II

Hi @leedabee,

thanks! I used your snippet for my notebook and it works.

I also had to correct the url of the image. Since I uploaded the file in dbfs:/FileStore and then in a subfolder called images, the right url is:

/files/files/Omnetric_logo.jpg

Reviewing the documentation I noticed that dbfs:/FileStore is mapped to files

Best, Paul

NavneetTuteja
New Contributor II

Hi @Paul Hernandez​ , Did you find a resolution to displaying the images? Image('xyz.png') results with <IPython.core.display.Image object> and no image.

sean_owen
Honored Contributor II
Honored Contributor II

For small images, another cheeky alternative is to base64-encode the image bytes and construct a data:/ URI out of it, and make a hyperlink to that. It is 'inline' in the notebook then, but, wouldn't be a good idea for big images.

_robschaper
New Contributor II

@Paul Hernandez​ @Sean Owen​ @Navneet Tuteja​ I solved this after I also ran into the same issue where my notebook suddenly wouldn't show an image sitting on the driver in an accessible folder - no matter what I was trying in the notebook the display function only returned <IPython.core.display.Image object> as output.

After investigating, I found the file was returning as raw bytes instead of the formatted image (even though it was saved as a png it was raw bytes). Even after encoding for mimetypes manually as well as a few other format conversion attempts it still only rendered <IPython.core.display.Image object> and no image. Like huh?

My Solution (Verified using Community Edition):

Reviewing Ipython.display driver dependencies to see if the runtime itself was having issues was successful, as simply updating the cluster's runtime from [Databricks 8.3 (includes Apache Spark 3.1.2, Scala 2.12)] to [8.4 (includes Apache Spark 3.1.2, Scala 2.12)] which gave me a fresh install of Ipython.display in the notebook, the png/images referenced in the notebook started rendering as normal again. Go down to 8.2 did not fix the display render. The likely root cause appeared to be mimetype conversion, usually handled by Ipython.display in the call, so if you take a look at your file you may see raw bytes even though the file type is assigned (png in my case), which means the file's raw data isn't being formatted/converted properly and the notebook returns an object to render instead of a supported file format.

Summary: Refresh Ipython to a newer version by updating to a newer runtime may solve the problem, assuming everything in notebook is correct.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.