[英]Display / Render an HTML file inside Jupyter Notebook on Google Colab platform
I am using Google Colab to work create maps with Python 3.0 and I have done so using Basemaps.我正在使用 Google Colab 使用 Python 3.0 创建地图,并且我已经使用 Basemaps 完成了此操作。 I am next trying to create similar maps using Google Maps.
我接下来尝试使用谷歌地图创建类似的地图。 There are two python packages that I have located namely gmaps and gmplot.
我找到了两个 python 包,即 gmaps 和 gmplot。 It seems as if gmaps requires a google API but gmplot does not hence I am using gmplot.
似乎 gmaps 需要谷歌 API 但 gmplot 不需要,因此我使用的是 gmplot。
With gmplot, I am able to create a file 'my_map.html' which if I download to my local desktop, I can open in a browser and see the map correctly.使用 gmplot,我可以创建一个文件“my_map.html”,如果我下载到本地桌面,我可以在浏览器中打开并正确查看 map。
However I would like to see the map in the notebook output cell without downloading to the local machine.但是,我想在笔记本 output 单元中查看 map 而无需下载到本地计算机。 The following image is a screenshot of what I have tried... there is no error, but no display either.
下图是我尝试过的截图……没有错误,但也没有显示。
is there some command like %matplotlib inline that i need execute to be able to display the contents of the file in the output cell?是否有一些像 %matplotlib inline 这样的命令需要执行才能在 output 单元格中显示文件的内容? or is there a better solution
还是有更好的解决方案
Try full path to your file: /content/my_map.html
尝试文件的完整路径:
/content/my_map.html
The code should be:代码应该是:
import IPython
IPython.display.HTML(filename='/path/to/your/filename')
Please Try Following code:请尝试以下代码:
from IPython.display import IFrame
IFrame(src='path/to/your/filename.html', width=900, height=600)
It did work for me.它确实对我有用。 :)
:)
This solution is not working for me if the html contains images located on colab or in your (mapped) google-drive:如果 html 包含位于 colab 或(映射的)google-drive 中的图像,则此解决方案对我不起作用:
IPython.display.HTML('<img src="/content/elmo_1.jpg">') # can't find image
IPython.display.HTML('<img src="/content/gdrive/My Drive/elmo_1.jpg">') # can't find image
IPython.display.HTML('<img src="/content/gdrive/My%20Drive/elmo_1.jpg">') # can't find image
It works, however, with standard url:但是,它适用于标准网址:
IPython.display.HTML('<img src="https://github.com/blablabla/elmo_1.jpg?raw=1">')
Colab won't display local file images in html. Colab 不会在 html 中显示本地文件图像。 You need to convert a png file to base64
您需要将 png 文件转换为 base64
encoded = base64.b64encode(open(png_file_name, "rb").read())
And show image using并使用显示图像
<img src="data:image/png;base64,{encoded.decode('utf-8')}" alt="" width="600" height="400">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.