Closed Frakko closed 3 years ago
Save the code into a BytesIO buffer and deliver the result. Either as SVG image which can be embedded directly or you need a view which sets the mime type accordingly. Or create a data URI which in turn can be used directly within HTML.
I appreciate your response, however I'm not completely understanding. Could you briefly explain me how to convert the BytesIO buffer in to an SVG image or a format that an HTML file would understand?
>>> import io
>>> import re
>>> import qrcode
>>> import qrcode.image.svg
>>> factory = qrcode.image.svg.SvgPathImage
>>> img = qrcode.make('test', image_factory=factory)
>>> out = io.BytesIO()
>>> img.save(out)
>>> res = re.sub(r'^<\?[^>]+>\n?', '', out.getvalue().decode('utf-8'))
>>> res
'<svg height="29mm" version="1.1" viewBox="0 0 29 29" width="29mm" xmlns="http://www.w3.org/2000/svg"><path d="M 20 10 L 20 11 L 21 11 L 21 10 z M 16 5 L 16 6 L 17 6 L 17 5 z M 15 15 L 15 16 L 16 16 L 16 15 z M 14 21 L 14 22 L 15 22 L 15 21 z M 23 23 L 23 24 L 24 24 L 24 23 z M 4 24 L 4 25 L 5 25 L 5 24 z M 18 8 L 18 9 L 19 9 L 19 8 z M 19 24 L 19 25 L 20 25 L 20 24 z M 24 24 L 24 25 L 25 25 L 25 24 z M 22 8 L 22 9 L 23 9 L 23 8 z M 17 21 L 17 22 L 18 22 L 18 21 z M 5 10 L 5 11 L 6 11 L 6 10 z M 4 14 L 4 15 L 5 15 L 5 14 z M 8 4 L 8 5 L 9 5 L 9 4 z M 14 12 L 14 13 L 15 13 L 15 12 z M 15 9 L 15 10 L 16 10 L 16 9 z M 14 11 L 14 12 L 15 12 L 15 11 z M 20 7 L 20 8 L 21 8 L 21 7 z M 10 14 L 10 15 L 11 15 L 11 14 z M 18 22 L 18 23 L 19 23 L 19 22 z M 16 10 L 16 11 L 17 11 L 17 10 z M 23 18 L 23 19 L 24 19 L 24 18 z M 14 22 L 14 23 L 15 23 L 15 22 z M 21 6 L 21 7 L 22 7 L 22 6 z M 19 15 L 19 16 L 20 16 L 20 15 z M 18 5 L 18 6 L 19 6 L 19 5 z M 4 21 L 4 22 L 5 22 L 5 21 z M 24 13 L 24 14 L 25 14 L 25 13 z M 4 8 L 4 9 L 5 9 L 5 8 z M 17 24 L 17 25 L 18 25 L 18 24 z M 24 8 L 24 9 L 25 9 L 25 8 z M 22 24 L 22 25 L 23 25 L 23 24 z M 7 6 L 7 7 L 8 7 L 8 6 z M 6 10 L 6 11 L 7 11 L 7 10 z M 13 18 L 13 19 L 14 19 L 14 18 z M 9 15 L 9 16 L 10 16 L 10 15 z M 13 7 L 13 8 L 14 8 L 14 7 z M 10 4 L 10 5 L 11 5 L 11 4 z M 8 20 L 8 21 L 9 21 L 9 20 z M 13 20 L 13 21 L 14 21 L 14 20 z M 20 4 L 20 5 L 21 5 L 21 4 z M 18 19 L 18 20 L 19 20 L 19 19 z M 16 15 L 16 16 L 17 16 L 17 15 z M 21 17 L 21 18 L 22 18 L 22 17 z M 20 23 L 20 24 L 21 24 L 21 23 z M 18 6 L 18 7 L 19 7 L 19 6 z M 17 14 L 17 15 L 18 15 L 18 14 z M 24 18 L 24 19 L 25 19 L 25 18 z M 6 21 L 6 22 L 7 22 L 7 21 z M 22 14 L 22 15 L 23 15 L 23 14 z M 4 5 L 4 6 L 5 6 L 5 5 z M 9 18 L 9 19 L 10 19 L 10 18 z M 7 22 L 7 23 L 8 23 L 8 22 z M 12 22 L 12 23 L 13 23 L 13 22 z M 10 20 L 10 21 L 11 21 L 11 20 z M 16 12 L 16 13 L 17 13 L 17 12 z M 23 12 L 23 13 L 24 13 L 24 12 z M 22 4 L 22 5 L 23 5 L 23 4 z M 24 20 L 24 21 L 25 21 L 25 20 z M 20 20 L 20 21 L 21 21 L 21 20 z M 24 7 L 24 8 L 25 8 L 25 7 z M 6 22 L 6 23 L 7 23 L 7 22 z M 4 18 L 4 19 L 5 19 L 5 18 z M 7 15 L 7 16 L 8 16 L 8 15 z M 8 16 L 8 17 L 9 17 L 9 16 z M 6 16 L 6 17 L 7 17 L 7 16 z M 13 8 L 13 9 L 14 9 L 14 8 z M 7 21 L 7 22 L 8 22 L 8 21 z M 14 7 L 14 8 L 15 8 L 15 7 z M 10 18 L 10 19 L 11 19 L 11 18 z M 15 14 L 15 15 L 16 15 L 16 14 z M 21 10 L 21 11 L 22 11 L 22 10 z M 18 9 L 18 10 L 19 10 L 19 9 z M 19 4 L 19 5 L 20 5 L 20 4 z M 24 4 L 24 5 L 25 5 L 25 4 z M 7 10 L 7 11 L 8 11 L 8 10 z M 6 6 L 6 7 L 7 7 L 7 6 z M 12 10 L 12 11 L 13 11 L 13 10 z M 14 13 L 14 14 L 15 14 L 15 13 z M 13 11 L 13 12 L 14 12 L 14 11 z M 10 8 L 10 9 L 11 9 L 11 8 z M 15 8 L 15 9 L 16 9 L 16 8 z M 13 24 L 13 25 L 14 25 L 14 24 z M 20 8 L 20 9 L 21 9 L 21 8 z M 15 13 L 15 14 L 16 14 L 16 13 z M 23 21 L 23 22 L 24 22 L 24 21 z M 4 22 L 4 23 L 5 23 L 5 22 z M 19 14 L 19 15 L 20 15 L 20 14 z M 18 10 L 18 11 L 19 11 L 19 10 z M 14 23 L 14 24 L 15 24 L 15 23 z M 24 14 L 24 15 L 25 15 L 25 14 z M 22 10 L 22 11 L 23 11 L 23 10 z M 4 9 L 4 10 L 5 10 L 5 9 z M 17 23 L 17 24 L 18 24 L 18 23 z M 24 9 L 24 10 L 25 10 L 25 9 z M 5 4 L 5 5 L 6 5 L 6 4 z M 4 12 L 4 13 L 5 13 L 5 12 z M 8 15 L 8 16 L 9 16 L 9 15 z M 8 10 L 8 11 L 9 11 L 9 10 z M 14 14 L 14 15 L 15 15 L 15 14 z M 10 5 L 10 6 L 11 6 L 11 5 z M 8 21 L 8 22 L 9 22 L 9 21 z M 18 16 L 18 17 L 19 17 L 19 16 z M 9 24 L 9 25 L 10 25 L 10 24 z M 16 8 L 16 9 L 17 9 L 17 8 z M 23 16 L 23 17 L 24 17 L 24 16 z M 10 24 L 10 25 L 11 25 L 11 24 z M 21 16 L 21 17 L 22 17 L 22 16 z M 19 13 L 19 14 L 20 14 L 20 13 z M 18 7 L 18 8 L 19 8 L 19 7 z M 17 13 L 17 14 L 18 14 L 18 13 z M 14 24 L 14 25 L 15 25 L 15 24 z M 15 24 L 15 25 L 16 25 L 16 24 z M 4 6 L 4 7 L 5 7 L 5 6 z M 12 17 L 12 18 L 13 18 L 13 17 z M 8 12 L 8 13 L 9 13 L 9 12 z M 7 4 L 7 5 L 8 5 L 8 4 z M 6 12 L 6 13 L 7 13 L 7 12 z M 14 19 L 14 20 L 15 20 L 15 19 z M 10 6 L 10 7 L 11 7 L 11 6 z M 13 22 L 13 23 L 14 23 L 14 22 z M 20 18 L 20 19 L 21 19 L 21 18 z M 10 21 L 10 22 L 11 22 L 11 21 z M 19 23 L 19 24 L 20 24 L 20 23 z M 23 15 L 23 16 L 24 16 L 24 15 z M 15 23 L 15 24 L 16 24 L 16 23 z M 5 24 L 5 25 L 6 25 L 6 24 z M 19 16 L 19 17 L 20 17 L 20 16 z M 17 16 L 17 17 L 18 17 L 18 16 z M 24 16 L 24 17 L 25 17 L 25 16 z M 4 19 L 4 20 L 5 20 L 5 19 z M 5 18 L 5 19 L 6 19 L 6 18 z M 12 14 L 12 15 L 13 15 L 13 14 z M 13 15 L 13 16 L 14 16 L 14 15 z M 9 4 L 9 5 L 10 5 L 10 4 z M 7 20 L 7 21 L 8 21 L 8 20 z M 14 4 L 14 5 L 15 5 L 15 4 z M 12 20 L 12 21 L 13 21 L 13 20 z M 10 19 L 10 20 L 11 20 L 11 19 z M 20 15 L 20 16 L 21 16 L 21 15 z M 18 14 L 18 15 L 19 15 L 19 14 z M 10 22 L 10 23 L 11 23 L 11 22 z M 16 18 L 16 19 L 17 19 L 17 18 z M 23 10 L 23 11 L 24 11 L 24 10 z M 22 6 L 22 7 L 23 7 L 23 6 z M 21 14 L 21 15 L 22 15 L 22 14 z M 24 5 L 24 6 L 25 6 L 25 5 z M 6 24 L 6 25 L 7 25 L 7 24 z M 4 16 L 4 17 L 5 17 L 5 16 z M 7 13 L 7 14 L 8 14 L 8 13 z M 6 7 L 6 8 L 7 8 L 7 7 z M 8 6 L 8 7 L 9 7 L 9 6 z M 6 18 L 6 19 L 7 19 L 7 18 z M 10 9 L 10 10 L 11 10 L 11 9 z M 10 12 L 10 13 L 11 13 L 11 12 z M 18 20 L 18 21 L 19 21 L 19 20 z M 23 20 L 23 21 L 24 21 L 24 20 z M 14 20 L 14 21 L 15 21 L 15 20 z M 21 4 L 21 5 L 22 5 L 22 4 z M 20 12 L 20 13 L 21 13 L 21 12 z M 4 23 L 4 24 L 5 24 L 5 23 z M 16 23 L 16 24 L 17 24 L 17 23 z M 4 10 L 4 11 L 5 11 L 5 10 z M 19 10 L 19 11 L 20 11 L 20 10 z M 17 22 L 17 23 L 18 23 L 18 22 z M 24 10 L 24 11 L 25 11 L 25 10 z M 22 22 L 22 23 L 23 23 L 23 22 z M 7 8 L 7 9 L 8 9 L 8 8 z M 6 8 L 6 9 L 7 9 L 7 8 z M 13 16 L 13 17 L 14 17 L 14 16 z M 12 8 L 12 9 L 13 9 L 13 8 z M 9 13 L 9 14 L 10 14 L 10 13 z M 14 15 L 14 16 L 15 16 L 15 15 z M 10 10 L 10 11 L 11 11 L 11 10 z M 9 10 L 9 11 L 10 11 L 10 10 z M 8 22 L 8 23 L 9 23 L 9 22 z M 15 6 L 15 7 L 16 7 L 16 6 z M 14 10 L 14 11 L 15 11 L 15 10 z M 20 6 L 20 7 L 21 7 L 21 6 z M 18 17 L 18 18 L 19 18 L 19 17 z M 23 19 L 23 20 L 24 20 L 24 19 z M 21 7 L 21 8 L 22 8 L 22 7 z M 4 20 L 4 21 L 5 21 L 5 20 z M 18 4 L 18 5 L 19 5 L 19 4 z M 16 20 L 16 21 L 17 21 L 17 20 z M 23 4 L 23 5 L 24 5 L 24 4 z M 4 7 L 4 8 L 5 8 L 5 7 z M 21 20 L 21 21 L 22 21 L 22 20 z M 7 18 L 7 19 L 8 19 L 8 18 z M 8 13 L 8 14 L 9 14 L 9 13 z M 7 7 L 7 8 L 8 8 L 8 7 z M 6 13 L 6 14 L 7 14 L 7 13 z M 12 5 L 12 6 L 13 6 L 13 5 z M 8 8 L 8 9 L 9 9 L 9 8 z M 7 24 L 7 25 L 8 25 L 8 24 z M 14 16 L 14 17 L 15 17 L 15 16 z M 12 24 L 12 25 L 13 25 L 13 24 z M 10 7 L 10 8 L 11 8 L 11 7 z M 11 14 L 11 15 L 12 15 L 12 14 z M 19 22 L 19 23 L 20 23 L 20 22 z M 24 22 L 24 23 L 25 23 L 25 22 z M 23 14 L 23 15 L 24 15 L 24 14 z M 15 22 L 15 23 L 16 23 L 16 22 z M 21 18 L 21 19 L 22 19 L 22 18 z M 17 15 L 17 16 L 18 16 L 18 15 z M 24 17 L 24 18 L 25 18 L 25 17 z M 6 20 L 6 21 L 7 21 L 7 20 z M 4 4 L 4 5 L 5 5 L 5 4 z M 21 23 L 21 24 L 22 24 L 22 23 z M 12 15 L 12 16 L 13 16 L 13 15 z M 8 18 L 8 19 L 9 19 L 9 18 z M 6 14 L 6 15 L 7 15 L 7 14 z M 13 14 L 13 15 L 14 15 L 14 14 z M 14 5 L 14 6 L 15 6 L 15 5 z M 10 16 L 10 17 L 11 17 L 11 16 z M 18 24 L 18 25 L 19 25 L 19 24 z M 8 24 L 8 25 L 9 25 L 9 24 z M 15 16 L 15 17 L 16 17 L 16 16 z M 23 24 L 23 25 L 24 25 L 24 24 z M 21 8 L 21 9 L 22 9 L 22 8 z M 11 13 L 11 14 L 12 14 L 12 13 z M 19 21 L 19 22 L 20 22 L 20 21 z M 10 23 L 10 24 L 11 24 L 11 23 z M 23 13 L 23 14 L 24 14 L 24 13 z M 22 7 L 22 8 L 23 8 L 23 7 z M 15 21 L 15 22 L 16 22 L 16 21 z M 24 6 L 24 7 L 25 7 L 25 6 z M 6 4 L 6 5 L 7 5 L 7 4 z M 8 7 L 8 8 L 9 8 L 9 7 z M 13 9 L 13 10 L 14 10 L 14 9 z" id="qr-path" style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" /></svg>'
"res" can be used in a HTML context. Maybe you should remove the namespace as well but I think it does no harm.
Cool, thanks for taking the time to explain it!
I was playing around with the qrcode package, and my goal was to embed a dynamically generated qr code in to an HTML page, however I wasn't able to make it work. I wouldn't want to save the image containing the qr code locally, due to resource optimization.
I'm wondering, is there a workaround for this?