Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

javascript - 在Google Colab Notebook中提供iframe服务:本地主机拒绝连接(Serving an Iframe in Google Colab Notebook: localhost refused to connect)

I am trying to serve some HTML from a Google Colab notebook using the following:(我正在尝试使用以下方法从Google Colab笔记本提供一些HTML:)

from IPython.display import IFrame IFrame(src='./output/index.html', width=700, height=600) However, this throws localhost refused to connect :(但是,这抛出localhost refused to connect :) 在此处输入图片说明 Does anyone know how I can serve the html in index.html (which must load javascript) inside the Colab notebook?(有谁知道我如何在Colab笔记本中为index.html(必须加载javascript)中的html提供服务?) Any pointers would be hugely appreciated!(任何指针将不胜感激!)   ask by duhaime translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This built-in example notebook gives a demo: https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=R8ZvCXC5A0wT(这个内置的示例笔记本提供了一个演示: https : //colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=R8ZvCXC5A0wT)

Reproducing the example here of serving content from the backend:(在此处重现从后端提供内容的示例:) 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))) 在此处输入图片说明

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...