简体   繁体   English

在Flask服务器上通过队列访问d3.json对象的问题

[英]Problems accessing d3.json object through queue on a flask server

At work I got an assigment to do a very simple report. 在工作中我得到了一份非常简单的报告。 In fact, so ridiculously simple that I decided to up the difficulty rating and make it a interactive chart report using dc.js, on a flask server. 实际上,这是如此简单,以至于我决定提高难度等级,并在烧瓶服务器上使用dc.js使其成为交互式图表报告。 The kicker here is that I have never touched Flask, dc.js, d3, crossfilter, or javascript for that matter... (Yes; stupid is, stupid does) 这里的缺点是我从来没有碰过Flask,dc.js,d3,crossfilter或javascript ...(是的;愚蠢的是,愚蠢的确实如此)

What made me go for it was a tutorial , that laid everything out so understandably, and tempting. 我之所以喜欢它是一个教程 ,它以易于理解和诱人的方式安排了所有内容。

So now I am here, sweating due to the time pressure, trying to get data generated at the back with pandas, over to the front-end. 所以现在我在这里,由于时间紧迫,出汗了,试图让大熊猫从后端获取数据,直到前端。 The code from the tutorial is as follows; 本教程中的代码如下:

queue()
.defer(d3.json, "/donorschoose/projects")
.defer(d3.json, "static/geojson/us-states.json")
.await(makeGraphs);

