How to call a function in Python code from a html file

Hi,

I created a new function called ‘get_filedname’ in /erpnext/controllers/print_setting.py, this function is used to return a field name list. I called this new function in print_setting.py, it works well. Then I’d like to call this function in /erpnext/erpnext/templates/print_formats/includes/item_table_description.html as well, so I’d like to know how to call a method in python code from a html file.
By the way, I found I can call a function directly in this html file if I write same function into frappe/frappe/utils/data.py by using ‘{%- set fieldnamelist=frappe.utils.get_filedname()’ -%}

Regards
Jasmine

@Jasmine

The fieldnames already exists in doc._meta check the options here https://github.com/frappe/frappe/blob/develop/frappe/model/meta.py#L70

@max_morais_dmm

Thanks for your reply. My function is getting field name of specific field, so I’m afraid I cannot use the function provided by meta. This is the reason I created a new function in print_setting.py by myself. So I’d like to know how to call this new function in /erpnext/erpnext/templates/print_formats/includes/item_table_description.html. Thanks.

@Jasmine Meta is able to inform it to you!

Hi, @max_morais_dmm, My function is extracting a few fields in a doc which meet specific conditions, then return a field name list. The main part of my function is to check if the field meet my conditions, not just getting the field name.

@Jasmine, I still not understanding why do you need customize a erpnext controller, that cant not made by the standard options in the system, do you can give-me a sample of what do you is trying to do?

@max_morais_dmm sorry for confusing you. I’ll explain more clearly.

We just upgraded our erpnext to version6 from a quite old version 4. Then we found the print format of item list was changed. It was always showing just five columns including sr, description, quantity, rate, amount, and other not print hide field was shown in the description column in version4, but in version6, all the fields of not print hide are shown in the list view, so the number of item list columns is variable now, which depends on how many not print hide fields have in the doc. My supervisor’d like to keep same with version4 in this part. As you know, the structure of this part of code was changed from 4 to 6, so I cannot use version4’s code replace current code. For doing that, I think I have to do :

  1. change print_setting.py code: add not print hide fields to ‘doc.hide_in_print_layout’ to make them not show in the list view
  2. change item_table_descrtipion.html code: loop the not print hide fields to show in the description column

def print_settings_for_item_table(doc):
doc.print_templates = {
“description”: “templates/print_formats/includes/item_table_description.html”,
“qty”: “templates/print_formats/includes/item_table_qty.html”
}
customised_print_preview = cint(frappe.db.get_value(“Stock Settings”, None, “customised_print_preview”))

doc.hide_in_print_layout = ["item_code", "item_name", "image", "uom", "stock_uom"]

if customised_print_preview:
	
	visible_fieldname = get_visible_fieldname(doc.doctype)
	for field in visible_fieldname:
		if field not in doc.hide_in_print_layout:
			doc.hide_in_print_layout.append(field)

def get_visible_fieldname(docname):

fieldnames=[]

for d in frappe.db.sql("""select fieldname from `tabDocField`
		where parent = %(parent)s
		and fieldtype not in ("Section Break", "Column Break", "Button")
		and print_hide = 0""", {
			"parent": docname
		}, as_dict = 1):
	
	if d.fieldname not in ("description", "qty", "rate", "amount", "stock_uom", "uom","item_code", "item_name"):
		fieldnames.append(d.fieldname)

for d in frappe.db.sql("""select field_name, value from `tabProperty Setter` 
				where doc_type = %(doctype)s
				and property = 'print_hide'""", {
					"doctype": docname
				}, as_dict = 1):
	
	if cint(d.value) == 0:
		fieldnames.append(d.field_name)
	elif cint(d.value) == 1:
		if d.field_name in fieldnames:
			fieldnames.remove(d.field_name)

return fieldnames

This is my changed code. And I found there is a function named ‘get_visible_columns’ in print.py, I don’t know if it can be used in my case.

Solved. Don’t need call that method, can get columns from doc.