mirror of
https://github.com/ilri/csv-metadata-quality-web.git
synced 2024-11-30 02:18:20 +01:00
Add simple HTML template with file upload
Code taken from the following blog posts: - https://pythonbasics.org/flask-upload-file - https://blog.miguelgrinberg.com/post/handling-file-uploads-with-flask
This commit is contained in:
parent
482cd25182
commit
81b5206e97
30
main.py
30
main.py
@ -1,13 +1,39 @@
|
|||||||
from flask import Flask
|
import os
|
||||||
|
|
||||||
|
from flask import Flask, abort, redirect, render_template, request, url_for
|
||||||
|
from werkzeug.utils import secure_filename
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.config["MAX_CONTENT_LENGTH"] = 1024 * 1024
|
||||||
|
app.config["UPLOAD_EXTENSIONS"] = [".csv"]
|
||||||
|
app.config["UPLOAD_PATH"] = "uploads"
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
|
return render_template("index.html")
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/", methods=["POST"])
|
||||||
|
def upload_file():
|
||||||
|
uploaded_file = request.files["file"]
|
||||||
|
filename = secure_filename(uploaded_file.filename)
|
||||||
|
|
||||||
|
if filename != "":
|
||||||
|
file_ext = os.path.splitext(filename)[1]
|
||||||
|
if file_ext not in app.config["UPLOAD_EXTENSIONS"]:
|
||||||
|
abort(400)
|
||||||
|
|
||||||
|
# make sure the upload directory exists
|
||||||
|
try:
|
||||||
|
os.mkdir(app.config["UPLOAD_PATH"])
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
uploaded_file.save(os.path.join(app.config["UPLOAD_PATH"], filename))
|
||||||
|
|
||||||
|
return redirect(url_for("index"))
|
||||||
|
|
||||||
return "Congratulations, it's a web app!"
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host="127.0.0.1", port=8080, debug=True)
|
app.run(host="127.0.0.1", port=8080, debug=True)
|
||||||
|
13
templates/index.html
Normal file
13
templates/index.html
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>CSV Metadata Quality Web</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>File Upload</h1>
|
||||||
|
<form method="POST" action="" enctype="multipart/form-data">
|
||||||
|
<p><input type="file" name="file" accept=".csv"></p>
|
||||||
|
<p><input type="submit" value="Submit"></p>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user