-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
51 lines (38 loc) · 1.26 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from flask import Flask, request, send_from_directory, render_template, url_for, make_response
import qrcode
import os
app = Flask(__name__, static_folder='static')
@app.route('/')
def home():
return render_template("index.html")
@app.route('/qr', methods=["GET", "POST"])
def showQR():
if request.method == "GET":
data = request.args.get("data", "")
else:
return render_template("index.html")
image_file, name = makeQR(data)
resp = make_response(render_template("image.html", image_file=image_file, data=data))
return resp
@app.route("/downloadQR", methods=["GET", "POST"])
def downloadQR():
data = request.form.get("data")
image_file, name = makeQR(data)
return send_from_directory(directory="static", path=name, as_attachment=True)
def makeQR(data):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=2,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
name = "qrcode.png"
path = os.path.join(os.getcwd(), "static", name)
img.save(path)
image_file = url_for('static', filename=name)
return image_file, name
if __name__ == '__main__':
app.run()