I am looking on guidance for the best method or tools complete this task. I need to build an application that replaces all negative numeric values in a csv file with 0 and then return the modified CSV(I have already done this in Python)
But it needs to be accessible via a HTTP api interface where a user can upload a CSV file and then provide the user with the CSV output. Does anyone know the best set of tools or even the best programming languages to do these last two steps in?
Thanks for any help
CodePudding user response:
Kinda large topic to answer it here, but i would recommend flask or fastapi to expose service in python
CodePudding user response:
The question could do with some more specifics but if you just want a web UI to upload a file, do the processing and then provide the output I'd suggest starting here:
https://flask.palletsprojects.com/en/2.1.x/patterns/fileuploads/
Something like this (essentially from this page) would get the file uploaded to a directory and ready for processing with your existing code. I should stress, this is not a REST API but could form the basis for you to make it one.
import os
from flask import Flask, request, redirect, url_for, flash, send_from_directory
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = set(['csv'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
'''
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == '__main__':
app.run(port=8000)
Then in the above "uploaded_file"
files route you can use your code to process the data.