Web development¶
A few proposals how to use Segno with popular Python web application frameworks.
Flask¶
There are various ways to output QR codes in Jinja templates in conjunction with Flask. See also the Flask example in the repository: https://github.com/heuer/segno/tree/master/examples/flask_qrcode
Data URIs¶
Create a QR code in the Flask view and use the segno.QRCode.svg_data_uri()
or segno.QRCode.png_data_uri()
methods in the template.
from flask import Flask, render_template
import segno
app = Flask(__name__)
@app.route('/)
def home():
qr = segno.make('The Continuing Story of Bungalow Bill')
return render_template('example.html', qr=qr)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>QR Codes</title>
</head>
<body>
<img src="{{ qr.png_data_uri(dark='darkblue', data_dark='steelblue', alignment_dark='darkgreen', scale=3) }}"><br>
<img src="{{ qr.svg_data_uri(dark='darkblue', scale=4) }}">
</body>
</html>
Embed SVG QR codes into HTML¶
Since HTML5 supports SVG directly, it’s also possible to embed the generated SVG directly into a template.
Create the QR code within the Flask view and use the
segno.QRCode.svg_inline()
method in conjunction with the Jinja
|safe
filter.
@app.route('/')
def home():
qr = segno.make('While My Guitar Gently Weeps')
return render_template('example.html', qr=qr)
<div>
{{ qr.svg_inline(scale=3) | safe }}
</div>
Create a view¶
Another possibility is to create the QR codes dynamically in a Flask view and
deliver them with send_file
.
BEATLES_SONGS = {'Yellow Submarine', 'Let It Be', 'Rocky Raccoon'}
@app.route('/qr-png/')
def qrcode_png():
data = request.args.get('data')
# Check if the data is acceptable otherwise a 404 error is generated
if data not in BEATLES_SONGS:
return abort(404)
buff = io.BytesIO()
segno.make(data, micro=False) \
.save(buff, kind='png', scale=4, dark='darkblue',
data_dark='#474747', light='#efefef')
buff.seek(0)
return send_file(buff, mimetype='image/png')
<img src="{{ url_for('qrcode_png', data='Rocky Raccoon') }}">
Django¶
The project django-segno-qr provides a template tag for creating SVG QR codes in Django templates.
Apart from that, the aforementioned information for Flask should also be
adaptable to Django, so here is just a hint on how to save QR codes in a
Django ImageField
.
The complete code is in the repository: https://github.com/heuer/segno/tree/master/examples/django_qrcode
Saving a QR code to an ImageField¶
Assuming this simple model.
from django.db import models
class Ticket(models.Model):
name = models.CharField(max_length=150, unique=True)
qrcode = models.ImageField(upload_to='ticket-qrcodes/')
Create a QR code with Segno and save it as PNG into a io.BytesIO
instance.
import io
import segno
out = io.BytesIO()
qr = segno.make('JULIA')
# Save the QR code with transparent background and use dark blue for
# the dark modules
qr.save(out, kind='png', dark='#00008b', light=None, scale=3)
Now you can use the content of the buffer as input for a Django ContentFile
.
ticket = Ticket(name='JULIA')
ticket.qrcode.save('JULIA.png', ContentFile(out.getvalue()), save=False)
ticket.save()
If for some reason the QR codes should be stored in the lossy file format JPEG,
the qrcode-artistic
plugin is required (see also Artistic QR Codes):
$ pip install qrcode-artistic
import io
import segno
out = io.BytesIO()
qr = segno.make('JULIA')
# img is a Pillow Image instance
img = qr.to_pil(dark='#00008b', scale=3)
# Now use Pillow Image.save() to save the QR code
img.save(out, format='jpg')
# ...
ticket.qrcode.save('JULIA.jpg', ContentFile(out.getvalue()), save=False)