This python / flask script creates an svg QR code byte stream but when I try to render it using a Jinja2 template {{ qr[0] }}
, it renders as text. How can I render it as an image? As you can see from the code below, I have successfully transferred the data to the client - I do not need help with that - the only issue is that the data is in the wrong format - an svg byte stream - whereas I want to display that data as an image.
For the benefit of clarification, I MUST export the qr code in the render_template because I have other variables that contribute to the qr code, which also need to be exported. Therefore, the <img src="{{ url_for('qr') }}">
method does not work.
Likewise, I do not want the data to be visible in the URL.
import pyqrcode
from io import BytesIO
@app.route('/qr/<id>/<price>', methods=['GET', 'POST'])
def qr(id,price):
if id:
data = id+price
basestring = pyqrcode.create(data, error='H')
stream = BytesIO()
basestring.svg(stream, scale=5, module_color='#802929', background='#FFFFFF')
qr = stream.getvalue(), 200, {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
return render_template('qr.html', qr=qr, data=data)
This is a portion of what currently renders as text on the HTML page:
b'\n<path transform="scale(5)" stroke="#802929" class="pyqrline" d="M4 4.5h7m1 0h1m1 0h2m2 0h2m1 0h3m1 0h1m1 0h2m1 0h2m2 0h5m2 0h4m1 0h1m1 0h4m2 0h7m-57 1h1m5 0h1m2 0h1m1 0h1m2 0h5m1 0h2m3 0h1m4 0h1m1 0h1m1 0h5m4 0h1m3 0h1m2 0h1m5 0h1m-57 1h1m1 0h3m1 0h1m1 0h1m1 0h4m3 0h1m3 0h1m2 0h2m3 0h2m3 0h1m2 0h1m1 0h4m1 0h4m2 0h1m1 0h3m1 0h1m-57 1h1m1 0h3m1 0h1m3 0h3m2 0h1m2 0h3m1 0h1m8 0h2m1 0h2m3 0h2m1 0h3m2 0h1m2 0h1m1 0h3m1 0h1m-57 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m2 0h1m5 0h1m3 0h6m1 0h1m1 0h3m3 0h3m1 0h1m2 0h1m2 0h1m1 0h3m1 0h1m-57 1h1m5 0h1m2 0h1m5 0h1m2 0h1m6 0h2m3 0h1m1 0h2m1 0h7m1 0h4m3 0h1m5 0h1m-57 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-49 1h1m3 0h3m1 0h5m1 0h1m3 0h1m3 0h1m1 0h1m2 0h1m3 0h3m1 0h1m1 0h2m1 0h1m-44 1h2m4 0h1m2 0h2m1 0h4m4 0h6m2 0h5m1 0h1m5 0h3m2 0h1m1 0h1m1 0h1m1 0h1m-57