Web development

A few proposals how to use Segno with popular Python web application frameworks.


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__)

def home():
    qrcode = segno.make('The Continuing Story of Bungalow Bill')
    return render_template('example.html', qrcode=qrcode)
<!DOCTYPE html>
    <meta charset="utf-8" />
    <title>QR Codes</title>
    <img src="{{ qrcode.png_data_uri(dark='darkblue', data_dark='steelblue', alignment_dark='darkgreen', scale=3) }}"><br>
    <img src="{{ qrcode.svg_data_uri(dark='darkblue', scale=4) }}">

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.

def home():
    qrcode = segno.make('While My Guitar Gently Weeps')
    return render_template('example.html', qrcode=qrcode)
  {{ qrcode.svg_inline(scale=3) | safe }}

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'}

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')
    return send_file(buff, mimetype='image/png')
<img src="{{ url_for('qrcode_png', data='Rocky Raccoon') }}">


The project django-segno-qr provides a template tag for creating SVG QR codes in Django templates while django-qr-code provides more template tags and utility functions.

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

qrcode = segno.make('JULIA')
# Save the QR code with transparent background and use dark blue for
# the dark modules
out = io.BytesIO()
qrcode.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)

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

qrcode = segno.make('JULIA')
# img is a Pillow Image instance
img = qrcode.to_pil(dark='#00008b', scale=3)
# Now use Pillow Image.save() to save the QR code
out = io.BytesIO()
img.save(out, format='jpg')

# ...

ticket.qrcode.save('JULIA.jpg', ContentFile(out.getvalue()), save=False)