Jupyter notebook: hyperlink to locally stored pdf docs stopped working in Chrome

人走茶凉 提交于 2021-01-03 06:04:21

问题


I have a large number of Jupyter Notebooks and in many of them I have hyperlinks to locally stored pdf documents. A short while ago the links stopped working on my iMac in Chrome. When clicking on a link, a new tab is opened with the proper address, but the page is just black. When I do this on my MacBook with exactly the same Jupyter Notebook, it works ok. I have basically the same environment on my MacBook as on my iMac. Everything is up to date. I am using Anaconda for Python and Jupyter.

When looking at the console in Chrome when this issue happens on my iMac I get the following error message: Failed to load 'http://localhost:8888/files/Cookbooks/Git%20%26%20GitHub/books/Pro_Git.pdf' as a plugin, because the frame into which the plugin is loading is sandboxed.

When I open Jupyter notebook on either Safari or Firefox, the hyperlinks are working fine. Chrome is my default browser and I would like to have this fixed. I am not very technical and I have not been able to find any solutions on the internet. Help would be very much appreciated.


回答1:


An easy fix is to use an <embed> element instead of an <iframe>.

You can define a helper function in your notebook:

from IPython.core.display import HTML
def pdf(url):
    return HTML('<embed src="%s" type="application/pdf" width="100%%" height="600px" />' % url)

To display your PDF, use the following:

pdf('./data-ref/pluto_datadictionary.pdf')

Note: the pdf file should be stored next to your jupyter notebook

The original iframe issue is related to the Content Security Policy applied to iframes by Chrome.



来源:https://stackoverflow.com/questions/50997580/jupyter-notebook-hyperlink-to-locally-stored-pdf-docs-stopped-working-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!