I'm trying to write a really simply webapp with PythonAnywhere and Flask that has lets the user upload a text file, generates a csv file, then lets the user download the csv file. It doesn't have to be fancy, it only has to work. I have already written the program for generating the csv from a txt file on the drive.
Right now, my function opens the file on the drive with:
with open(INPUTFILE, "r") as fname:
and writes the csv with:
with open(OUTPUTFILE, 'w') as fname:
with INPUTFILE and OUTPUTFILE being filename strings.
Would it be better for me to handle the files as objects, returned by the flask/html somehow?
I don't know how to do this. How should I structure this program? How many HTML Templates do I need? I would prefer to work on the files wihthout saving them anywhere but if I have to save them to the PythonAnywhere directory, I could. How can I do that?
Handling file upload in Flask is very easy. It needs an HTML form with its enctype attribute set to 'multipart/form-data', posting the file to a URL. The URL handler fetches file from request. files[] object and saves it to the desired location.
PythonAnywhere dev here. This is a good question about Flask and web development in general rather than specific to our system, so I'll try to give a generic answer without anything specific to us :-)
There are a few things that I'd need to know to give a definitive answer to your question, so I'll start by listing the assumptions I'm making -- leave me a comment if I'm wrong with any of them and I'll update the answer appropriately.
If both of those are the case, then the best way to structure your Flask app would be to handle everything inside Flask. A code sample is worth a thousand words, so here's a simple one I put together that allows the user to upload a text file, runs it through a function called transform
(which is where the function from your conversion program would slot in -- mine just replaces =
with ,
throughout the file), and sends the results back to the browser. There's a live version of this app on PythonAnywhere here.
from flask import Flask, make_response, request app = Flask(__name__) def transform(text_file_contents): return text_file_contents.replace("=", ",") @app.route('/') def form(): return """ <html> <body> <h1>Transform a file demo</h1> <form action="/transform" method="post" enctype="multipart/form-data"> <input type="file" name="data_file" /> <input type="submit" /> </form> </body> </html> """ @app.route('/transform', methods=["POST"]) def transform_view(): request_file = request.files['data_file'] if not request_file: return "No file" file_contents = request_file.stream.read().decode("utf-8") result = transform(file_contents) response = make_response(result) response.headers["Content-Disposition"] = "attachment; filename=result.csv" return response
Regarding your other questions:
form
view into a template, but that's all.save(
filename)
method on the file
object that I'm using the stream
property of. But if your files are pretty small (as per my assumption above) then it probably makes more sense to process them in-memory like the code above does.I hope that all helps, and if you have any questions then just leave a comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With