Javascript 为Google Colab笔记本中的Iframe提供服务:localhost拒绝连接

Javascript 为Google Colab笔记本中的Iframe提供服务:localhost拒绝连接,javascript,iframe,jupyter,google-colaboratory,Javascript,Iframe,Jupyter,Google Colaboratory,我正在尝试使用以下内容从Google Colab笔记本提供一些HTML: from IPython.display import IFrame IFrame(src='./output/index.html', width=700, height=600) 但是,这会引发本地主机拒绝连接: 有人知道我如何在Colab笔记本中提供index.html(必须加载javascript)中的html吗?任何指点都将不胜感激 此内置示例笔记本提供了一个演示: 此处重现从后端提供内容的示例: imp

我正在尝试使用以下内容从Google Colab笔记本提供一些HTML:

from IPython.display import IFrame

IFrame(src='./output/index.html', width=700, height=600)
但是,这会引发本地主机拒绝连接:


有人知道我如何在Colab笔记本中提供index.html(必须加载javascript)中的html吗?任何指点都将不胜感激

此内置示例笔记本提供了一个演示:

此处重现从后端提供内容的示例:

import portpicker
import threading
import socket
import IPython

from six.moves import socketserver
from six.moves import SimpleHTTPServer

class V6Server(socketserver.TCPServer):
  address_family = socket.AF_INET6

class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  def do_GET(self):
    self.send_response(200)
    # If the response should not be cached in the notebook for
    # offline access:
    # self.send_header('x-colab-notebook-cache-control', 'no-cache')
    self.end_headers()
    self.wfile.write(b'''
      document.querySelector('#output-area').appendChild(document.createTextNode('Script result!'));
    ''')

port = portpicker.pick_unused_port()

def server_entry():
    httpd = V6Server(('::', port), Handler)
    # Handle a single request then exit the thread.
    httpd.serve_forever()

thread = threading.Thread(target=server_entry)
thread.start()

# Display some HTML referencing the resource.
display(IPython.display.HTML('<script src="https://localhost:{port}/"></script>'.format(port=port)))
导入端口选择器
导入线程
导入套接字
进口伊皮顿
从six.moves导入socketserver
从six.moves导入SimpleHTTPServer
V6Server类(socketserver.TCPServer):
地址\u系列=socket.AF\u INET6
类处理程序(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_获得(自我):
自我发送_响应(200)
#如果响应不应缓存在笔记本中
#脱机访问:
#self.send_头('x-colab-notebook-cache-control','no cache')
self.end_头()
self.wfile.write(b“”)
document.querySelector(“#输出区域”).appendChild(document.createTextNode('Script result!'));
''')
端口=端口选择器。拾取未使用的端口()
def服务器_条目():
httpd=V6Server((“:”,端口),处理程序)
#处理单个请求,然后退出线程。
httpd.永远为你服务()
线程=线程。线程(目标=服务器\u项)
thread.start()
#显示一些引用资源的HTML。
显示(IPython.display.HTML(“”.format(port=port)))

您可以从路径
/nbextensions/
提供内容,该路径映射到
/usr/local/share/jupyter/nbextensions

所以你可以把内容放在那里

!ln-s/usr/local/share/jupyter/nbextensions/nbextensions
%cd/nbextensions
!wget-qhttps://upload.wikimedia.org/wikipedia/commons/3/37/Youtube.svg
然后为形象服务

%%html
<img src=/nbextensions/Youtube.svg>
%%%html
我不能让它和IFrame一起工作,心想。我不知道为什么


这里有一个例子。

谢谢你。我承认,我不确定这是如何用于服务加载js资源的html的——我是否会读取我的html并将其传递给
IPython.display.html()
函数?2021-04-14T10:43这不起作用。也许有一次,但不是今天。