-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
34 lines (27 loc) · 917 Bytes
/
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
import os
import qrcode
import uuid
from flask import Flask, render_template, request, redirect, send_file, url_for
app = Flask(__name__)
save_dir = os.path.join('static', 'img', 'qrcodes')
os.makedirs(save_dir, exist_ok=True)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate', methods=['POST'])
def generate_qrcode():
url = request.form.get('url')
if not url:
return redirect(url_for('index'))
img = qrcode.make(url)
random_filename = f"{uuid.uuid4()}.png"
save_path = os.path.join(save_dir, random_filename)
img.save(save_path)
return redirect(url_for('download', qrcode=random_filename))
@app.route('/download')
def download():
qrcode_name = request.args.get('qrcode')
file_path = os.path.join(save_dir, qrcode_name)
return send_file(file_path, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)