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

☆樱花仙子☆ 提交于 2020-07-23 06:39:31

问题


This is a follow up of my previous question.

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

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

回答1:


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:

  • 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.


来源:https://stackoverflow.com/questions/62628332/use-image-url-in-a-standalone-bokeh-server-by-running-python-with-the-m-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!