由散景項目的核心開發人員之一編輯以下信息未回答上述問題。如下所述,通過使用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中),但可能會很麻煩。
非常感謝!這個例子對我來說的問題是,它只是用新的參數創建一個新的情節 - 如果我正在尋找交互性,那不是那種類型,其次,它不涉及真正的散景小程序 - 讓我們使用sliders_app.py例如:這是一個真正的散景應用程序,它直接在散景服務器上工作得很好,並且它具有我期待的那種「真正的交互性」。我找不到嵌入此小程序的方法。 – lakerz
嗯。看看[散景服務器代碼](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)不要使用任何散景控件,而應該自己重新實現控件(例如使用jQuery)。工作正常,但如果散景本身提供相同的功能,則感覺有點不必要。 (2)只需用iFrame嵌入散景小程序url即可。看起來它工作正常,但不知怎的,我有一個瘋狂的猜測,即iFrames的使用並不是一種很好的風格..但是我爲網絡做了一些事情已經有好幾年了,也許現在變了? – lakerz