TL/DR: 基于 Windows 的 Chrome 浏览器将毫无问题地加载基于 WSL2 的 html 文件,但不会加载嵌入的基于 WSL2 的图像,因为未知的解析问题不会在文件字符串中添加“Ubuntu”。通过网页检查手动更改图像文件字符串是一个临时修复,在生成 html 页面时将“Ubuntu”添加到文件字符串并不能解决问题。
情况如下: 处理数据后,我想使用 Jupyter Notebook 在网页中显示结果。 因此,我运行以下代码:
import pandas as pd
from IPython.core.display import HTML
# For showing images in Pandas DF
def path_to_image_html(path, windows = True):
return '<img src="'+ path + '" width="250" >'
df = pd.DataFrame(columns = ['img_guid', 'img_text', 'img_original', 'img_processed', 'img_filestring'])
for image_name in images.items():
img_processed = process(img_original)
data_output_dict = {
'img_guid': [img_guid],
'img_text': [text.rstrip()],
'img_original': [path_to_image_html(img_original)],
'img_processed': [path_to_image_html(img_processed)],
'img_filestring': [img_filestring]
}
df_temp = pd.DataFrame(data_output_dict)
df = pd.concat([df, df_temp], ignore_index=True)
html_output = HTML(df.to_html(escape=False))
with open('html_file.html', 'w') as f: f.write(html_output.data)
所以网页看起来就像我想要的那样,只是图像没有加载。它们的文件路径是
file://wsl.localhost/home/Oaknut/ProjectDir/img_dir/example_image.png
,而 html 的文件位置是 file://wsl.localhost/Ubuntu/home/Oaknut/ProjectDir/html_file.html
- 在浏览器中手动添加图像文件字符串 中缺少的
Ubuntu
位,使用 html 检查器 会使所述图像出现,但这当然是不是解决问题的方法。将行 if windows: path = 'Ubuntu' + path
添加到上面显示的 path_to_image_html
函数并不能解决问题,因为这将使图像(在重新编译的 html 文件中)具有文件位置 file://wsl.localhost/Ubuntu/home/Oaknut/ProjectDir/Ubuntu/home/Oaknut/ProjectDir/img_dir/example_image.png
。
img_processed
中存储的值是多少?您是否确认它是您所期望的?