Frappe._dict use-case and how it works?

How is frappe._dict different than a usual list when returning a data[] for a script report? I’m also planning on learning on how to make a custom chart.

I can’t see the exact “frappe._dict” syntax in the documentation so I figured I’d ask here on when and how to use this one. Thanks.

# Copyright (c) 2023, Frappe and contributors
# For license information, please see license.txt

import frappe

def execute(filters=None):
	# returm empty object if no filters
	if not filters:
		filters = {}

	columns, data = [], []

	columns = get_columns()
	cs_data = get_filtered_data(filters)

	if not cs_data:
		frappe.msgprint("No records found")
		return columns, data
	
	for d in cs_data:
		data.append(frappe._dict({
			'first_name': d.first_name,
			'age': d.age,
			'status': d.status
		}))
	
	# Chart
	# chart = {
	# 	'data':{
	# 		'labels':[
	# 			d.status for d in data
	# 		],
	# 		'datasets':[
	# 			{
	# 				'Name': "Status",
	# 				'values':[
	# 					d.status for d in data
	# 				],
	# 			}
	# 		]
	# 	},'type':'bar'
	# }

	# report_summary = [
	# 	{
	# 		"label":"Open",
	# 		"value":3,
	# 		'indicator':'Green'
	# 	},
	# 	{
	# 		"label":"Pending",
	# 		"value":10,
	# 		'indicator':'Orange'
	# 	},
	# 	{
	# 		"label":"Closed",
	# 		"value":7,
	# 		'indicator':'REd'
	# 	}
	# ]

	return columns, data, None, chart

def get_columns():
	return [
		{
			"fieldname" : "first_name",
			"label" : "FIrst Name",
			"fieldtype" : "Data",
		},
		{
			"fieldname" : "age",
			"label" : "Age",
			"fieldtype" : "Data",
		},
		{
			"fieldname" : "Status",
			"label" : "status",
			"fieldtype" : "Data",
		},
	]

def get_filtered_data(filters):
	filter = get_filters(filters)
	
	# Get and bypass permissions
	filtered_data = frappe.get_all(
		doctype= "Test Doctype",
		fields= ["first_name", "age", "status"],
		filters= filter,
		order_by= "first_name asc"
	)
	# with permissions

	# filtered_data = frappe.get_list(
	# 	'Test Doctype',
	# 	filters=filter,
	# 	fields= ["first_name", "age", "status"],
	# 	order_by= "first_name asc"
	# )
	return filtered_data

def get_filters(filters):
	filters_object = {}
	
	for key, value in filters.items():
		if filters.get(key):
			filters_object[key] = value

	return filters_object
1 Like

@iaiaian1 frappe._dict is just a shortcurt, to deal with dicts with an object like syntax!

For example:

>>> abc = {'a':1, 'b': 2, 'c': 3}
>>> (abc['a'], abc['b'], abc['c'])
(1, 2, 3)

>>> (abc.a, abc.b, abc.c)
 Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'a'

>>> abc['d']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'd'

>>> abc = frappe._dict(abc)
>>> (abc['a'], abc['b'], abc['c'])
(1, 2, 3)

>>>  (abc.a, abc.b, abc.c)
(1, 2, 3)

>>> abc['d']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'd'

>>> abc.d
None

The use of frappe._dict with save you typing some keys, while dealing with dicts!

Instead of type ['key'] every time, you can just type .key saving 3 key strokes per attribute access

5 Likes