在 Google Colab Notebook 中提供 iframe:本地主机拒绝连接

duh*_*ime 5 javascript iframe jupyter google-colaboratory

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

from IPython.display import IFrame

IFrame(src='./output/index.html', width=700, height=600)
Run Code Online (Sandbox Code Playgroud)

但是,这会抛出localhost refused to connect

在此处输入图片说明

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

Bob*_*ith 0

这个内置示例笔记本提供了演示: https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=R8ZvCXC5A0wT

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

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)))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 谢谢你。我承认我不确定它是如何用于服务加载 js 资源的 html ——我是否只是读取我的 html 并将其传递给 `IPython.display.HTML()` 函数? (2认同)