从Flask API将参数传递给Bokeh autoload_server [英] Passing arguments to Bokeh autoload_server from Flask api
问题描述
我上周开始使用Bokeh,这是一个非常新的东西,我正尝试使用Flask API中嵌入的滑块和下拉列表创建交互式条形图,因此我为其创建了flask api,其显示了带有滑块和下拉菜单,但在更改滑块/下拉菜单值时不会动态更新图表.
I started using Bokeh last week, so very much new to it, I am trying to create an interactive bar chart using sliders and dropdowns embedded in Flask API, so I created the flask api for the same, its showing the chart with a slider and dropdown but its not dynamically updating the chart upon changing the slider/dropdown value.
然后进一步研究发现,我需要为交互部分运行一个单独的bokeh服务器,并从我的Flask api调用自动加载服务器.但是由于我的输入数据来自带有参数作为用户输入的外部API,因此我不确定如何将http post参数发送到bokeh服务器.
Then upon further research I found out that I need to have a separate bokeh server running for the interaction part and call the autoload server from my Flask api. But in that I am not sure how to send my http post params to the bokeh server as my input data is from an external API with params as user input.
script=autoload_server(model=None,app_path="/bokeh-sliders",url="http://localhost:5006")
return render_template('hello.html',script=script)
参考将URL参数从Flask发送到Bokeh服务器,因为我无法对此进行评论,因此似乎集成了该功能以将参数传递给自动加载服务器,但我似乎找不到任何文档.请帮我解决这个问题.
In reference to Sending URL parameter from Flask to a Bokeh server as I am not able to comment in it, it seems the feature has been integrated to pass arguments to the autoload server but I cant seem to find any documentation on it. Please help me in figuring this out.
请注意,请注意,如果不运行bokeh服务器,就不能仅在烧瓶api中进行诸如滑块,下拉菜单之类的交互操作.
On a side note, just to be sure, is it not possible to do interactions such as slider, dropdown, etc in just the flask api without running a bokeh server.
谢谢.
推荐答案
我遇到了同样的问题,无法添加与Flask的交互,并且走了同样的路. 此处还讨论了传递参数的问题.
I was having the same problems, not being able to add interactions with Flask, and went down the same road. The issue of passing arguments is also discussed here.
此功能已添加到Bokeh 0.12.7中,现在您可以使用arguments
参数将键/值的字典传递到应用脚本中:
The functionality has been added to Bokeh 0.12.7, and you can now pass a dictionary of key/values to include to the app script using the arguments
parameter:
script = server_document("https://example.com/myapp",
arguments={'foo': 'bar'})
请注意,server_document
是最近添加的,更简单的替代autoload_server
Note that server_document
is a recently-added, simpler replacement for autoload_server
对于0.12.7之前的版本,您还可以使用以下解决方法(贷记到github上的kevinsa5):
For versions before 0.12.7, you can also use the following workaround (credit goes to kevinsa5 on github):
@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
这篇关于从Flask API将参数传递给Bokeh autoload_server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!