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?任何指针将不胜感激!
这个内置示例笔记本提供了演示: 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)
| 归档时间: |
|
| 查看次数: |
2966 次 |
| 最近记录: |