Python 波基0.12.14+;Flask:AjaxDataSource的库损坏还是新语法?
安装最新的Bokeh库(0.12.14)后,我发现一个奇怪的行为 在下面的示例中,根据Ajax请求随机创建一组180个值,并每秒钟可视化一次 版本0.12.13工作正常,但0.12.14会生成JS错误:Python 波基0.12.14+;Flask:AjaxDataSource的库损坏还是新语法?,python,ajax,flask,bokeh,Python,Ajax,Flask,Bokeh,安装最新的Bokeh库(0.12.14)后,我发现一个奇怪的行为 在下面的示例中,根据Ajax请求随机创建一组180个值,并每秒钟可视化一次 版本0.12.13工作正常,但0.12.14会生成JS错误: bokeh-0.12.14.min.js:1 undefined http://0.0.0.0:5011/undefined 404 (NOT FOUND) 如您所见,端点是http://0.0.0.0:5011/undefined而不是http://0.0.0.0:5011/data。 直接
bokeh-0.12.14.min.js:1 undefined http://0.0.0.0:5011/undefined 404 (NOT FOUND)
如您所见,端点是http://0.0.0.0:5011/undefined
而不是http://0.0.0.0:5011/data
。
直接调用时,http://0.0.0.0:5011/data
并返回JSON数据。
看起来Ajax端点已经丢失这是一个bug还是库中记录的语法更改?
如果你想玩它,别忘了在BOKEH_版本中更改版本:
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, render_template_string
from bokeh.embed import components
from bokeh.models.sources import AjaxDataSource
from bokeh.plotting import figure
import pandas as pd
import numpy as np
#########################
BOKEH_VERSION = "0.12.13" # <--- DON'T FORGET TO CHANGE HERE IF USING ANOTHER VERSION
#########################
html_template="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
<link href="http://cdn.bokeh.org/bokeh/release/bokeh-{bokeh_ver}.min.css" rel="stylesheet" type="text/css">
<script src="http://cdn.bokeh.org/bokeh/release/bokeh-{bokeh_ver}.min.js"></script>
</head>
""".format(bokeh_ver=BOKEH_VERSION)
html_template += """
<body>
<h1>Working in Bokeh 0.12.13<br>and not working in 0.12.14</h1>
<div id="example_plot"></div>
{{ plot_div.plot_grid | safe}}
{{ plot_script | safe}}
</body>
</html>
"""
# ------------------------------------------------------------------------------
app = Flask(__name__)
# ------------------------------------------------------------------------------
class ExamplePlot:
def __init__(self, app):
self._data_source = None
self.data_source_update() # create the first set of dummy data for self.data_source
self._ajax_data_source = AjaxDataSource(
data_url="/data",
data=self._data_source,
polling_interval = 1000,
)
self.figure = figure()
self.figure.line(
source=self._ajax_data_source,
x="index",
y="value",
)
def data_source_update(self):
self._data_source = {
"index": range(180),
"value": list(np.random.randint(1,100,size=180))
}
# ------------------------------------------------------------------------------
example_plot = ExamplePlot(app)
@app.route("/")
def main_page():
script, div = components({'plot_grid': example_plot.figure})
html = render_template_string(
html_template,
plot_script = script,
plot_div = div,
)
return html
@app.route("/data", methods=['GET', 'OPTIONS', 'POST'])
def serve():
example_plot.data_source_update()
return jsonify(
index=example_plot._data_source["index"],
value=example_plot._data_source["value"],
)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5011)
#-*-编码:utf-8-*-
从烧瓶导入烧瓶,jsonify,呈现模板字符串
从bokeh.embed导入组件
从bokeh.models.sources导入AjaxDataSource
从bokeh.plotting导入图形
作为pd进口熊猫
将numpy作为np导入
#########################
BOKEH_VERSION=“0.12.13”#这绝对是一个bug。最近对如何处理选择进行了重构,并错过了对AjaxDataSource的必要更新。对于0.12.15