Generate QR Code using Jinja

I have a custom print format that displays a qr code using {{ qrcode (doc.name) }}

hooks.py

from .custom_app.qrcode import jenvs
jenv = jenvs

custom_app/qrcode.py

import frappe
from frappe import _
import qrcode
import qrcode.image.svg
import io
import base64

jenvs = {
	"methods": [
		"qrcode:custom_app.qrcode.gen_qr"
	],
	"filters": [],
}

@frappe.whitelist()
def gen_qr(name):		
	img = qrcode.make(name)
	buffer = io.BytesIO()
	img.save(buffer)
	img_str = base64.b64encode(buffer.getvalue()).decode()
	buffer.seek(0)
	buffer.truncate(0)
	return "data:image/png;base64, " + img_str

However, when I select the custom print format, it appears jinja2.exceptions.UndefinedError: ‘gen_qr’ is undefined

Am I doing something wrong? Do I need to whitelist it somewhere else?

It used to work on version 13, but I can’t make it work since I updated to version 14.

According to Migrating to Version 14 · frappe/frappe Wiki · GitHub jenv = { was replaced with jinja = { and “qrcode:custom_app.qrcode.gen_qr” was replaced with “custom_app.qrcode.gen_qr”. However, I still din’t manage to get it working

1 Like

Can you post all relevant lines in your hooks.py file? It’s working fine for me.