【发布时间】:2015-02-16 12:06:39
【问题描述】:
我正在处理this d3JS example for creating a treemap。该示例使用flare.json 作为包含json 数据的文件。
然而,我希望这个 json 数据从我的 python 烧瓶应用程序中发送并加载到 javascript 中。
这是当前从flare.json 文件加载JSON 的方式:
d3.json("flare.json", function(error, root) {
var node = div.datum(root).selectAll(".node")
.data(treemap.nodes)
.enter().append("div")
.attr("class", "node")
.call(position)
.style("background", function(d) { return d.children ? color(d.name) : null; })
.text(function(d) { return d.children ? null : d.name; });
d3.selectAll("input").on("change", function change() {
var value = this.value === "count"
? function() { return 1; }
: function(d) { return d.size; };
node
.data(treemap.value(value).nodes)
.transition()
.duration(1500)
.call(position);
});
});
我想以某种方式修改它并实现这样的目标:
// Load the data.
var callback = function(data, error, root) {
var node = div.datum(root).selectAll(".node")
.data(treemap.nodes)
.enter().append("div")
.attr("class", "node")
.call(position)
.style("background", function(d) { return d.children ? color(d.name) : null; })
.text(function(d) { return d.children ? null : d.name; });
d3.selectAll("input").on("change", function change() {
var value = this.value === "count"
? function() { return 1; }
: function(d) { return d.size; };
node
.data(treemap.value(value).nodes)
.transition()
.duration(1500)
.call(position);
});
};
d3.json("/data", callback);
目前这对我不起作用。
在我的 app.py 文件中,这是我拥有的一些代码:
app = flask.Flask(__name__)
@app.route("/")
def index():
"""
When you request the root path, you'll get the index.html template.
"""
return flask.render_template("index.html")
@app.route("/data")
@app.route("/data/<int:ndata>")
def data():
d3js_chart_data = json.dumps(
{ //JSON here }
)
return d3js_chart_data
if __name__ == "__main__":
import os
port = 80
# Open a web browser pointing at the app.
os.system("open http://localhost:{0}".format(port))
# Set up the development server on port 8000.
app.debug = True
app.run(host='0.0.0.0', port=port)
【问题讨论】:
标签: javascript python json flask