简体   繁体   English

使用 Flask Chart.js 绘制实时数据

[英]Plotting Real Time data using Flask Chart.js

I am sending some data to my Flask server from another application using HTTP requests.我正在使用 HTTP 请求从另一个应用程序向我的 Flask 服务器发送一些数据。 I need to plot and display the data on my webpage.我需要 plot 并在我的网页上显示数据。 The graph needs to update whenever new data comes in. The plot is a real-time plot. So far data transfer is working properly but I am unable to plot the data.每当有新数据进入时,图表都需要更新。plot 是实时的 plot。到目前为止,数据传输工作正常,但我无法 plot 数据。 This is my python code这是我的 python 代码

@app.route('/', methods=["GET", "POST"])
def main():
   
return render_template('index.html')


@app.route('/data', methods=["GET", "POST"])
def data():
    # Using Static data
    #data = [time() * 1000, random() * 100]


    data = [request.json["time"],request.json["value"]]
    print(data)

    response = make_response(json.dumps(data))
    response.content_type = 'application/json'
    return response

Using data inside the app, the plot works but when I try to get data from server, it does not work.使用应用程序内部的数据,plot 有效,但当我尝试从服务器获取数据时,它不起作用。

The script used for plotting is given below用于绘图的脚本如下所示

 <script> var chart; function requestData() { // Ajax call to get the Data from Flask var requests = $.get('/data'); var tm = requests.done(function (result) { var series = chart.series[0], shift = series.data.length > 20; // add the point chart.series[0].addPoint(result, true, shift); // call it again after one second setTimeout(requestData, 2000); }); } $(document).ready(function() { chart = new Highcharts.Chart({ chart: { renderTo: 'data-container', defaultSeriesType: 'spline', events: { load: requestData } }, title: { text: 'Live random data' }, xAxis: { type: 'datetime', tickPixelInterval: 150, maxZoom: 20 * 1000 }, yAxis: { minPadding: 0.2, maxPadding: 0.2, title: { text: 'Value', margin: 80 } }, series: [{ name: 'Random data', data: [] }] }); }); </script>

I keep getting the error "NoneType" object is not subscript-able.我不断收到错误消息“NoneType”object 不可下标。 Any help would be much appreciated.任何帮助将非常感激。 Thanks in advance.提前致谢。

Try this:试试这个:

@app.route('/data', methods=["GET", "POST"])
def data():
    data = [time() * 1000, random() * 100]
    response = make_response(json.dumps(data))
    response.content_type = 'application/json'
    return response

I use it in my app and it works.我在我的应用程序中使用它并且它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM