我上周开始使用Bokeh,这是一个非常新的东西,我试图使用嵌入在Flask API中的滑块和下拉列表创建交互式条形图,因此我为它创建了flask api,它使用滑块和下拉菜单,但是在更改滑块/下拉菜单值时不会动态更新图表。

然后,经过进一步研究,我发现需要为交互部分运行一个单独的bokeh服务器,并从我的Flask api调用自动加载服务器。但是由于我的输入数据来自带有参数作为用户输入的外部API,因此我不确定如何将http post参数发送到bokeh服务器。

script=autoload_server(model=None,app_path="/bokeh-sliders",url="http://localhost:5006")
return render_template('hello.html',script=script)


关于我无法在其中引用的Sending URL parameter from Flask to a Bokeh server,似乎已集成了该功能以将参数传递给自动加载服务器,但我似乎找不到任何文档。请帮我解决这个问题。

顺便说一句,请确保,如果不运行bokeh服务器,就不能仅在烧瓶api中进行诸如滑块,下拉菜单之类的交互。

提前致谢。

最佳答案

我遇到了同样的问题,无法添加与Flask的交互,因此走了同样的路。
The issue of passing arguments is also discussed here.

该功能已添加到Bokeh 0.12.7中,现在您可以使用arguments参数传递键/值的字典以包含到应用程序脚本中:

script = server_document("https://example.com/myapp",
                         arguments={'foo': 'bar'})


请注意,server_document是最近添加的,更简单的autoload_server替代品



对于0.12.7之前的版本,您还可以使用以下解决方法(贷记到github上的kevinsa5):

@app.route('/amped')
def amped():
    script = autoload_server(model = None, app_path="/amped")
    # `script` is a string that looks like this (the first character is a newline):
    """
<script
    src="http://localhost:5006/amped/autoload.js?bokeh-autoload-element=6b813263-05df-45a5-bd91-e25c5e53c020"
    id="6b813263-05df-45a5-bd91-e25c5e53c020"
    data-bokeh-model-id=""
    data-bokeh-doc-id=""
></script>
"""
    # so to add on the necessary parameters, we have to insert them manually.  hopefully we won't need to urlencode anything.
    # note that request.args = a MultiDict, so be careful of duplicate params
    # http://werkzeug.pocoo.org/docs/0.11/datastructures/#werkzeug.datastructures.MultiDict

    script_list = script.split("\n")
    script_list[2] = script_list[2][:-1]
    for key in request.args:
        script_list[2] = script_list[2] + "&{}={}".format(key, request.args[key])
    script_list[2] = script_list[2] + '"'
    script = "\n".join(script_list)
    return render_template("amped.html", script = script)


这使您可以使用

doc.session_context.request.arguments

09-20 22:39