How to debug python file in frappe

Hi all,

In frappe, how to debug the python files. For example,
def get_columns(qty_list):
columns = [{
“fieldname”: “supplier_name”,
“label”: “Supplier”,
“fieldtype”: “Link”,
“options”: “Supplier”,
“width”: 200
}]

for qty in qty_list:
columns.append({
“fieldname”: qty.key,
“label”: qty.label,
“fieldtype”: “Currency”,
“options”: “currency”,
“width”: 80
})
columns.append({
“fieldname”: qty.key + “QUOTE”,
“label”: “Quotation”,
“fieldtype”: “Link”,
“options”: “Supplier Quotation”,
“width”: 90
})
columns.append({
“fieldname”: qty.key + “QUOTE1”,
“label”: “Description”,
“fieldtype”: “Data”,
“options”: “Item”,
“width”: 200
})

return columns

in the above code, if i want to see the parameter value “qty_list” mean, how can i debug?

I tried with this print(“qty_list”,qty_list) but it doesn’t work. Someone please help me

@Racer_Aravind
use

frappe.msgprint("qty_list:"+qty_list)
1 Like

Sorry, I am getting a error like this,
Traceback (most recent call last):
File “/home/aravind/frappe-bench/apps/frappe/frappe/app.py”, line 56, in application
response = frappe.handler.handle()
File “/home/aravind/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/aravind/frappe-bench/apps/frappe/frappe/handler.py”, line 52, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/aravind/frappe-bench/apps/frappe/frappe/init.py”, line 922, in call
return fn(*args, **newargs)
File “/home/aravind/frappe-bench/apps/frappe/frappe/desk/query_report.py”, line 95, in run
res = frappe.get_attr(method_name)(frappe._dict(filters))
File “/home/aravind/frappe-bench/apps/erpnext/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py”, line 14, in execute
columns = get_columns(qty_list)
File “/home/aravind/frappe-bench/apps/erpnext/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py”, line 80, in get_columns
frappe.msgprint(“qty_list:”+qty_list)
TypeError: coercing to Unicode: need string or buffer, list found

@Racer_Aravind
qty_list is a list whereas you need a string to print.

Hi,

I want to add a description column in Quoted Item Comparison. So I had changed the code and it is working fine for the items which have Supplier Quotation Document. But the item which don’t have Supplier Quotation document, I am getting a error like this.
Traceback (most recent call last):
File “/home/aravind/frappe-bench/apps/frappe/frappe/app.py”, line 56, in application
response = frappe.handler.handle()
File “/home/aravind/frappe-bench/apps/frappe/frappe/handler.py”, line 21, in handle
data = execute_cmd(cmd)
File “/home/aravind/frappe-bench/apps/frappe/frappe/handler.py”, line 52, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “/home/aravind/frappe-bench/apps/frappe/frappe/init.py”, line 922, in call
return fn(*args, **newargs)
File “/home/aravind/frappe-bench/apps/frappe/frappe/desk/query_report.py”, line 95, in run
res = frappe.get_attr(method_name)(frappe._dict(filters))
File “/home/aravind/frappe-bench/apps/erpnext/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py”, line 10, in execute
qty_list = get_quantity_list(filters.item)
File “/home/aravind/frappe-bench/apps/erpnext/erpnext/buying/report/quoted_item_comparison/quoted_item_comparison.py”, line 76, in get_quantity_list
out.append(col)
UnboundLocalError: local variable ‘col’ referenced before assignment

Here is my code,

from future import unicode_literals
from erpnext.setup.utils import get_exchange_rate
from frappe.utils import flt, cint
import frappe

def execute(filters=None):
qty_list = get_quantity_list(filters.item)

data = get_quote_list(filters.item, qty_list)

columns = get_columns(qty_list)

return columns, data
def get_quote_list(item, qty_list):
out = []
if item:
price_data = []
suppliers = []
company_currency = frappe.db.get_default(“currency”)
float_precision = cint(frappe.db.get_default(“float_precision”)) or 2

Get the list of suppliers

for root in frappe.db.sql(""“select parent, qty, description, rate from tabSupplier Quotation Item where item_code=%s and docstatus < 2"”", item, as_dict=1):
for splr in frappe.db.sql(""“SELECT supplier from tabSupplier Quotation where name =%s and docstatus < 2"”", root.parent, as_dict=1):
ip = frappe._dict({
“supplier”: splr.supplier,
“qty”: root.qty,
“description”: root.description,
“parent”: root.parent,
“rate”: root.rate})
price_data.append(ip)
suppliers.append(splr.supplier)

#Add a row for each supplier
for root in set(suppliers):
	supplier_currency = frappe.db.get_value("Supplier", root, "default_currency")
	if supplier_currency:
		exchange_rate = get_exchange_rate(supplier_currency, company_currency)
	else:
		exchange_rate = 1

	row = frappe._dict({
		"supplier_name": root,
	})
	for col in qty_list:
		# Get the quantity for this row
		for item_price in price_data:
			if str(item_price.qty) == col.key and item_price.supplier == root:
				row[col.key] = flt(item_price.rate * exchange_rate, float_precision)
				row[col.key + "QUOTE"] = item_price.parent
				row[col.key + "QUOTE1"] = item_price.description
				break
			else:
				row[col.key] = ""
				row[col.key + "QUOTE"] = ""
				row[col.key + "QUOTE1"] = ""
	out.append(row)

return out
def get_quantity_list(item):
out = []

if item:
qty_list = frappe.db.sql(""“select distinct qty, description from tabSupplier Quotation Item where ifnull(item_code,’’)=%s and docstatus < 2"”", item, as_dict=1)
qty_list.sort(reverse=False)
for qt in qty_list:
col = frappe._dict({
“key”: str(qt.qty),
“label”: "Qty: " + str(int(qt.qty))
})
out.append(col)

return out
def get_columns(qty_list):
columns = [{
“fieldname”: “supplier_name”,
“label”: “Supplier”,
“fieldtype”: “Link”,
“options”: “Supplier”,
“width”: 200
}]

for qty in qty_list:
columns.append({
“fieldname”: qty.key,
“label”: qty.label,
“fieldtype”: “Currency”,
“options”: “currency”,
“width”: 80
})
columns.append({
“fieldname”: qty.key + “QUOTE”,
“label”: “Quotation”,
“fieldtype”: “Link”,
“options”: “Supplier Quotation”,
“width”: 90
})
columns.append({
“fieldname”: qty.key + “QUOTE1”,
“label”: “Description”,
“fieldtype”: “Data”,
“options”: “Item”,
“width”: 200
})

return columns
Kindly help me to solve it.

Change your code like this:

if qty_list:
	for col in qty_list:
			# Get the quantity for this row
			for item_price in price_data:
				if str(item_price.qty) == col.key and item_price.supplier == root:
					row[col.key] = flt(item_price.rate * exchange_rate, float_precision)
					row[col.key + "QUOTE"] = item_price.parent
					row[col.key + "QUOTE1"] = item_price.description
					break
				else:
					row[col.key] = ""
					row[col.key + "QUOTE"] = ""
					row[col.key + "QUOTE1"] = ""
		out.append(row)
1 Like

Thanks Ruchin, solved