简体   繁体   English

如何使用 fetch 调用从 Flask 服务器提供 csv 文件或检索 csv 数据?

[英]How to serve a csv file or retrieve csv data from a Flask server using fetch calls?

I'm trying to use a fetch call from a JavaScript front end in order to download a csv file from a server/api written in Flask.我正在尝试使用来自 JavaScript 前端的 fetch 调用,以便从用 Flask 编写的服务器/api 下载 csv 文件。 But, after reading it seems like there's no current way to download a csv file using fetch calls so instead I'm trying to convert csv file data into json to be sent as the data instead in the response.但是,在阅读之后,似乎没有当前的方法可以使用 fetch 调用下载 csv 文件,因此我尝试将 csv 文件数据转换为 json 以作为响应发送数据。 However, when I do a fetch call I get:但是,当我进行 fetch 调用时,我得到:

SyntaxError: Unexpected token S in JSON at position 0

Here is the flask server I've written so far which involves servicing api calls to / and converting csv file data into json:这是迄今为止我编写的 flask 服务器,其中涉及将 api 调用服务到 / 并将 csv 文件数据转换为 csv:FZ 文件数据到 api 调用

from flask import Flask, request, send_file, jsonify
from flask_cors import CORS, cross_origin
import csv

app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/', methods=["GET"])
@cross_origin()
def transform_view():
    json_ = request.json
    new = pd.read_csv('grades.csv')
    json_vector = new.transform(json_)
    query = pd.DataFrame(json_vector)
    prediction = regr.predict(query)
    data = {'prediction': list({{prediction}})}
    return jsonify(data)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5001, debug=True)

And the fetch call:以及 fetch 调用:

fetch(
     "http://localhost:5001/",
     {
      method: "GET",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
    }
  ).then((response) => {
    // Check if the request is 200
    if (response.ok) {
      return response.json();
    }
}) 

Any help would be great任何帮助都会很棒

Have a look at send_from_directory in the official documentation.查看官方文档中的send_from_directory It should help you if the only purpose is to fetch the csv from flask server.如果唯一的目的是从 flask 服务器获取 csv,它应该会对您有所帮助。 For any modifications to the csv you can continue to use pandas library.对于 csv 的任何修改,您可以继续使用 pandas 库。

@app.route('/uploads/<path:filename>')
def download_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                           filename, as_attachment=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Angular2从数据库中获取数据并导出到csv文件 - How to fetch data from database and export to csv file using Angular2 如何将CSV文件中的数据导入到服务器端的Meteor集合中 - How to import data from CSV file into Meteor collection at server side 如何使用流从服务器下载CSV文件 - How to download csv file from server using stream 使用JS获取csv文件并在图表Js中显示数据 - fetch csv file by using JS and display the data in chart Js 从gmail检索csv附件文件并将数据放在谷歌电子表格中 - Retrieve csv attachment file from gmail and place the data in a google spreadsheet 如何使用jquery将表中的数据导出到csv文件 - How to export data from table to csv file using jquery 如何使用 JavaScript 从 *.CSV 文件中读取数据? - How to read data From *.CSV file using JavaScript? 如何使用 javascript 获取屏幕上当前存在的表格数据并将其转换为 csv 文件 - How to fetch table data which is currently present on screen using javascript and convert it into csv file 从数组中检索值,然后使用MongoDB将其存储在csv文件中 - Retrieve the values from array and store it in csv file using MongoDB 从Javascript对象中的CSV中检索已解析的数据(使用Papa Parse) - Retrieve parsed data from CSV in Javascript object (using Papa Parse)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM