I am working on a challenge in which I have to create a Flask app to train a Machine Learning model using a csv file. This csv file is sent to my Flask app using the following curl command:
curl \
--silent \
--fail \
--data-binary @train.csv \
--header "Content-Type: text/csv"
This curl command is pre-defined and so I can't change it to suit my needs. Also I don't have a front-end component with a form, only the Flask app.
I am trying to open the train.csv file in Flask but I have been unsucessful. I have tried many different ways but nothing works. How can you open this file in Flask using this command? Specifically a csv file sent as binary data with no variable name like so [email protected]
Here is a snippet of some code I have tried to open the file:
# Imports
import os
from flask import Flask, jsonify, request, make_response, session
import numpy as np
import pandas as pd
app = Flask(__name__)
@app.route('/genres/train', methods=['POST'])
def train_model():
# Check if the POST request has the file part
#if 'file' not in request.files:
#return jsonify({'error': 'No file part'})
# Get uploaded file
#file = request.files['file']
file = request.get_data()
# If user does not select file, raise error
if file.filename == '':
return jsonify({'error': 'No selected file'})
# If a file has been selected and it has csv extension then it can proceed
if file and file.filename.split('.', 1)[1].lower() == 'csv':
#with open('test.war', 'rb') as f:
data = pd.read_csv(file, delimiter=',')
Note that request.files['file'] was used by me to test my Flask app using Postman to check that it worked, which it did.