2016-12-27 92 views
0

我使用了散景網站上給出的嵌入式.html示例:http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html。注意我正在使用散景12.3。這些圖很好地顯示,但文本呈現爲腳本函數的確切輸出 - 包括'{'和'\ n'字符。散景劇情標記呈現問題

散射功能:

from bokeh.plotting import figure 
from bokeh.models import Range1d 
from bokeh.embed import components 

def scatter(): 
    # create some data 
    x1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
    y1 = [0, 8, 2, 4, 6, 9, 5, 6, 25, 28, 4, 7] 
    x2 = [2, 5, 7, 15, 18, 19, 25, 28, 9, 10, 4] 
    y2 = [2, 4, 6, 9, 15, 18, 0, 8, 2, 25, 28] 
    x3 = [0, 1, 0, 8, 2, 4, 6, 9, 7, 8, 9] 
    y3 = [0, 8, 4, 6, 9, 15, 18, 19, 19, 25, 28] 

    # select the tools we want 
    TOOLS="pan,wheel_zoom,box_zoom,reset,save" 

    # the red and blue graphs will share this data range 
    xr1 = Range1d(start=0, end=30) 
    yr1 = Range1d(start=0, end=30) 

    # only the green will use this data range 
    xr2 = Range1d(start=0, end=30) 
    yr2 = Range1d(start=0, end=30) 

    # build our figures 
    p1 = figure(x_range=xr1, y_range=yr1, tools=TOOLS, plot_width=300, plot_height=300) 
    p1.scatter(x1, y1, size=12, color="red", alpha=0.5) 

    p2 = figure(x_range=xr1, y_range=yr1, tools=TOOLS, plot_width=300, plot_height=300) 
    p2.scatter(x2, y2, size=12, color="blue", alpha=0.5) 

    p3 = figure(x_range=xr2, y_range=yr2, tools=TOOLS, plot_width=300, plot_height=300) 
    p3.scatter(x3, y3, size=12, color="green", alpha=0.5) 

    # plots can be a single Bokeh Model, a list/tuple, or even a dictionary 
    plots = {'Red': p1, 'Blue': p2, 'Green': p3} 

    script, div = components(plots) 
    return script, div 

我燒瓶代碼:

script, div = scatter() 
return self.render_template('bokeh_example.html', script=script, div=div) 

bokeh_example.html:

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.css" type="text/css" /> 
     <script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.js"></script> 
     {{ script | safe }} 
</head> 
<body> 
    <div class='bokeh'> 
     <h1>Scatter Example</h1> 
     {{ div | safe }} 
    </div> 
</body> 
</html> 

的曲線顯示效果細膩,但股利文本呈現爲文字:

{'Red': '\n #this text displays instead of just the string 'Red' 
\n    #this displays on next line in smaller font 
#plot displays fine here 
\n    #this text displays after the plot instead of creating a blank line. 

任何線索?

+0

我有一個(可能的)線索,但是你遺漏了最相關的部分,這是你實際傳遞給Bokeh的'components'功能的東西。請使用該信息更新您的問題。 – bigreddot

+0

@bigreddot我已經添加了分散功能。乾杯 –

回答

1

你傳入地塊的字典components

plots = {'Red': p1, 'Blue': p2, 'Green': p3} 

script, div = components(plots) 
return script, div 

這意味着(每documentation)的結果是不是一個單一的腳本和一個單一的股利。相反,它是一個腳本和字典映射你的原來的名稱,以多個div:

components({"Red": p1, "Blue": p2, "Green": p3}) 
#=> (script, {"Red": p1_div, "Blue": p2_div, "Green": p3_div}) 

現在你正試圖將自身字典模板到HTML中。據推測,Jinja只是在字典上調用str將其變成一個字符串,瀏覽器不知道該怎麼做。您需要分別在components返回的字典中爲每個div製作一個模板。

對於一個適當的更新的模板,可能看起來像:

script, divs = scatter() # notice plural: divS 
return self.render_template(
    'bokeh_example.html', 
    script=script, 
    div_red=divs['Red'], 
    div_blue=divs['Blue'], 
    div_green=divs['Green'], 
) 

或者你也可能會更新模板超過divs迭代直接使用一些Jinja2的的能力用於遍歷是集合模板參數。