best way to transfer data from flask to d3

[亡魂溺海] 提交于 2019-12-06 14:23:27

问题


I have csv tables, processed in Pandas, that I would like to serve from Flask to the browser so that I can use d3 to display the information. How do I transfer the data from Flask to the browser?


回答1:


Use the to_csv method of the Pandas dataframe, and return that from your flask server:

# app.py
@app.route('/my/data/entpoint')
def get_d3_data():
    df = pandas.DataFrame(...) # Constructed however you need it
    return df.to_csv()

Then on the front end, direct d3.tsv to your endpoint above:

<!-- page.html -->
<script>
d3.tsv("/my/data/endpoint", function(data) {
  console.log(data); // Do something with data
});
</script>


来源:https://stackoverflow.com/questions/39881571/best-way-to-transfer-data-from-flask-to-d3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!