gpt4 book ai didi

Flask + Bokeh AjaxDataSource

转载 作者:行者123 更新时间:2023-12-03 15:26:16 25 4
gpt4 key购买 nike

挣扎于 Flask + Bokeh AjaxDataSource:

我有一个返回 json 数据的函数:

@app.route("/data", methods=['POST'])
def get_x():
global x, y
x = x + 0.1
y = math.sin(x)
return flask.jsonify(x=[x], y=[y])

我可以将它与 Bokeh AjaxDataSource 一起使用来创建流图:
source = AjaxDataSource(data_url="http://localhost:5000/data", polling_interval=1000, mode='append')
p = figure()
p.line('x', 'y', source=source)
show(p)

但是,当我尝试将其嵌入到 flask 页面中时,AjaxDataSource 不会查询服务器。该图无法无错误地渲染。请注意,如果我使用静态绘图而不是 AjaxDataSource,它的绘图效果很好。以下是相关代码:
template = Template('''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Streaming Example</title>
{{ js_resources }}
{{ css_resources }}
</head>
<body>
{{ plot_div }}
{{ plot_script }}
</body>
</html>
''')

@app.route("/")
def simple():
streaming=True
source = AjaxDataSource(data_url="http://localhost:5000/data",
polling_interval=1000, mode='append')

fig = figure(title="Streaming Example")
fig.line( 'x', 'y', source=source)

js_resources = INLINE.render_js()
css_resources = INLINE.render_css()

script, div = components(fig, INLINE)

html = template.render(
plot_script=script,
plot_div=div,
js_resources=js_resources,
css_resources=css_resources
)

return encode_utf8(html)

如果有人有任何想法,我将不胜感激。

布赖恩

最佳答案

像 OP 一样,我也想将 AJAX 与 Bokeh 和 Flask 一起使用。但是,不是使用 AjaxDataSource 连续从服务器流式传输数据。我只想在用户与网页上的输入交互时从服务器获取新数据。为此,我使用了 bigreddot 的 answer作为基础,更改了AjaxDataSourceColumnDataSource并在 CustomJS 中添加了一个 jQuery AJAX 调用(以下示例是使用 Python 3.6.4、Flask 1.0.2 和 Bokeh 0.13.0 创建的):

import json

from flask import Flask, jsonify, request
from jinja2 import Template
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, CustomJS, Select
from bokeh.embed import components
from bokeh.resources import INLINE
from bokeh.layouts import column
from bokeh.util.string import encode_utf8

app = Flask(__name__)

N_DATAPOINTS = 20
DEFAULT_VARIABLE = 'bar'
MY_DATABASE = {
'foo': [i**1 for i in range(N_DATAPOINTS)],
'bar': [i**2 for i in range(N_DATAPOINTS)],
'baz': [i**3 for i in range(N_DATAPOINTS)]}


@app.route("/get_new_data", methods=['POST'])
def get_new_data():
app.logger.info(
"Browser sent the following via AJAX: %s", json.dumps(request.form))
variable_to_return = request.form['please_return_data_of_this_variable']
return jsonify({variable_to_return: MY_DATABASE[variable_to_return]})


SIMPLE_HTML_TEMPLATE = Template('''
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
{{ js_resources }}
{{ css_resources }}
</head>
<body>
{{ plot_div }}
{{ plot_script }}
</body>
</html>
''')


@app.route("/")
def simple():
x = range(N_DATAPOINTS)
y = MY_DATABASE[DEFAULT_VARIABLE]

source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(title="Flask + JQuery AJAX in Bokeh CustomJS")
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=dict(source=source), code="""
var selected_value = cb_obj.value;
var plot_data = source.data;

jQuery.ajax({
type: 'POST',
url: '/get_new_data',
data: {"please_return_data_of_this_variable": selected_value},
dataType: 'json',
success: function (json_from_server) {
// alert(JSON.stringify(json_from_server));
plot_data.y = json_from_server[selected_value];
source.change.emit();
},
error: function() {
alert("Oh no, something went wrong. Search for an error " +
"message in Flask log and browser developer tools.");
}
});
""")

select = Select(title="Select variable to visualize",
value=DEFAULT_VARIABLE,
options=list(MY_DATABASE.keys()),
callback=callback)

layout = column(select, plot)
script, div = components(layout)
html = SIMPLE_HTML_TEMPLATE.render(
plot_script=script,
plot_div=div,
js_resources=INLINE.render_js(),
css_resources=INLINE.render_css())

return encode_utf8(html)

app.run(debug=True, host="127.0.0.1", port=5002)

关于Flask + Bokeh AjaxDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com