Skip to content

Instantly share code, notes, and snippets.

@ipetrushin
Created November 14, 2022 07:49
Show Gist options
  • Save ipetrushin/7ae80b8369c5055e9c9bd6166241d5d6 to your computer and use it in GitHub Desktop.
Save ipetrushin/7ae80b8369c5055e9c9bd6166241d5d6 to your computer and use it in GitHub Desktop.
Upload Flask App
from flask import Flask, render_template
from flask import flash, request, redirect, url_for
import random, os
app = Flask(__name__)
# list of cat images
images = [ "https://www.memecreator.org/static/images/memes/5128017.jpg" ]
@app.route('/')
def index():
url = random.choice(images)
return render_template('index.html', url=url)
app.config['UPLOAD_FOLDER'] = '/home/teacher/flaskapp/images'
@app.route('/upload', 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 an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
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>
<input type=file name=file>
<input type=submit value=Upload>
</form>
'''
if __name__ == "__main__":
app.run(host="0.0.0.0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment