2015-04-29 46 views
16

我正在拼命地將工作散景小程序嵌入到燒瓶中,並且找不到合適的方法來執行此操作。我瀏覽了所有示例,但找不到包含更新數據的最佳示例(最佳示例:sliders_applet)。在燒瓶中嵌入一個散景應用程序

如果我沒有弄錯,我確實需要使用散景服務器才能更改數據(使用滑塊等)。啓動小程序這樣的工作,例如:

bokeh-server --script sliders_app.py 

但我不能找到合適的,或至少是工作方式嵌入sliders_app進入瓶中。由於應該可以使用多個小程序,因此在啓動散景服務器時也指定一個小程序似乎並不乾淨。

我很樂意爲您提供任何幫助 - 散景看起來像一個偉大的爲我的工具。

回答

9

由散景項目的核心開發人員之一編輯以下信息未回答上述問題。如下所述,通過使用bokeh.embed.components嵌入散景應用是絕對不可能的。 components僅能夠嵌入獨立documenents(即那些不是一個散景服務器上運行的)的


一種example of embedding bokeh with flask存在於the bokeh github repo

import flask 

from bokeh.embed import components 
from bokeh.plotting import figure 
from bokeh.resources import INLINE 
from bokeh.templates import RESOURCES 
from bokeh.util.string import encode_utf8 

app = flask.Flask(__name__) 

colors = { 
    'Black': '#000000', 
    'Red': '#FF0000', 
    'Green': '#00FF00', 
    'Blue': '#0000FF', 
} 


def getitem(obj, item, default): 
    if item not in obj: 
     return default 
    else: 
     return obj[item] 


@app.route("/") 
def polynomial(): 
    """ Very simple embedding of a polynomial chart""" 
    # Grab the inputs arguments from the URL 
    # This is automated by the button 
    args = flask.request.args 

    # Get all the form arguments in the url with defaults 
    color = colors[getitem(args, 'color', 'Black')] 
    _from = int(getitem(args, '_from', 0)) 
    to = int(getitem(args, 'to', 10)) 

    # Create a polynomial line graph 
    x = list(range(_from, to + 1)) 
    fig = figure(title="Polynomial") 
    fig.line(x, [i ** 2 for i in x], color=color, line_width=2) 

    # Configure resources to include BokehJS inline in the document. 
    # For more details see: 
    # http://bokeh.pydata.org/en/latest/docs/reference/resources_embedding.html#module-bokeh.resources 
    plot_resources = RESOURCES.render(
     js_raw=INLINE.js_raw, 
     css_raw=INLINE.css_raw, 
     js_files=INLINE.js_files, 
     css_files=INLINE.css_files, 
    ) 

    # For more details see: 
    # http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components 
    script, div = components(fig, INLINE) 
    html = flask.render_template(
     'embed.html', 
     plot_script=script, plot_div=div, plot_resources=plot_resources, 
     color=color, _from=_from, to=to 
    ) 
    return encode_utf8(html) 


def main(): 
    app.debug = True 
    app.run() 

if __name__ == "__main__": 
    main() 

另一個想法是運行bokeh-server和你flask Web應用程序並排側,並加載背景虛化代碼這種方式(服務器端或通過JS或iframe中),但可能會很麻煩。

+1

非常感謝!這個例子對我來說的問題是,它只是用新的參數創建一個新的情節 - 如果我正在尋找交互性,那不是那種類型,其次,它不涉及真正的散景小程序 - 讓我們使用sliders_app.py例如:這是一個真正的散景應用程序,它直接在散景服務器上工作得很好,並且它具有我期待的那種「真正的交互性」。我找不到嵌入此小程序的方法。 – lakerz

+1

嗯。看看[散景服務器代碼](https://github.com/bokeh/bokeh/blob/master/bokeh/server/start.py)我看到他們做了很多東西。 Flask應用程序在[app.py](https://github.com/bokeh/bokeh/blob/master/bokeh/server/app.py)結尾處實例化,但僅在以後(在start.py中)修改。也許你可以在''start.py''中重複使用這些代碼,並將你的路由添加到''app''實例中? – halflings

+1

看起來很有趣,我會研究它,猜想它需要我一段時間才能完全理解。到目前爲止,我發現了另外兩個選項: (1)不要使用任何散景控件,而應該自己重新實現控件(例如使用jQuery)。工作正常,但如果散景本身提供相同的功能,則感覺有點不必要。 (2)只需用iFrame嵌入散景小程序url即可。看起來它工作正常,但不知怎的,我有一個瘋狂的猜測,即iFrames的使用並不是一種很好的風格..但是我爲網絡做了一些事情已經有好幾年了,也許現在變了? – lakerz

9

其他答案沒有描述如何嵌入一個Bokeh服務器應用程序(它使用components來嵌入一個獨立的Bokeh文檔)。

首先,你可以看到很多在託管活生生的實例:https://demo.bokehplots.com/

用於嵌入應用程序有兩種常用的選項:

後者通常是這樣使用的:

script = autoload_server(model=None, 
         app_path="/apps/slider", 
         url="https://demo.bokehplots.com") 

這將返回類似於下面的一個<script>標籤,你可以把你的燒瓶HTML響應,無論你希望顯示的應用程序:

<script 
    src="https://demo.bokehplots.com/apps/slider/autoload.js?bokeh-autoload-element=c5c9bdb5-40e8-46a2-9bf0-40a9d396ce97" 
    id="c5c9bdb5-40e8-46a2-9bf0-40a9d396ce97" 
    data-bokeh-model-id="" 
    data-bokeh-doc-id="" 
></script> 

最後,需要注意的是很重要的默認情況下,Bokeh服務器選擇相當保守的網絡配置。您需要啓動帶有--allow-websocket-origin命令行選項的Bokeh服務器,將其設置爲您將嵌入散景應用程序的任何主機。

+3

你有兩個完整的工作示例來說明iframe和'autoload_server'嗎? –

+0

我不確定你在問什麼。 IFrame實際上就是'