2

代码来源来自这里

我有两个版本的代码,test_a.html不显示图像和test_b.html显示图像。我希望第一个版本可以工作,但似乎def效果不佳。

如何使第一个版本与图像一起使用?

test_a.html 输出[不显示图像]:

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

test_b.html 输出[显示图像]:

<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>

代码:

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)

操作系统:jupyter notebook 中的 Mac (mojave) python 3

4

0 回答 0