Skip to content

Instantly share code, notes, and snippets.

@tonyseek
Created August 22, 2015 12:27
Show Gist options
  • Save tonyseek/5ce19d2a36fa44dfe45c to your computer and use it in GitHub Desktop.
Save tonyseek/5ce19d2a36fa44dfe45c to your computer and use it in GitHub Desktop.
QRCode generator in Flask.
from __future__ import print_function, absolute_import, unicode_literals
from base64 import urlsafe_b64encode, urlsafe_b64decode
from mimetypes import types_map
from io import BytesIO
from flask import Blueprint, current_app, send_file, url_for, jsonify
from qrcode import QRCode
from itsdangerous import Signer
__all__ = ['make_qrcode_image']
bp = Blueprint('qrcode', __name__, url_prefix='/_qrcode')
@bp.route('/<url>/<signature>')
def qrcode_image(url, signature):
"""The endpoint for generated QRCode image."""
try:
url = urlsafe_b64decode(url.encode('ascii'))
except (ValueError, UnicodeEncodeError):
return jsonify(r=False, error='invalid_data'), 404
signer = get_qrcode_signer()
if not signer.verify_signature(url, signature):
return jsonify(r=False, error='invalid_signature'), 404
image = make_qrcode_image(url, border=0)
response = make_image_response(image, kind='png')
return response
def get_qrcode_signer(app=None):
"""Creates a signer by the secret key of current app."""
app = app or current_app
if 'qrcode.signer' not in app.extensions:
app.extensions['qrcode.signer'] = Signer(app.secret_key)
return app.extensions['qrcode.signer']
def make_qrcode_image(data, **kwargs):
"""Creates a QRCode image from given data."""
qrcode = QRCode(**kwargs)
qrcode.add_data(data)
return qrcode.make_image()
def make_image_response(image, kind):
"""Creates a cacheable response from given image."""
mimetype = types_map['.' + kind.lower()]
io = BytesIO()
image.save(io, kind.upper())
io.seek(0)
return send_file(io, mimetype=mimetype, conditional=True)
def make_qrcode_url(url):
"""Creates a signed URL which pointed to a QRCode image."""
signer = get_qrcode_signer()
signature = signer.get_signature(url)
url = urlsafe_b64encode(url.strip().encode('utf-8'))
return url_for('jqrcode.qrcode_image', url=url, signature=signature)
Flask==0.10.1
itsdangerous==0.24
Pillow==2.8.1
qrcode==5.1
Werkzeug==0.10.4

How to use

from flask import Flask, render_template
from myapp.views.qrcode import bp, make_qrcode_url


app = Flask(__name__)
app.config['SECRET_KEY'] = 'random-bytes'
app.register_blueprint(bp)


@app.route('/')
def index():
    qrcode_url = make_qrcode_url('https://goo.gl/3K59Aj')
    return render_template('index.html', qrcode_url=qrcode_url)
    

if __name__ == '__main__':
    app.run()
<img src="{{ qrcode_url }}" width="300" height="300" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment