2

The source of the code comes from here

I have two versions of code, test_a.html not showing images and test_b.html showing images.I want the 1st version to work but seems like the def is not working well.

How can I make the 1st version work with images?

test_a.html output [not showing images]:

<td>https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825</td>

test_b.html output [shows images]:

<td><img src="https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825" width="60" <="" td="">
    </td>

code:

import pandas as pd
from IPython.core.display import HTML
import imgkit
import pdfkit

df = pd.DataFrame([['A231', 'Book', 5, 3, 150], 
                   ['M441', 'Magic Staff', 10, 7, 200]],
                   columns = ['Code', 'Name', 'Price', 'Net', 'Sales'])

################1st version
images = ['https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825',
          'https://i.pinimg.com/originals/d9/5c/9b/d95c9ba809aa9dd4cb519a225af40f2b.png'] 

df['image'] = images
def path_to_image_html(path):
    return '<img src="'+ path + '" width="60" >'

pd.set_option('display.max_colwidth', -1)
HTML(df.to_html(escape=False ,formatters=dict(image=path_to_image_html)))
df.to_html('test_a.html', escape=False)

################2nd version
images = ['<img src="https://vignette.wikia.nocookie.net/2007scape/images/7/7a/Mage%27s_book_detail.png/revision/latest?cb=20180310083825" width="60"',
          '<img src="https://i.pinimg.com/originals/d9/5c/9b/d95c9ba809aa9dd4cb519a225af40f2b.png" width="60"'] 

df['image'] = images
pd.set_option('display.max_colwidth', -1)
HTML(df.to_html(escape=False))
df.to_html('test_b.html', escape=False)

OS:Mac (mojave) python 3 in jupyter notebook

Hachi
  • 179
  • 14

0 Answers0