繁体   English   中英

在 Google Colab 平台上的 Jupyter Notebook 中显示/渲染 HTML 文件

[英]Display / Render an HTML file inside Jupyter Notebook on Google Colab platform

我正在使用 Google Colab 使用 Python 3.0 创建地图,并且我已经使用 Basemaps 完成了此操作。 我接下来尝试使用谷歌地图创建类似的地图。 我找到了两个 python 包,即 gmaps 和 gmplot。 似乎 gmaps 需要谷歌 API 但 gmplot 不需要,因此我使用的是 gmplot。

使用 gmplot,我可以创建一个文件“my_map.html”,如果我下载到本地桌面,我可以在浏览器中打开并正确查看 map。

但是,我想在笔记本 output 单元中查看 map 而无需下载到本地计算机。 下图是我尝试过的截图……没有错误,但也没有显示。
截屏

是否有一些像 %matplotlib inline 这样的命令需要执行才能在 output 单元格中显示文件的内容? 还是有更好的解决方案

尝试文件的完整路径: /content/my_map.html

代码应该是:

import IPython
IPython.display.HTML(filename='/path/to/your/filename')

请尝试以下代码:

from IPython.display import IFrame
IFrame(src='path/to/your/filename.html', width=900, height=600)

它确实对我有用。 :)

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

但是,它适用于标准网址:

IPython.display.HTML('<img src="https://github.com/blablabla/elmo_1.jpg?raw=1">') 

Colab 不会在 html 中显示本地文件图像。 您需要将 png 文件转换为 base64

encoded = base64.b64encode(open(png_file_name, "rb").read())

并使用显示图像

<img src="data:image/png;base64,{encoded.decode('utf-8')}" alt="" width="600" height="400">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM