问题
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