mardi 21 juillet 2020

Upload, manipulate, and save files in python flask

I am trying to create a simple web application with Flask. The goal is for a user to be able to upload a file (a .csv in my case), which then gets processed, and in the end the processed file should be saved to the user's computer.

So far, I am able to choose files from the browser and upload it. I save it as a Python object and I can also directly save it to my personal Downloads folder. However, I do not understand how I can make the download path dynamic. If I deploy the app, everybody should be able to have the processed file directly downloaded to their individual folder. How does that work?

Below is my code:

from flask import Flask, render_template, request, redirect
import os

app = Flask(__name__)


@app.route("/")
def home():
    return render_template("index.html")


@app.route("/upload-csv", methods=["GET", "POST"])
def uplaod_csv():

    if request.method == "POST":

        if  request.files:

            csv = request.files["csv"]

            path = "this should be dynamic"
            csv.save(os.path.join(path, csv.filename))

            return redirect(request.url)

    return render_template("index.html")

if __name__ == "__main__":
    app.run(debug=True)

I am rather new to web development, any kind of help is greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire