Ответ 1
EDIT одним из основных разработчиков проекта Bokeh. Информация ниже не отвечает на вопрос выше. Категорически невозможно внедрить приложение Bokeh с помощью bokeh.embed.components
, как описано ниже. components
способен только встраивать автономные документы (т.е. не запускаться на сервере Bokeh)
пример внедрения боке с флягой присутствует на 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
бок о бок и загружать код bokeh таким образом (на стороне сервера или через JS или iframe), но это может быть неприятно.