Answer the question
In order to leave comments, you need to log in
Python Flask how to return json data in template and accept it in d3.js?
View that returns json
import pymysql, json
from flask import Flask, request, render_template, jsonify
app = Flask(__name__)
@app.route('/get_data')
def get_data():
if request.method == 'GET':
#Подключаемся к MYSQL
conn = pymysql.connect(host='localhost', port=3306,
user='root', passwd='nemate666', db='data_mapping')
cursor = conn.cursor(pymysql.cursors.DictCursor)
#Вытаскиваем данные и сериализуем в json
query_data = cursor.execute("SELECT project_name, countryname, lendprojectcost FROM data_load")
for row in cursor:
data_json = row
return jsonify(data_json)
return render_template('index.html')
#Закрываем соединение
cursor.close()
conn.close()
if __name__=="__main__":
app.run(host='0.0.0.0', port=5000, debug='true')
d3.json("/get_data", function(error, data_json){
data_json.map(function(d) {
return d.project_name
})
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question