通过使用-m选项运行python,在独立的bokeh服务器中使用image_url [英] Use image_url in a standalone bokeh server by running python with the -m option

查看:113
本文介绍了通过使用-m选项运行python,在独立的bokeh服务器中使用image_url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我先前的问题的后续操作.

This is a follow up of my previous question.

文件的结构如下所示.我必须使用顶级目录中的python -m bokeh_module.bokeh_sub_module运行脚本

The structure of the files is shown below. I have to run the scripts using python -m bokeh_module.bokeh_sub_module from the top directory

.
├── 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正在使用独立的bokeh服务器.但是,无论放置在哪里,该图像都不会显示.有什么我想念的吗?谢谢您的帮助.

The bokeh_sub_module.py is using the standalone bokeh server. However the image will not show no matter where it is placed. Is there something I missed? Thank you for any help.

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()

推荐答案

使用file://作为URL通常无法正常工作.仅当查看该应用程序的浏览器在运行Bokeh服务器应用程序的同一台服务器上运行时,它才可能工作.即使那样,我认为可能会有一些浏览器安全策略禁止将图像从本地URL加载到HTML画布上.实际上,图像URL需要引用实际的http://https:// URL.如果您可以使用目录样式的Bokeh应用,则可以将图像放在static子目录中,Bokeh会自动为您提供图像.但是,由于您提到必须通过执行带有嵌入式Bokeh Server的模块来运行应用程序,因此我认为您的两个选择是:

Using file:// for the URL is not going to work in general. It could only possibly work if the browser viewing the app is running on the same server that is running the Bokeh server app. Even then I think there may be browser security policies that prohibit loading images from local URLs on to an HTML canvas. Realistically, the image URL needs to refer to an actual http:// or https:// URL. If you could use a directory-style Bokeh app then you could put the image in a static subdirectory, and Bokeh would automatically serve the image for you. But since you mention having to run the app by executing a module with an embedded Bokeh Server, I think your two options are:

  • 明确创建一个BokehTornado实例,该实例配置为 投放图片,并将其作为extra_patterns参数传递给Server
  • 将图像托管在其他服务器或服务上,这些服务器或服务可以使它们从真实的httphttps URL可用.
  • Explicitly create a BokehTornado instance configured with StaticFileHandler to serve the images, and pass that as the extra_patterns argument to Server
  • Host the images on a different server or service that can make them available from real http or https URLs.

这篇关于通过使用-m选项运行python,在独立的bokeh服务器中使用image_url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