How to use frappe.call in print format or the equivalent

Hi All;

If I need to call a python method and get values to be used in the print format, how I can do this?

Regards
Bilal

1 Like

If your print format is for doctype abc, and on abc.py you have your python method you can directly call it using doc.methodname().

Hello @lokesh

I am getting the below error:

I did the following in sales_invoice.py:

@frappe.whitelist()
def get_value_print(test=None):
return “Bilal Ghayad”

And in the printing format, I used the following in the custom html element:

{{ doc.get_value_print("Welcome") }}

Can you please help?
Regards
Bilal

Hi,
Use
{% set var = doc.get_value_print("Welcome") %}
{{var}}

1 Like

Still the same error.
I did some investigation and the error is coming from the line return get_jenv().from_string(template).render(context) in the jinja.py as it is appearing in the raised below error:

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/utils/jinja.py”, line 78, in render_template
return get_jenv().from_string(template).render(context)
File “/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/home/frappe/frappe-bench/env/lib/python3.6/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File "

Please note that I added the following to the hooks.py and I did bench migrate but it did not resolved:

jenv = {
“methods”: [
“get_value_print:erpnext.accounts.doctype.sales_invoice.sales_invoice.get_value_print”
]
}

This obtained from the following links:

https://github.com/frappe/frappe/pull/5585

Please, I appreciate the kindly help.

Regards
Bilal

HELLO;

It has resolved using the following:

{{get_value_print()}}

So generally speaking, it is as following:

{{method_name()}}

Of course, it is required to add the needed configuration in the hooks.py

Regards
Bilal

1 Like

in hooks.py

jenv = {
	"methods": [
		"get_jinja_data:property_sales.property_sales.doctype.property_sales.property_sales.get_jinja_data"
	]
}

in py file

@frappe.whitelist()
def get_jinja_data(doc):
	return frappe.db.sql("""select outstanding_amount, datediff(CURDATE(), posting_date) as "outstanding_days" from `tabSales Invoice` a where associated_property_sales = %s and docstatus = 1 and outstanding_amount > 0""",doc.name, as_dict=True)

in jinja print format

get_jinja_data(doc)

5 Likes