Python Bokeh:AjaxDataSource,如何;“再利用”;javascript中的JSON数据
使用Bokeh 0.12.7并通过AjaxDataSource更新绘图。 这很有效 现在,我希望重用传递到绘图的数据,避免从服务器一次又一次地读取它们 我想访问从另一个JS组件(即仪表)传递到Bokeh javascript的JSON数据文件 在本例中,仪表随机更改,但我想将其设置为绘图中的最后一个值。唉,我对Javascript的了解是空的,这一点帮助都没有 这可能吗 这是我正在编写的精简代码Python Bokeh:AjaxDataSource,如何;“再利用”;javascript中的JSON数据,python,bokeh,Python,Bokeh,使用Bokeh 0.12.7并通过AjaxDataSource更新绘图。 这很有效 现在,我希望重用传递到绘图的数据,避免从服务器一次又一次地读取它们 我想访问从另一个JS组件(即仪表)传递到Bokeh javascript的JSON数据文件 在本例中,仪表随机更改,但我想将其设置为绘图中的最后一个值。唉,我对Javascript的了解是空的,这一点帮助都没有 这可能吗 这是我正在编写的精简代码 import numpy as np from math import sin from rando
import numpy as np
from math import sin
from random import random
from flask import Flask, jsonify, render_template_string
from bokeh.plotting import figure
from bokeh.embed import components
from bokeh.models.sources import AjaxDataSource
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<link href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.7.min.css" rel="stylesheet" type="text/css">
<script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.7.min.js"></script>
</head>
<body>
<script>
google.charts.load('current', {'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['value1', 0],
]);
var options = {
width: 200, height: 200,
minorTicks: 5
};
var chart = new google.visualization.Gauge(document.getElementById('gauge_div'));
chart.draw(data, options);
setInterval(
function() {
<!-- ***** Change the setValue HERE! ***** -->
data.setValue(0, 1, 40 + Math.round(60 * Math.random()));
chart.draw(data, options);
},
100);
}
</script>
<div id="gauge_div"></div>
{{ plot_script | safe }}
{{ plot_div.plot_1 | safe }}
</body>
</html>
"""
x = list(np.arange(0, 10, 0.1))
y = [sin(xx) for xx in x]
source = AjaxDataSource(data=dict(x=x, y=y),
data_url='http://localhost:5050/data',
polling_interval=100)
p = figure()
p.line('x', 'y', source=source, color="green")
p.toolbar.logo = None
p.toolbar_location = None
p.x_range.follow = "end"
p.x_range.follow_interval = 200
app = Flask(__name__)
@app.route("/")
def main_graph():
script, div = components({'plot_1': p})
html = render_template_string(html_template, plot_script=script, plot_div=div)
return html
@app.route('/data', methods=['GET', 'OPTIONS', 'POST'])
def hello_world():
global x, y
x = x[1:]
x.append(x[-1]+0.1)
y = y[1:]
y.append(sin(x[-1])+random()*.1)
return jsonify(x=x[-100:], y=y[-100:])
if __name__ == "__main__":
app.run(host="localhost", port=5050)
将numpy导入为np
从数学输入罪
从随机导入随机
从烧瓶导入烧瓶,jsonify,呈现模板字符串
从bokeh.plotting导入图形
从bokeh.embed导入组件
从bokeh.models.sources导入AjaxDataSource
html_template=“”
load('current',{'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
函数绘图图(){
var data=google.visualization.arrayToDataTable([
['Label','Value'],
['value1',0],
]);
变量选项={
宽度:200,高度:200,
米诺蒂克斯:5
};
var chart=new google.visualization.Gauge(document.getElementById('Gauge_div'));
图表绘制(数据、选项);
设定间隔(
函数(){
setValue(0,1,40+Math.round(60*Math.random());
图表绘制(数据、选项);
},
100);
}
{{plot_script | safe}}
{{plot_div.plot_1|safe}
"""
x=列表(np.arange(0,10,0.1))
y=[sin(xx)表示x中的xx]
source=AjaxDataSource(数据=dict(x=x,y=y),
数据http://localhost:5050/data',
轮询(间隔=100)
p=图()
p、 行('x','y',source=source,color=“green”)
p、 toolbar.logo=无
p、 工具栏位置=无
p、 x_range.follow=“结束”
p、 x_范围。跟随时间间隔=200
app=烧瓶(名称)
@附件路线(“/”)
def main_graph():
脚本,div=components({'plot_1':p})
html=呈现\模板\字符串(html \模板,绘图\脚本=脚本,绘图\ div=div)
返回html
@app.route('/data',methods=['GET','OPTIONS','POST'])
def hello_world():
全局x,y
x=x[1:]
x、 追加(x[-1]+0.1)
y=y[1:]
y、 附加(sin(x[-1])+random()*.1)
返回jsonify(x=x[-100:],y=y[-100:]
如果名称=“\uuuuu main\uuuuuuuu”:
app.run(host=“localhost”,port=5050)