function makeGraphs(error, projectsJson, statesJson) {

//Clean projectsJson data
var donorschooseProjects = projectsJson;
var dateFormat = d3.time.format("%Y-%m-%d");
donorschooseProjects.forEach(function(d) {
    d["date_posted"] = dateFormat.parse(d["date_posted"]);
    d["date_posted"].setDate(1);
    d["total_donations"] = +d["total_donations"];
});

Following the monkey see monkey do principle (the monkey has been using the last couple of hours googling, trying to get an understanding of what is happening, but to no avail), my code is as follows; 遵循猴子看到猴子做事的原则(猴子已经使用了过去几个小时的谷歌搜索,试图了解正在发生的事情,但无济于事),我的代码如下;

queue()
.defer(d3.json, "/salgshisto")
.await(makeGraphs);

function makeGraphs(error, salgsData) {

var salgsTransaksjonene = salgsData;
var dateFormat = d3.time.format("%Y-%m-%d %H:%M:%S");
salgsTransaksjonene.forEach(function(d) {
    d["InvoiceDate"] = dateFormat.parse(d["InvoiceDate"]);
    d["InvoiceDate"].setDate(1);
    d["TotalAmount"] = +d["TotalAmount"];
});

This leads to the following error in the safari console; 在safari控制台中导致以下错误;

TypeError: undefined is not a function (evaluating 'salgsTransaksjonene.forEach')

And the last piece of info; 还有最后一条信息; The flask app.py file, has a flask的app.py文件有一个

@app.route("/salgshisto")
def betHisto():
    ....
    creating FO = pandas.DataFrame object 
    ....

   return FO.to_json(orient='index', date_format='iso', date_unit='s', force_ascii=False)

Also, the console shows an object, which is the .json data, I can see that since I can span out the object's object elements, and see the info from the pandas Dataframe. 同样,控制台显示了一个对象,即.json数据,由于可以扩展对象的对象元素,因此可以看到它,并可以从pandas Dataframe中查看信息。 So; 所以; The info makes it across, but it does not look like it gets to the makeGraphs function and the salgsTransaksjonene selector. 该信息可以解决问题,但看起来并没有达到makeGraphs函数和salgsTransaksjonene选择器。

I just cannot understand what the typeError is, so if anyone has any idea, I would be very grateful! 我只是不明白typeError是什么,所以如果有人有什么想法,我将非常感激!

Edit 2: 编辑2:

@Mark thank you for replying!! @Mark感谢您的答复! I implemented the d3.json() you supplied like so; 我像这样实现了您提供的d3.json()

var salgsData = d3.json("/salgshisto", function(error, json) { if (error) return console.warn(error);}); 

function makeGraphs(salgsData) {.....

There are now, no error messages either in the Flask server, or the chrome / safari console. 现在,Flask服务器或chrome / safari控制台中没有错误消息。 So the code seems to be executing now. 因此,代码现在似乎正在执行。 But it is not behaving as expected. 但是,它的行为不符合预期。 The rest of the code does not return what I expect - a rowChart: 其余代码未返回我期望的结果-rowChart:

var salgsData = d3.json("/salgshisto", function(error, json) { if (error) return console.warn(error);}); 

function makeGraphs(salgsData) {

//  var salgsData = d3.json.loads("/salgshisto");

//klargjoer salgsData
var salgsTransaksjonene = salgsData;
var dateFormat = d3.time.format("%Y-%m-%dT%H:%M:%SZ");
salgsTransaksjonene.forEach(function(d) {
    d["InvoiceDate"] = dateFormat.parse(d["InvoiceDate"]);
    d["InvoiceDate"].setDate(1);
    d["TotalAmount"] = +d["TotalAmount"];
});

//Create a Crossfilter instance
var ndx = crossfilter(salgsTransaksjonene);

//Define Dimensions
var kundeDim = ndx.dimension(function(d) { return d["CustomerName"]; });
//var betingelseDim = ndx.dimension(function(d) { return d["Name"]; }); 

//Calculate metrics
var OmsetningKunder = kundeDim.group().reduceSum(function(fact) { return fact.TotalAmount;});

//Charts
var resourceTypeChart = dc.rowChart("#topp-20-row-chart");

resourceTypeChart
    .width(300)
    .height(600)
    .dimension(kundeDim)
    .group(OmsetningKunder)
    .xAxis().ticks(4);

dc.renderAll();

}; };

I can see from the server log that "/salgshisto" is being called. 从服务器日志中可以看到正在调用"/salgshisto"

The corresponding bootstrap keen.io template has the correct <div id="topp-20-row-chart"></div> added. 相应的引导keen.io模板已添加正确的<div id="topp-20-row-chart"></div>

Concerning the .json data. 关于.json数据。 I am wondering how to transmit it to you. 我想知道如何将其传播给您。 I cannot get the same info, laying out the object, in the javascript console anymore. 我无法再在javascript控制台中获得相同的信息,从而布置对象。 Best I can do I guess is copying the response from running ".../salgshisto" url in the browser. 我想我能做的最好的就是从浏览器中运行“ ... / salgshisto” URL复制响应。 That returns the entire list. 那将返回整个列表。 The only problem then is that the text is uff-8 which I cannot get it printed as. 唯一的问题是文本为uff-8 ,我无法将其打印为。 So here is the first two entries, as unicode; 因此,这是前两个条目,以unicode表示; {"0":{"InvoiceDate":"2011-04-04T00:00:00Z","CustomerName":"M\e\c\h\a\n\i\c\a\ \A\S\","Name":"Netto per 30 dager","TotalAmount":14689.74},"1":{"InvoiceDate":"2011-04-07T00:00:00Z","CustomerName":"H\e\l\l\a\n\d\ \M\e\k\.\ \V\e\r\k\s\t\e\d\","Name":"Netto per 30 dager","TotalAmount":3705.0},...}

@Mark Thank you so much for all your help. @Mark非常感谢您的所有帮助。 It turned out that you were right; 原来你是对的。 It was the .json that was mal-formed. 格式错误的是.json。 For all that come after me using pandas, please note that the correct orient for .json, at least as far as d3 and cross filter is concerned; 对于使用熊猫的所有后续工作,请注意,.json的正确方向,至少在d3和交叉过滤器方面; orient='records' is correct. orient='records'是正确的。 not orient='index' not orient='index'

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

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