From a243c0c9173989a9ce52323d6e4d9aad2d09b235 Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Sun, 13 Aug 2017 21:49:35 -0400 Subject: [PATCH] Flask framework with uploading photos --- app.py | 32 ++++++++++++++++++++++++++++++++ requirements.txt | 15 +++++++++++++++ templates/index.html | 22 ++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 app.py create mode 100644 requirements.txt create mode 100644 templates/index.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..88fc8e7 --- /dev/null +++ b/app.py @@ -0,0 +1,32 @@ +from flask import Flask, render_template, request, redirect, url_for, send_from_directory +from werkzeug import secure_filename +import requests +import os + +app = Flask(__name__) +app.config['DEBUG'] = True +app.config['UPLOAD_FOLDER'] = 'uploads' +app.config['FILE_EXTENSIONS'] = ['png', 'jpg', 'pdf', 'jpeg', 'gif'] + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1] in app.config['FILE_EXTENSIONS'] + +@app.route('/') +def index(): + return render_template('index.html') + +@app.route('/upload', methods=['GET', 'POST']) +def uploadFile(): + if request.method == 'POST': + fileObj = request.files['file'] + if fileObj and allowed_file(fileObj.filename): + safeFile = secure_filename(fileObj.filename) + fileObj.save(os.path.join(app.config['UPLOAD_FOLDER'], safeFile)) + return redirect(url_for('uploaded_file', filename=safeFile)) + +@app.route('/uploads/') +def uploaded_file(filename): + return send_from_directory(app.config['UPLOAD_FOLDER'], filename) + +if __name__ == '__main__': + app.run(host='0.0.0.0') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3980d90 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,15 @@ +Flask +Flask-Login==0.4.0 +Flask-WTF==0.13.0 +Jinja2==2.8 +MarkupSafe==0.23 +WTForms==2.1 +Werkzeug==0.10.4 +argparse==1.2.1 +flask-mongoengine==0.8.2 +itsdangerous==0.24 +mongoengine==0.11.0 +pymongo==3.4.0 +request==2.2.1 +six==1.10.0 +wsgiref==0.1.2 \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..4f35d87 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,22 @@ + + + + + + +
+
+

How To Upload a File

+
+
+
+ +
+

+ +
+
+
+ + \ No newline at end of file