Python用户,学习一些基本的javascript:
我创建了一个简单的flask应用程序进行演示。(为了快速测试,我将这个例子放在Github上:https://github.com/robertdavidwest/flask_d3_example)
dependencies:
- python=3.7.1
- flask=1.0.2
- flask-cors=3.0.7
和https://d3js.org/d3.v5.min.js
应用程序:
^{pr2}$我正在从flask应用程序提供数据:
from flask import Flask, render_template, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/get-data")
def get_data():
return jsonify({"a": 1, "b": 2})
在服务器运行的情况下,如果我导航到http://127.0.0.1:5000/get-data,那么我就会看到数据。在
但是当我试图使用d3.json
从前端请求数据时,似乎什么都没有发生(我尝试过记录数据并写入div):
d3.json("http://127.0.0.1:5000/get-data",
function(d){
console.log(d);
document.getElementById("d3-write-here").innerHTML = d;
})
任何指导都将不胜感激。在
简单应用程序中所有代码的详细信息:
在索引.html公司名称:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset="utf-8" />
<title>Simple Example</title>
</head>
<body>
<h2>Nothing much here</h2>
<div id="d3-write-here"></div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="{{ url_for('static', filename='js/data.js') }}">
</script>
在 在
在运行.py公司名称:
from flask import Flask, render_template, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get-data")
def get_data():
return jsonify({"a": 1, "b": 2})
if __name__ == '__main__':
app.debug = True
app.run()
静态/js/数据.js公司名称:
d3.json("http://127.0.0.1:5000/get-data",
function(d){
console.log(d);
document.getElementById("d3-write-here").innerHTML = d;
})
在d3v5中,您需要为json调用使用不同的语法。在
尝试:
相关问题 更多 >
编程相关推荐