在Bokeh服务应用程序中绘制本地图像

时间:2018-03-01 18:02:00

标签: python bokeh

我正在尝试使用本地存储在应用程序ImageURL目录中的./static类来绘制png图像。在下面的代码中,当使用web url到同一图像时,它按预期工作,但所有创建本地URL的尝试都失败。此外,当运行基本相同的代码,输出到文件时,所有url案例都能正常工作。

散景服务 - 显示

import os
import numpy as np
from bokeh.plotting import curdoc
from bokeh.models import ColumnDataSource, Range1d, Plot
from bokeh.models.glyphs import ImageURL

url= "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png", # works
# url= "static/palm.png", # 404 GET /static/palm.png
# url=os.path.join(os.path.dirname(__file__), 'palm.png'), # 404 GET /Volumes/Home/Code/scratch/palm.png
# url='file://'+os.path.join(os.path.dirname(__file__), 'static', 'palm.png'),
N = 1
source = ColumnDataSource(dict(
    url = [url]*N,
    x1  = np.linspace(  1, 1, N),
    y1  = np.linspace(  1, 1, N),
    w1  = np.linspace( 253,  253, N),
    h1  = np.linspace( 409,  409, N),
))
p = Plot(
    title=None, 
    x_range=Range1d(start=0, end=500), 
    y_range=Range1d(start=0, end=500), 
    plot_width=500, 
    plot_height=500,
    h_symmetry=False, 
    v_symmetry=False, 
    min_border=0, 
    toolbar_location=None
)
p.add_glyph(source, ImageURL(url="url", x="x1", y="y1", w="w1", h="h1", anchor="bottom_left"))
curdoc().add_root(p)

python main.py

from bokeh.plotting import show, output_file
output_file("tbe.html")

import os
import numpy as np
from bokeh.plotting import curdoc
from bokeh.models import ColumnDataSource, Range1d, Plot
from bokeh.models.glyphs import ImageURL
# all work
url = "http://pngimg.com/uploads/palm_tree/palm_tree_PNG2504.png", 
url = "static/palm.png",
url = os.path.join(os.path.dirname(__file__), 'static', 'palm.png'),
url ='file://'+os.path.join(os.path.dirname(__file__), 'static', 'palm.png'),
N = 1
source = ColumnDataSource(dict(
    url = [url]*N,
    x1  = np.linspace(  1, 1, N),
    y1  = np.linspace(  1, 1, N),
    w1  = np.linspace( 253,  253, N),
    h1  = np.linspace( 409,  409, N),
))
p = Plot(
    title=None, 
    x_range=Range1d(start=0, end=500), 
    y_range=Range1d(start=0, end=500), 
    plot_width=500, 
    plot_height=500,
    h_symmetry=False, 
    v_symmetry=False, 
    min_border=0, 
    toolbar_location=None
)
p.add_glyph(source, ImageURL(url="url", x="x1", y="y1", w="w1", h="h1", anchor="bottom_left"))
show(p)

1 个答案:

答案 0 :(得分:0)

this answer中的建议,您必须从脚本上方的目录中启动bokeh serve(这意味着您必须将脚本重命名为main.py)。您可以使用:

url = os.path.join(os.path.basename(os.path.dirname(__file__)), "static", "palm.png")

例如:

your_folder/
  +main.py
  +static/
    +palm.png

bokeh serve your_folder --show的形式散景。图像的最终可到达地址将为http://localhost:5006/your_folder/static/palm.png