通过 运行 python 和 -m 选项在独立散景服务器中使用 image_url

Use image_url in a standalone bokeh server by running python with the -m option

这是我之前 的跟进。

文件结构如下所示。我必须使用顶级目录

中的 python -m bokeh_module.bokeh_sub_module 运行 脚本
.
├── other_module
│   ├── __init__.py
│   └── other_sub_module.py
├── bokeh_module
│   ├── __init__.py
│   ├── image.png # not showing
│   └── bokeh_sub_module.py
└── image.png # not showing either

bokeh_sub_module.py正在使用独立的散景服务器。但是,无论放置在何处,图像都不会显示。我错过了什么吗?感谢您的帮助。

from other_module import other_sub_module
import os
from bokeh.server.server import Server
from bokeh.layouts import column
from bokeh.plotting import figure, show

def make_document(doc):
    def update():
        pass

    # do something with other_sub_module
    p = figure(match_aspect=True)
    p.image_url( ['file://'+os.path.join(os.path.dirname(__file__), 'image.png')], 0, 0, 1, 1)
    doc.add_root(column(p, sizing_mode='stretch_both'))
    doc.add_periodic_callback(callback=update, period_milliseconds=1000)

apps = {'/': make_document}
server = Server(apps)
server.start()
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()

对 URL 使用 file:// 通常是行不通的。它只有在查看该应用程序的浏览器 运行 正在 运行 正在 Bokeh 服务器应用程序的同一台服务器上时才可能起作用。即便如此,我认为可能存在禁止从本地 URL 加载图像到 HTML canvas 的浏览器安全策略。实际上,图像 URL 需要引用实际的 http://https:// URL。如果您可以使用 directory-style Bokeh app,那么您可以将图像放在 static 子目录中,Bokeh 会自动为您提供图像。但是既然你提到必须通过执行带有嵌入式散景 Server 的模块来 运行 应用程序,我认为你的两个选择是:

  • 显式创建一个 BokehTornado 配置了 StaticFileHandler 的实例来提供图像,并将其作为 extra_patterns 参数传递给 Server
  • 将图像托管在不同的服务器或服务上,使它们可以从真实的 httphttps URL 中获得。