简体   繁体   English

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

[英]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.

相关问题 如何使用 google colab 在 jupyter notebook 中显示 GIF? - How to display a GIF in jupyter notebook using google colab? 如何将 pyscipopt 导入 Google Colab(在 Jupyter Notebook 文件中)? - How to import pyscipopt to Google Colab (in the Jupyter Notebook file)? 如何在 Google Colab 中的笔记本中显示图像(如在 Anacondan Jupyter Notebook 中)? - How to display an image within the notebook in Google Colab (like in Anacondan Jupyter Notebook)? 如何从 Google Colab 笔记本内的 gmplot draw 命令查看 html 输出文件? - How can i view an html output file from a gmplot draw command inside of a Google Colab notebook? Google CoLab - 如何运行位于我的 CoLab 环境的“文件”选项卡(即 /content/)中的 jupyter notebook 文件 - Google CoLab - How to run a jupyter notebook file that is in the 'Files' tab (i.e. /content/) of my CoLab environment 递归错误(Jupyter Notebook/Google Colab) - Recursion Error(Jupyter Notebook/Google Colab) 在 Jupyter Notebook 中获取 Google Colab 自动完成功能 - Get Google Colab autocomplete in Jupyter Notebook 在 jupyter notebook 和 google colab 中使用.so - Using .so in a jupyter notebook and google colab 如何在 Google Colab 上实现这个 Jupyter Notebook? - How to implement this Jupyter notebook on Google Colab? 在 Django 应用程序中嵌入 jupyter notebook/google colab - embedding jupyter notebook/ google colab in Django app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM